Fix timezone issues with sunrise/sunset times
This commit is contained in:
@@ -67,17 +67,17 @@ function getWeatherUndergroundData( location, weatherUndergroundKey, callback )
|
|||||||
try {
|
try {
|
||||||
var data = JSON.parse( data ),
|
var data = JSON.parse( data ),
|
||||||
|
|
||||||
date = new Date( Date.UTC( 1970, 0, 1, 0, 0, data.current_observation.local_epoch, 0 ) ),
|
tzOffset = getTimezone( data.current_observation.local_tz_offset, "minutes" ),
|
||||||
|
|
||||||
// Calculate sunrise and sunset since Weather Underground does not provide it
|
// Calculate sunrise and sunset since Weather Underground does not provide it
|
||||||
sunData = SunCalc.getTimes( date,
|
sunData = SunCalc.getTimes( data.current_observation.local_epoch * 1000,
|
||||||
data.current_observation.observation_location.latitude,
|
data.current_observation.observation_location.latitude,
|
||||||
data.current_observation.observation_location.longitude ),
|
data.current_observation.observation_location.longitude ),
|
||||||
weather = {
|
weather = {
|
||||||
icon: data.current_observation.icon,
|
icon: data.current_observation.icon,
|
||||||
timezone: data.current_observation.local_tz_offset,
|
timezone: data.current_observation.local_tz_offset,
|
||||||
sunrise: ( sunData.sunrise.getHours() * 60 + sunData.sunrise.getMinutes() ),
|
sunrise: ( sunData.sunrise.getUTCHours() * 60 + sunData.sunrise.getUTCMinutes() ) + tzOffset,
|
||||||
sunset: ( sunData.sunset.getHours() * 60 + sunData.sunset.getMinutes() ),
|
sunset: ( sunData.sunset.getUTCHours() * 60 + sunData.sunset.getUTCMinutes() ) + tzOffset,
|
||||||
maxTemp: parseInt( data.history.dailysummary[0].maxtempi ),
|
maxTemp: parseInt( data.history.dailysummary[0].maxtempi ),
|
||||||
minTemp: parseInt( data.history.dailysummary[0].mintempi ),
|
minTemp: parseInt( data.history.dailysummary[0].mintempi ),
|
||||||
temp: data.current_observation.temp_f,
|
temp: data.current_observation.temp_f,
|
||||||
@@ -88,8 +88,6 @@ function getWeatherUndergroundData( location, weatherUndergroundKey, callback )
|
|||||||
elevation: data.current_observation.observation_location.elevation
|
elevation: data.current_observation.observation_location.elevation
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log( date, date.getTimezoneOffset() );
|
|
||||||
|
|
||||||
if ( weather.sunrise > weather.sunset ) {
|
if ( weather.sunrise > weather.sunset ) {
|
||||||
weather.sunset += 1440;
|
weather.sunset += 1440;
|
||||||
}
|
}
|
||||||
@@ -425,19 +423,27 @@ function httpRequest( url, callback ) {
|
|||||||
// Accepts a time string formatted in ISO-8601 or just the timezone
|
// Accepts a time string formatted in ISO-8601 or just the timezone
|
||||||
// offset and returns the timezone.
|
// offset and returns the timezone.
|
||||||
// The timezone output is formatted for OpenSprinkler Unified firmware.
|
// The timezone output is formatted for OpenSprinkler Unified firmware.
|
||||||
function getTimezone( time ) {
|
function getTimezone( time, format ) {
|
||||||
|
|
||||||
// Match the provided time string against a regex for parsing
|
// Match the provided time string against a regex for parsing
|
||||||
time = time.match( filters.time ) || time.match( filters.timezone );
|
time = time.match( filters.time ) || time.match( filters.timezone );
|
||||||
|
|
||||||
var hour = parseInt( time[7] + time[8] ),
|
var hour = parseInt( time[7] + time[8] ),
|
||||||
minute = parseInt( time[9] );
|
minute = parseInt( time[9] ),
|
||||||
|
tz;
|
||||||
|
|
||||||
// Convert the timezone into the OpenSprinkler encoded format
|
if ( format === "minutes" ) {
|
||||||
minute = ( minute / 15 >> 0 ) / 4;
|
tz = ( hour * 60 ) + minute;
|
||||||
hour = hour + ( hour >= 0 ? minute : -minute );
|
} else {
|
||||||
|
|
||||||
return ( ( hour + 12 ) * 4 ) >> 0;
|
// Convert the timezone into the OpenSprinkler encoded format
|
||||||
|
minute = ( minute / 15 >> 0 ) / 4;
|
||||||
|
hour = hour + ( hour >= 0 ? minute : -minute );
|
||||||
|
|
||||||
|
tz = ( ( hour + 12 ) * 4 ) >> 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tz;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to return the sunrise and sunset times from the weather reply
|
// Function to return the sunrise and sunset times from the weather reply
|
||||||
|
|||||||
Reference in New Issue
Block a user