Merge pull request #92 from goedson/fix_day_night_rendering

Fix calculation of Sunrise and Sunset times
This commit is contained in:
pavolzibrita 2011-10-18 14:59:38 -07:00
commit d6b1ac46c3
2 changed files with 29 additions and 8 deletions

View file

@ -174,6 +174,8 @@ package net.osmand;
// Import required classes and packages // Import required classes and packages
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
@ -258,16 +260,21 @@ public class SunriseSunset
double dfLatIn, // latitude double dfLatIn, // latitude
double dfLonIn, // longitude double dfLonIn, // longitude
Date dateInputIn, // date Date dateInputIn, // date
double dfTimeZoneIn // time zone TimeZone tzIn // time zone
) )
{ {
// Calculate internal representation of timezone offset as fraction of hours from GMT
// Our calculations consider offsets to the West as positive, so we must invert
// the signal of the values provided by the standard library
double dfTimeZoneIn = -1.0 * tzIn.getOffset(dateInputIn.getTime()) / 3600000;
// Copy values supplied as agruments to local variables. // Copy values supplied as agruments to local variables.
dfLat = dfLatIn; dfLat = dfLatIn;
dfLon = dfLonIn; dfLon = dfLonIn;
dateInput = dateInputIn; dateInput = dateInputIn;
dfTimeZone = dfTimeZoneIn; dfTimeZone = dfTimeZoneIn;
origTimeZone = dfTimeZoneIn; origTimeZone= dfTimeZoneIn;
// Call the method to do the calculations. // Call the method to do the calculations.
doCalculations(); doCalculations();
@ -602,6 +609,15 @@ public class SunriseSunset
// Load dateSunrise with data // Load dateSunrise with data
dfmtDateTime = new SimpleDateFormat( "d M yyyy HH:mm z" ); dfmtDateTime = new SimpleDateFormat( "d M yyyy HH:mm z" );
// Timezone signal is reversed in SunriseSunset class
String tz_signal = origTimeZone <= 0?"+":"-";
double abs_tz = Math.abs(origTimeZone);
NumberFormat formatter = new DecimalFormat("00");
String tz_offset_hours = formatter.format((int)abs_tz);
String tz_offset_minutes = formatter.format((int)(60 * (abs_tz - (int)abs_tz)));
if( bSunriseToday ) if( bSunriseToday )
{ {
dateSunrise = dfmtDateTime.parse( iDay dateSunrise = dfmtDateTime.parse( iDay
@ -609,7 +625,9 @@ public class SunriseSunset
+ " " + iYear + " " + iYear
+ " " + (int)dfHourRise + " " + (int)dfHourRise
+ ":" + (int)dfMinRise + ":" + (int)dfMinRise
+ " GMT+"+origTimeZone ); + " GMT"
+ tz_signal + tz_offset_hours
+":" + tz_offset_minutes );
} }
// Load dateSunset with data // Load dateSunset with data
@ -620,7 +638,9 @@ public class SunriseSunset
+ " " + iYear + " " + iYear
+ " " + (int)dfHourSet + " " + (int)dfHourSet
+ ":" + (int)dfMinSet + ":" + (int)dfMinSet
+ " GMT+"+origTimeZone ); + " GMT"
+ tz_signal + tz_offset_hours
+":" + tz_offset_minutes );
} }
} // end of try } // end of try

View file

@ -86,8 +86,9 @@ public class DayNightHelper implements SensorEventListener {
} }
double longitude = lastKnownLocation.getLongitude(); double longitude = lastKnownLocation.getLongitude();
Date actualTime = new Date(); Date actualTime = new Date();
SunriseSunset daynightSwitch = new SunriseSunset(lastKnownLocation.getLatitude(), longitude < 0 ? 360 - longitude SunriseSunset daynightSwitch = new SunriseSunset(lastKnownLocation.getLatitude(),
: longitude, actualTime, TimeZone.getDefault().getOffset(actualTime.getTime()) / 3600000); longitude < 0 ? 360 - longitude : longitude, actualTime,
TimeZone.getDefault());
boolean daytime = daynightSwitch.isDaytime(); boolean daytime = daynightSwitch.isDaytime();
log.debug("Sunrise/sunset setting to day: " + daytime); //$NON-NLS-1$ log.debug("Sunrise/sunset setting to day: " + daytime); //$NON-NLS-1$
lastAutoValue = Boolean.valueOf(daytime); lastAutoValue = Boolean.valueOf(daytime);