xref: /haiku/src/system/libroot/add-ons/icu/ICUTimeConversion.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2010-2011, Oliver Tappe, zooey@hirschkaefer.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "ICUTimeConversion.h"
8 
9 #include <math.h>
10 #include <string.h>
11 #include <strings.h>
12 
13 #include <unicode/gregocal.h>
14 
15 
16 U_NAMESPACE_USE
17 
18 
19 namespace BPrivate {
20 namespace Libroot {
21 
22 
23 ICUTimeConversion::ICUTimeConversion(const ICUTimeData& timeData)
24 	:
25 	fTimeData(timeData),
26 	fDataBridge(NULL),
27 	fTimeZone(NULL)
28 {
29 	fTimeZoneID[0] = '\0';
30 }
31 
32 
33 ICUTimeConversion::~ICUTimeConversion()
34 {
35 	delete fTimeZone;
36 }
37 
38 
39 void
40 ICUTimeConversion::Initialize(TimeConversionDataBridge* dataBridge)
41 {
42 	fDataBridge = dataBridge;
43 }
44 
45 
46 status_t
47 ICUTimeConversion::TZSet(const char* timeZoneID, const char* tz)
48 {
49 	bool offsetHasBeenSet = false;
50 	bool timeZoneIdMatches = false;
51 
52 	// The given TZ environment variable's content overrides the default
53 	// system timezone.
54 	if (tz != NULL) {
55 		// If the value given in the TZ env-var starts with a colon, that
56 		// value is implementation specific, we expect a full timezone ID.
57 		if (*tz == ':') {
58 			// nothing to do if the given name matches the current timezone
59 			if (strcasecmp(fTimeZoneID, tz + 1) == 0) {
60 				timeZoneIdMatches = true;
61 				goto done;
62 			}
63 
64 			strlcpy(fTimeZoneID, tz + 1, sizeof(fTimeZoneID));
65 		} else {
66 			// note timezone name
67 			strlcpy(fTimeZoneID, tz, sizeof(fTimeZoneID));
68 
69 			// nothing to do if the given name matches the current timezone
70 			if (strcasecmp(fTimeZoneID, fDataBridge->addrOfTZName[0]) == 0) {
71 				timeZoneIdMatches = true;
72 				goto done;
73 			}
74 
75 			// parse TZ variable (only <std> and <offset> supported)
76 			const char* tzNameEnd = tz;
77 			while(isalpha(*tzNameEnd))
78 				++tzNameEnd;
79 			if (*tzNameEnd == '-' || *tzNameEnd == '+') {
80 				int hours = 0;
81 				int minutes = 0;
82 				int seconds = 0;
83 				sscanf(tzNameEnd + 1, "%2d:%2d:%2d", &hours, &minutes,
84 					&seconds);
85 				hours = min_c(24, max_c(0, hours));
86 				minutes = min_c(59, max_c(0, minutes));
87 				seconds = min_c(59, max_c(0, seconds));
88 
89 				*fDataBridge->addrOfTimezone = (*tzNameEnd == '-' ? -1 : 1)
90 					* (hours * 3600 + minutes * 60 + seconds);
91 				offsetHasBeenSet = true;
92 			}
93 		}
94 	} else {
95 		// nothing to do if the given name matches the current timezone
96 		if (strcasecmp(fTimeZoneID, timeZoneID) == 0) {
97 			timeZoneIdMatches = true;
98 			goto done;
99 		}
100 
101 		strlcpy(fTimeZoneID, timeZoneID, sizeof(fTimeZoneID));
102 	}
103 
104 done:
105 	// fTimeZone can still be NULL if we don't initialize it
106 	// in the first TZSet, causing problems for future
107 	// Localtime invocations.
108 	if (fTimeZone != NULL && timeZoneIdMatches)
109 		return B_OK;
110 
111 	delete fTimeZone;
112 	fTimeZone = TimeZone::createTimeZone(fTimeZoneID);
113 	if (fTimeZone == NULL)
114 		return B_NO_MEMORY;
115 
116 	if (offsetHasBeenSet) {
117 		fTimeZone->setRawOffset(*fDataBridge->addrOfTimezone * -1 * 1000);
118 	} else {
119 		int32_t rawOffset;
120 		int32_t dstOffset;
121 		UDate nowMillis = 1000 * (UDate)time(NULL);
122 		UErrorCode icuStatus = U_ZERO_ERROR;
123 		fTimeZone->getOffset(nowMillis, FALSE, rawOffset, dstOffset, icuStatus);
124 		if (!U_SUCCESS(icuStatus)) {
125 			*fDataBridge->addrOfTimezone = 0;
126 			*fDataBridge->addrOfDaylight = false;
127 			strcpy(fDataBridge->addrOfTZName[0], "GMT");
128 			strcpy(fDataBridge->addrOfTZName[1], "GMT");
129 
130 			return B_ERROR;
131 		}
132 		*fDataBridge->addrOfTimezone = -1 * (rawOffset + dstOffset) / 1000;
133 			// we want seconds, not the ms that ICU gives us
134 	}
135 
136 	*fDataBridge->addrOfDaylight = fTimeZone->useDaylightTime();
137 
138 	for (int i = 0; i < 2; ++i) {
139 		if (tz != NULL && *tz != ':' && i == 0) {
140 			strcpy(fDataBridge->addrOfTZName[0], fTimeZoneID);
141 		} else {
142 			UnicodeString icuString;
143 			fTimeZone->getDisplayName(i == 1, TimeZone::SHORT,
144 				fTimeData.ICULocaleForStrings(), icuString);
145 			CheckedArrayByteSink byteSink(fDataBridge->addrOfTZName[i],
146 				sizeof(fTimeZoneID));
147 			icuString.toUTF8(byteSink);
148 
149 			// make sure to canonicalize "GMT+00:00" to just "GMT"
150 			if (strcmp(fDataBridge->addrOfTZName[i], "GMT+00:00") == 0)
151 				fDataBridge->addrOfTZName[i][3] = '\0';
152 		}
153 	}
154 
155 	return B_OK;
156 }
157 
158 
159 status_t
160 ICUTimeConversion::Localtime(const time_t* inTime, struct tm* tmOut)
161 {
162 	if (fTimeZone == NULL)
163 		return B_NO_INIT;
164 
165 	tmOut->tm_zone = fTimeZoneID;
166 	return _FillTmValues(fTimeZone, inTime, tmOut);
167 }
168 
169 
170 status_t
171 ICUTimeConversion::Gmtime(const time_t* inTime, struct tm* tmOut)
172 {
173 	const TimeZone* icuTimeZone = TimeZone::getGMT();
174 		// no delete - doesn't belong to us
175 
176 	return _FillTmValues(icuTimeZone, inTime, tmOut);
177 }
178 
179 
180 status_t
181 ICUTimeConversion::Mktime(struct tm* inOutTm, time_t& timeOut)
182 {
183 	return _Mktime(fTimeZone, inOutTm, timeOut);
184 }
185 
186 
187 status_t
188 ICUTimeConversion::Timegm(struct tm* inOutTm, time_t& timeOut)
189 {
190 	const TimeZone* icuTimeZone = TimeZone::getGMT();
191 	return _Mktime(icuTimeZone, inOutTm, timeOut);
192 }
193 
194 
195 status_t
196 ICUTimeConversion::_FillTmValues(const TimeZone* icuTimeZone,
197 	const time_t* inTime, struct tm* tmOut)
198 {
199 	UErrorCode icuStatus = U_ZERO_ERROR;
200 	GregorianCalendar calendar(*icuTimeZone, fTimeData.ICULocale(), icuStatus);
201 	if (!U_SUCCESS(icuStatus))
202 		return B_ERROR;
203 
204 	calendar.setTime(1000 * (UDate)*inTime, icuStatus);
205 	if (!U_SUCCESS(icuStatus))
206 		return B_ERROR;
207 
208 	tmOut->tm_sec = calendar.get(UCAL_SECOND, icuStatus);
209 	if (!U_SUCCESS(icuStatus))
210 		return B_ERROR;
211 	tmOut->tm_min = calendar.get(UCAL_MINUTE, icuStatus);
212 	if (!U_SUCCESS(icuStatus))
213 		return B_ERROR;
214 	tmOut->tm_hour = calendar.get(UCAL_HOUR_OF_DAY, icuStatus);
215 	if (!U_SUCCESS(icuStatus))
216 		return B_ERROR;
217 	tmOut->tm_mday = calendar.get(UCAL_DAY_OF_MONTH, icuStatus);
218 	if (!U_SUCCESS(icuStatus))
219 		return B_ERROR;
220 	tmOut->tm_mon = calendar.get(UCAL_MONTH, icuStatus);
221 	if (!U_SUCCESS(icuStatus))
222 		return B_ERROR;
223 	tmOut->tm_year = calendar.get(UCAL_YEAR, icuStatus) - 1900;
224 	if (!U_SUCCESS(icuStatus))
225 		return B_ERROR;
226 	tmOut->tm_wday = calendar.get(UCAL_DAY_OF_WEEK, icuStatus) - 1;
227 	if (!U_SUCCESS(icuStatus))
228 		return B_ERROR;
229 	tmOut->tm_yday = calendar.get(UCAL_DAY_OF_YEAR, icuStatus) - 1;
230 	if (!U_SUCCESS(icuStatus))
231 		return B_ERROR;
232 	tmOut->tm_isdst = calendar.inDaylightTime(icuStatus);
233 	if (!U_SUCCESS(icuStatus))
234 		return B_ERROR;
235 	tmOut->tm_gmtoff = (calendar.get(UCAL_ZONE_OFFSET, icuStatus)
236 		+ calendar.get(UCAL_DST_OFFSET, icuStatus)) / 1000;
237 	if (!U_SUCCESS(icuStatus))
238 		return B_ERROR;
239 	tmOut->tm_zone = fDataBridge->addrOfTZName[tmOut->tm_isdst ? 1 : 0];
240 
241 	return B_OK;
242 }
243 
244 
245 status_t
246 ICUTimeConversion::_Mktime(const TimeZone* icuTimeZone,
247 	struct tm* inOutTm, time_t& timeOut)
248 {
249 	if (icuTimeZone == NULL)
250 		return B_NO_INIT;
251 
252 	UErrorCode icuStatus = U_ZERO_ERROR;
253 	GregorianCalendar calendar(*icuTimeZone, fTimeData.ICULocale(),
254 		icuStatus);
255 	if (!U_SUCCESS(icuStatus))
256 		return B_ERROR;
257 
258 	calendar.setLenient(TRUE);
259 	calendar.set(inOutTm->tm_year + 1900, inOutTm->tm_mon, inOutTm->tm_mday,
260 		inOutTm->tm_hour, inOutTm->tm_min, inOutTm->tm_sec);
261 
262 	UDate timeInMillis = calendar.getTime(icuStatus);
263 	if (!U_SUCCESS(icuStatus))
264 		return B_ERROR;
265 	timeOut = (time_t)((int64_t)timeInMillis / 1000);
266 
267 	return _FillTmValues(icuTimeZone, &timeOut, inOutTm);
268 }
269 
270 
271 }	// namespace Libroot
272 }	// namespace BPrivate
273