1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 2005-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
11 ********************************************************************************
14 #include "unicode/utypes.h"
16 #if U_PLATFORM_HAS_WIN32_API
18 #if !UCONFIG_NO_FORMATTING
20 #include "unicode/ures.h"
21 #include "unicode/format.h"
22 #include "unicode/fmtable.h"
23 #include "unicode/datefmt.h"
24 #include "unicode/simpleformatter.h"
25 #include "unicode/calendar.h"
26 #include "unicode/gregocal.h"
27 #include "unicode/locid.h"
28 #include "unicode/unistr.h"
29 #include "unicode/ustring.h"
30 #include "unicode/timezone.h"
31 #include "unicode/utmscale.h"
36 #include "wintzimpl.h"
38 # define WIN32_LEAN_AND_MEAN
48 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32DateFormat)
50 #define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
51 #define DELETE_ARRAY(array) uprv_free((void *) (array))
53 #define STACK_BUFFER_SIZE 64
55 UnicodeString* Win32DateFormat::getTimeDateFormat(const Calendar *cal, const Locale *locale, UErrorCode &status) const
57 UnicodeString *result = NULL;
58 const char *type = cal->getType();
59 const char *base = locale->getBaseName();
60 UResourceBundle *topBundle = ures_open((char *) 0, base, &status);
61 UResourceBundle *calBundle = ures_getByKey(topBundle, "calendar", NULL, &status);
62 UResourceBundle *typBundle = ures_getByKeyWithFallback(calBundle, type, NULL, &status);
63 UResourceBundle *patBundle = ures_getByKeyWithFallback(typBundle, "DateTimePatterns", NULL, &status);
65 if (status == U_MISSING_RESOURCE_ERROR) {
66 status = U_ZERO_ERROR;
67 typBundle = ures_getByKeyWithFallback(calBundle, "gregorian", typBundle, &status);
68 patBundle = ures_getByKeyWithFallback(typBundle, "DateTimePatterns", patBundle, &status);
71 if (U_FAILURE(status)) {
72 static const UChar defaultPattern[] = {0x007B, 0x0031, 0x007D, 0x0020, 0x007B, 0x0030, 0x007D, 0x0000}; // "{1} {0}"
73 return new UnicodeString(defaultPattern, UPRV_LENGTHOF(defaultPattern));
76 int32_t resStrLen = 0;
77 int32_t glueIndex = DateFormat::kDateTime;
78 int32_t patSize = ures_getSize(patBundle);
79 if (patSize >= (DateFormat::kDateTimeOffset + DateFormat::kShort + 1)) {
80 // Get proper date time format
81 glueIndex = (int32_t)(DateFormat::kDateTimeOffset + (fDateStyle - DateFormat::kDateOffset));
83 const UChar *resStr = ures_getStringByIndex(patBundle, glueIndex, &resStrLen, &status);
85 result = new UnicodeString(TRUE, resStr, resStrLen);
87 ures_close(patBundle);
88 ures_close(typBundle);
89 ures_close(calBundle);
90 ures_close(topBundle);
95 // TODO: Range-check timeStyle, dateStyle
96 Win32DateFormat::Win32DateFormat(DateFormat::EStyle timeStyle, DateFormat::EStyle dateStyle, const Locale &locale, UErrorCode &status)
97 : DateFormat(), fDateTimeMsg(NULL), fTimeStyle(timeStyle), fDateStyle(dateStyle), fLocale(locale), fZoneID()
99 if (U_SUCCESS(status)) {
100 fLCID = locale.getLCID();
101 fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
102 uprv_memset(fTZI, 0, sizeof(TIME_ZONE_INFORMATION));
103 adoptCalendar(Calendar::createInstance(locale, status));
107 Win32DateFormat::Win32DateFormat(const Win32DateFormat &other)
113 Win32DateFormat::~Win32DateFormat()
120 Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other)
122 // The following handles fCalendar
123 DateFormat::operator=(other);
127 this->fDateTimeMsg = other.fDateTimeMsg == NULL ? NULL : new UnicodeString(*other.fDateTimeMsg);
128 this->fTimeStyle = other.fTimeStyle;
129 this->fDateStyle = other.fDateStyle;
130 this->fLocale = other.fLocale;
131 this->fLCID = other.fLCID;
132 // this->fCalendar = other.fCalendar->clone();
133 this->fZoneID = other.fZoneID;
135 this->fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
136 *this->fTZI = *other.fTZI;
141 Format *Win32DateFormat::clone(void) const
143 return new Win32DateFormat(*this);
146 // TODO: Is just ignoring pos the right thing?
147 UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, FieldPosition &pos) const
152 TIME_ZONE_INFORMATION tzi = *fTZI;
153 UErrorCode status = U_ZERO_ERROR;
154 const TimeZone &tz = cal.getTimeZone();
157 setTimeZoneInfo(&tzi, tz);
159 uct = utmscale_fromInt64((int64_t) cal.getTime(status), UDTS_ICU4C_TIME, &status);
160 uft = utmscale_toInt64(uct, UDTS_WINDOWS_FILE_TIME, &status);
162 ft.dwLowDateTime = (DWORD) (uft & 0xFFFFFFFF);
163 ft.dwHighDateTime = (DWORD) ((uft >> 32) & 0xFFFFFFFF);
165 FileTimeToSystemTime(&ft, &st_gmt);
166 SystemTimeToTzSpecificLocalTime(&tzi, &st_gmt, &st_local);
169 if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
172 UnicodeString *pattern = fDateTimeMsg;
174 formatDate(&st_local, date);
175 formatTime(&st_local, time);
177 if (strcmp(fCalendar->getType(), cal.getType()) != 0) {
178 pattern = getTimeDateFormat(&cal, &fLocale, status);
181 SimpleFormatter(*pattern, 2, 2, status).format(time, date, appendTo, status);
182 } else if (fDateStyle != DateFormat::kNone) {
183 formatDate(&st_local, appendTo);
184 } else if (fTimeStyle != DateFormat::kNone) {
185 formatTime(&st_local, appendTo);
191 void Win32DateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& pos) const
193 pos.setErrorIndex(pos.getIndex());
196 void Win32DateFormat::adoptCalendar(Calendar *newCalendar)
198 if (fCalendar == NULL || strcmp(fCalendar->getType(), newCalendar->getType()) != 0) {
199 UErrorCode status = U_ZERO_ERROR;
201 if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
203 fDateTimeMsg = getTimeDateFormat(newCalendar, &fLocale, status);
208 fCalendar = newCalendar;
210 fZoneID = setTimeZoneInfo(fTZI, fCalendar->getTimeZone());
213 void Win32DateFormat::setCalendar(const Calendar &newCalendar)
215 adoptCalendar(newCalendar.clone());
218 void Win32DateFormat::adoptTimeZone(TimeZone *zoneToAdopt)
220 fZoneID = setTimeZoneInfo(fTZI, *zoneToAdopt);
221 fCalendar->adoptTimeZone(zoneToAdopt);
224 void Win32DateFormat::setTimeZone(const TimeZone& zone)
226 fZoneID = setTimeZoneInfo(fTZI, zone);
227 fCalendar->setTimeZone(zone);
230 static const DWORD dfFlags[] = {DATE_LONGDATE, DATE_LONGDATE, DATE_SHORTDATE, DATE_SHORTDATE};
232 void Win32DateFormat::formatDate(const SYSTEMTIME *st, UnicodeString &appendTo) const
235 wchar_t stackBuffer[STACK_BUFFER_SIZE];
236 wchar_t *buffer = stackBuffer;
238 result = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, STACK_BUFFER_SIZE);
241 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
242 int newLength = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, NULL, 0);
244 buffer = NEW_ARRAY(wchar_t, newLength);
245 GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, newLength);
249 appendTo.append((const UChar *)buffer, (int32_t) wcslen(buffer));
251 if (buffer != stackBuffer) {
252 DELETE_ARRAY(buffer);
256 static const DWORD tfFlags[] = {0, 0, 0, TIME_NOSECONDS};
258 void Win32DateFormat::formatTime(const SYSTEMTIME *st, UnicodeString &appendTo) const
261 wchar_t stackBuffer[STACK_BUFFER_SIZE];
262 wchar_t *buffer = stackBuffer;
264 result = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, STACK_BUFFER_SIZE);
267 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
268 int newLength = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, NULL, 0);
270 buffer = NEW_ARRAY(wchar_t, newLength);
271 GetDateFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, newLength);
275 appendTo.append((const UChar *)buffer, (int32_t) wcslen(buffer));
277 if (buffer != stackBuffer) {
278 DELETE_ARRAY(buffer);
282 UnicodeString Win32DateFormat::setTimeZoneInfo(TIME_ZONE_INFORMATION *tzi, const TimeZone &zone) const
284 UnicodeString zoneID;
288 if (zoneID.compare(fZoneID) != 0) {
292 if (! uprv_getWindowsTimeZoneInfo(tzi, icuid.getBuffer(), icuid.length())) {
294 int32_t ec = TimeZone::countEquivalentIDs(icuid);
296 for (int z = 0; z < ec; z += 1) {
297 UnicodeString equiv = TimeZone::getEquivalentID(icuid, z);
299 if (found = uprv_getWindowsTimeZoneInfo(tzi, equiv.getBuffer(), equiv.length())) {
305 GetTimeZoneInformation(tzi);
315 #endif /* #if !UCONFIG_NO_FORMATTING */
317 #endif // U_PLATFORM_HAS_WIN32_API