]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/weather/WeatherResults.java
Version 19.2, December 2018
[GpsPrune.git] / tim / prune / function / weather / WeatherResults.java
1 package tim.prune.function.weather;
2
3 import java.util.ArrayList;
4 import java.util.Calendar;
5 import java.util.TimeZone;
6
7 import tim.prune.gui.DisplayUtils;
8
9
10 /**
11  * Model for results of weather forecast from openweathermap.org
12  */
13 public class WeatherResults
14 {
15         /** List of forecasts */
16         private ArrayList<SingleForecast> _forecastList = new ArrayList<SingleForecast>();
17         /** Flag whether the units are metric (Celsius) or not (Fahrenheit) */
18         private boolean _tempsCelsius = true;
19         /** Location name */
20         private String _locationName = null;
21         /** Last update timestamp */
22         private String _updateTime = null;
23         /** Sunrise and sunset times as HH:MM */
24         private String _sunriseTime = null, _sunsetTime = null;
25
26
27         /**
28          * Add a single forecast to this model (for the current weather)
29          * @param inResults current results
30          */
31         public void setForecast(SingleForecast inResults)
32         {
33                 _forecastList.clear();
34                 if (inResults != null) {
35                         _forecastList.add(inResults);
36                 }
37         }
38
39         /**
40          * Add a list of forecasts to this model
41          * @param inList list of forecasts to add
42          */
43         public void setForecasts(ArrayList<SingleForecast> inList)
44         {
45                 _forecastList.clear();
46                 if (inList != null && inList.size() > 0) {
47                         _forecastList.addAll(inList);
48                 }
49         }
50
51         /** @return the number of forecasts */
52         public int getNumForecasts()
53         {
54                 return _forecastList.size();
55         }
56
57         /**
58          * @param inIndex index of forecast starting from 0
59          * @return the specified forecast
60          */
61         public SingleForecast getForecast(int inIndex)
62         {
63                 if (inIndex < 0 || inIndex >= getNumForecasts()) {
64                         return null;
65                 }
66                 return _forecastList.get(inIndex);
67         }
68
69         /**
70          * Clear the list of forecasts
71          */
72         public void clear()
73         {
74                 _forecastList.clear();
75                 _sunriseTime = _sunsetTime = null;
76                 _updateTime = null;
77         }
78
79         /**
80          * @param inCelsius true for celsius, false for fahrenheit
81          */
82         public void setTempsCelsius(boolean inCelsius)
83         {
84                 _tempsCelsius = inCelsius;
85         }
86
87         /** @return true if this forecast uses Celsius */
88         public boolean isCelsius() {
89                 return _tempsCelsius;
90         }
91
92         /**
93          * Set the sunrise and sunset times (only from current weather, not from forecast)
94          * @param inRiseTime sunrise time as YYYY-MM-DDThh:mm:ss
95          * @param inSetTime  sunset  time as YYYY-MM-DDThh:mm:ss
96          */
97         public void setSunriseSunsetTimes(String inRiseTime, String inSetTime)
98         {
99                 _sunriseTime = _sunsetTime = null;
100                 if (inRiseTime != null && inRiseTime.length() == 19
101                         && inSetTime != null && inSetTime.length() == 19)
102                 {
103                         // Convert from UTC to system's time zone (not necessarily target's time zone!)
104                         _sunriseTime = convertToLocalTimezone(inRiseTime.substring(11, 16));
105                         _sunsetTime  = convertToLocalTimezone(inSetTime.substring(11, 16));
106                 }
107         }
108
109         /** @return sunrise time as HH:MM */
110         public String getSunriseTime() {
111                 return _sunriseTime;
112         }
113         /** @return sunset time as HH:MM */
114         public String getSunsetTime() {
115                 return _sunsetTime;
116         }
117
118         /** @param inName location name */
119         public void setLocationName(String inName) {
120                 _locationName = inName;
121         }
122
123         /** @return location name */
124         public String getLocationName() {
125                 return _locationName;
126         }
127
128         /** @param inTime timestamp of forecast */
129         public void setUpdateTime(String inTime) {
130                 _updateTime = inTime;
131         }
132
133         /** @return timestamp of last update */
134         public String getUpdateTime()
135         {
136                 return _updateTime;
137         }
138
139         /**
140          * Convert the given UTC time (HH:MM) into current timezone of computer
141          * @param inTimeString sunrise/sunset time in UTC (HH:MM)
142          * @return time in this timezone (HH:MM)
143          */
144         private static String convertToLocalTimezone(String inTimeString)
145         {
146                 if (inTimeString != null && inTimeString.length() == 5)
147                 {
148                         try
149                         {
150                                 final int hour = Integer.parseInt(inTimeString.substring(0, 2));
151                                 final int min  = Integer.parseInt(inTimeString.substring(3));
152                                 Calendar utcCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
153                                 utcCal.set(Calendar.HOUR_OF_DAY, hour);
154                                 utcCal.set(Calendar.MINUTE, min);
155                                 // Make a second calendar in the current time zone and apply values
156                                 Calendar currCal = Calendar.getInstance();
157                                 currCal.setTimeInMillis(utcCal.getTimeInMillis());
158                                 return DisplayUtils.makeTimeString(currCal.get(Calendar.HOUR_OF_DAY),
159                                         currCal.get(Calendar.MINUTE));
160                         }
161                         catch (NumberFormatException e) {} // ignore, just drop through
162                 }
163                 // Couldn't be parsed / converted
164                 return inTimeString;
165         }
166 }