Fix calculation of Sunrise and Sunset times

Fixed the Sunrise/Sunset time calculation by:

- Inverting signal of timezone offset when calling SunriseSunset's
  constructor in DayNightHelper
- Properly formating the timezone offset when parsing the times in
  SunriseSunset's calculations
This commit is contained in:
Goedson Teixeira Paixao 2011-10-10 23:06:19 -03:00
parent 80be496580
commit f4b1156625
2 changed files with 20 additions and 4 deletions

View file

@ -174,6 +174,8 @@ package net.osmand;
// Import required classes and packages
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.TimeZone;
@ -602,6 +604,15 @@ public class SunriseSunset
// Load dateSunrise with data
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 )
{
dateSunrise = dfmtDateTime.parse( iDay
@ -609,7 +620,9 @@ public class SunriseSunset
+ " " + iYear
+ " " + (int)dfHourRise
+ ":" + (int)dfMinRise
+ " GMT+"+origTimeZone );
+ " GMT"
+ tz_signal + tz_offset_hours
+":" + tz_offset_minutes );
}
// Load dateSunset with data
@ -620,7 +633,9 @@ public class SunriseSunset
+ " " + iYear
+ " " + (int)dfHourSet
+ ":" + (int)dfMinSet
+ " GMT+"+origTimeZone );
+ " GMT"
+ tz_signal + tz_offset_hours
+":" + tz_offset_minutes );
}
} // end of try

View file

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