X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Ffunction%2Fweather%2FWeatherResults.java;h=10fd3635fd5474326dbc1ba6b3ba527f59cd132b;hp=83c5ca0dfeb0828cfe9f0666d3e6768bca780cbe;hb=88f2c3647ed9e055090484f01a959d4581f85e7d;hpb=326f489e36aa7f235bc19409a57bf4955cd50f24 diff --git a/tim/prune/function/weather/WeatherResults.java b/tim/prune/function/weather/WeatherResults.java index 83c5ca0..10fd363 100644 --- a/tim/prune/function/weather/WeatherResults.java +++ b/tim/prune/function/weather/WeatherResults.java @@ -1,6 +1,10 @@ package tim.prune.function.weather; import java.util.ArrayList; +import java.util.Calendar; +import java.util.TimeZone; + +import tim.prune.gui.DisplayUtils; /** @@ -96,8 +100,9 @@ public class WeatherResults if (inRiseTime != null && inRiseTime.length() == 19 && inSetTime != null && inSetTime.length() == 19) { - _sunriseTime = inRiseTime.substring(11, 16); - _sunsetTime = inSetTime.substring(11, 16); + // Convert from UTC to system's time zone (not necessarily target's time zone!) + _sunriseTime = convertToLocalTimezone(inRiseTime.substring(11, 16)); + _sunsetTime = convertToLocalTimezone(inSetTime.substring(11, 16)); } } @@ -130,4 +135,32 @@ public class WeatherResults { return _updateTime; } + + /** + * Convert the given UTC time (HH:MM) into current timezone of computer + * @param inTimeString sunrise/sunset time in UTC (HH:MM) + * @return time in this timezone (HH:MM) + */ + private static String convertToLocalTimezone(String inTimeString) + { + if (inTimeString != null && inTimeString.length() == 5) + { + try + { + final int hour = Integer.parseInt(inTimeString.substring(0, 2)); + final int min = Integer.parseInt(inTimeString.substring(3)); + Calendar utcCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); + utcCal.set(Calendar.HOUR_OF_DAY, hour); + utcCal.set(Calendar.MINUTE, min); + // Make a second calendar in the current time zone and apply values + Calendar currCal = Calendar.getInstance(); + currCal.setTimeInMillis(utcCal.getTimeInMillis()); + return DisplayUtils.makeTimeString(currCal.get(Calendar.HOUR_OF_DAY), + currCal.get(Calendar.MINUTE)); + } + catch (NumberFormatException e) {} // ignore, just drop through + } + // Couldn't be parsed / converted + return inTimeString; + } }