1 package tim.prune.function.weather;
3 import java.util.ArrayList;
4 import java.util.Calendar;
5 import java.util.TimeZone;
7 import tim.prune.gui.DisplayUtils;
11 * Model for results of weather forecast from openweathermap.org
13 public class WeatherResults
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;
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;
28 * Add a single forecast to this model (for the current weather)
29 * @param inResults current results
31 public void setForecast(SingleForecast inResults)
33 _forecastList.clear();
34 if (inResults != null) {
35 _forecastList.add(inResults);
40 * Add a list of forecasts to this model
41 * @param inList list of forecasts to add
43 public void setForecasts(ArrayList<SingleForecast> inList)
45 _forecastList.clear();
46 if (inList != null && inList.size() > 0) {
47 _forecastList.addAll(inList);
51 /** @return the number of forecasts */
52 public int getNumForecasts()
54 return _forecastList.size();
58 * @param inIndex index of forecast starting from 0
59 * @return the specified forecast
61 public SingleForecast getForecast(int inIndex)
63 if (inIndex < 0 || inIndex >= getNumForecasts()) {
66 return _forecastList.get(inIndex);
70 * Clear the list of forecasts
74 _forecastList.clear();
75 _sunriseTime = _sunsetTime = null;
80 * @param inCelsius true for celsius, false for fahrenheit
82 public void setTempsCelsius(boolean inCelsius)
84 _tempsCelsius = inCelsius;
87 /** @return true if this forecast uses Celsius */
88 public boolean isCelsius() {
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
97 public void setSunriseSunsetTimes(String inRiseTime, String inSetTime)
99 _sunriseTime = _sunsetTime = null;
100 if (inRiseTime != null && inRiseTime.length() == 19
101 && inSetTime != null && inSetTime.length() == 19)
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));
109 /** @return sunrise time as HH:MM */
110 public String getSunriseTime() {
113 /** @return sunset time as HH:MM */
114 public String getSunsetTime() {
118 /** @param inName location name */
119 public void setLocationName(String inName) {
120 _locationName = inName;
123 /** @return location name */
124 public String getLocationName() {
125 return _locationName;
128 /** @param inTime timestamp of forecast */
129 public void setUpdateTime(String inTime) {
130 _updateTime = inTime;
133 /** @return timestamp of last update */
134 public String getUpdateTime()
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)
144 private static String convertToLocalTimezone(String inTimeString)
146 if (inTimeString != null && inTimeString.length() == 5)
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));
161 catch (NumberFormatException e) {} // ignore, just drop through
163 // Couldn't be parsed / converted