]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/function/weather/WeatherResults.java
Version 16.3, July 2014
[GpsPrune.git] / tim / prune / function / weather / WeatherResults.java
index 83c5ca0dfeb0828cfe9f0666d3e6768bca780cbe..10fd3635fd5474326dbc1ba6b3ba527f59cd132b 100644 (file)
@@ -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;
+       }
 }