1
/********************************************************************
3
* Copyright (c) 1997-2009, International Business Machines Corporation and
4
* others. All Rights Reserved.
5
********************************************************************/
9
* Modification History:
11
* Date Name Description
12
* 08/06/97 aliu Creation.
13
********************************************************************************
16
#include "unicode/utypes.h"
18
#if !UCONFIG_NO_FORMATTING
21
#include "unicode/smpdtfmt.h"
24
DateFormat* CalendarTimeZoneTest::fgDateFormat = 0;
25
Calendar* CalendarTimeZoneTest::fgCalendar = 0;
27
UBool CalendarTimeZoneTest::failure(UErrorCode status, const char* msg, UBool possibleDataError)
29
if (U_FAILURE(status))
31
if (possibleDataError) {
32
dataerrln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
34
errcheckln(status, UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
41
DateFormat* CalendarTimeZoneTest::getDateFormat()
43
DateFormat *theFormat = 0;
45
if (fgDateFormat != 0) // if there's something in the cache
49
if (fgDateFormat != 0) // Someone might have grabbed it.
51
theFormat = fgDateFormat;
52
fgDateFormat = 0; // We have exclusive right to this formatter.
56
if(theFormat == 0) // If we weren't able to pull it out of the cache, then we have to create it.
58
UErrorCode status = U_ZERO_ERROR;
59
theFormat = new SimpleDateFormat(UnicodeString("EEE MMM dd HH:mm:ss zzz yyyy"), status);
60
if (U_FAILURE(status))
64
errcheckln(status, "FAIL: Could not create SimpleDateFormat - %s", u_errorName(status));
71
void CalendarTimeZoneTest::releaseDateFormat(DateFormat *adopt)
73
if(fgDateFormat == 0) // If the cache is empty we must add it back.
88
Calendar* CalendarTimeZoneTest::getCalendar()
90
Calendar *theCalendar = 0;
92
if (fgCalendar != 0) // if there's something in the cache
96
if (fgCalendar != 0) // Someone might have grabbed it.
98
theCalendar = fgCalendar;
99
fgCalendar = 0; // We have exclusive right to this calendar.
103
if(theCalendar == 0) // If we weren't able to pull it out of the cache, then we have to create it.
105
UErrorCode status = U_ZERO_ERROR;
106
theCalendar = Calendar::createInstance(status);
107
if (U_FAILURE(status))
111
errln("FAIL: Calendar::createInstance failed");
117
void CalendarTimeZoneTest::releaseCalendar(Calendar* adopt)
119
if(fgCalendar == 0) // If the cache is empty we must add it back.
135
// Utility method for formatting dates for printing; useful for Java->C++ conversion.
136
// Tries to mimic the Java Date.toString() format.
138
CalendarTimeZoneTest::dateToString(UDate d)
141
return dateToString(d, str);
145
CalendarTimeZoneTest::dateToString(UDate d, UnicodeString& str)
148
DateFormat* format = getDateFormat();
151
str += "DATE_FORMAT_FAILURE";
154
format->format(d, str);
155
releaseDateFormat(format);
160
CalendarTimeZoneTest::dateToString(UDate d, UnicodeString& str,
164
DateFormat* format = getDateFormat();
167
str += "DATE_FORMAT_FAILURE";
170
TimeZone* save = format->getTimeZone().clone();
171
format->setTimeZone(tz);
172
format->format(d, str);
173
format->adoptTimeZone(save);
174
releaseDateFormat(format);
178
// Utility methods to create a date. This is useful for converting Java constructs
179
// which create a Date object.
181
CalendarTimeZoneTest::date(int32_t y, int32_t m, int32_t d, int32_t hr, int32_t min, int32_t sec)
183
Calendar* cal = getCalendar();
184
if (cal == 0) return 0.0;
186
cal->set(1900 + y, m, d, hr, min, sec); // Add 1900 to follow java.util.Date protocol
187
UErrorCode status = U_ZERO_ERROR;
188
UDate dt = cal->getTime(status);
189
releaseCalendar(cal);
190
if (U_FAILURE(status))
192
errln("FAIL: Calendar::getTime failed");
198
// Utility methods to create a date. The returned Date is UTC rather than local.
200
CalendarTimeZoneTest::utcDate(int32_t y, int32_t m, int32_t d, int32_t hr, int32_t min, int32_t sec)
202
Calendar* cal = getCalendar();
203
if (cal == 0) return 0.0;
204
UErrorCode status = U_ZERO_ERROR;
205
Date dt = date(y, m, d, hr, min, sec) +
206
cal->get(Calendar::ZONE_OFFSET, status) -
207
cal->get(Calendar::DST_OFFSET, status);
208
releaseCalendar(cal);
209
if (U_FAILURE(status))
211
errln("FAIL: Calendar::get failed");
217
// Mimics Date.getYear() etc.
219
CalendarTimeZoneTest::dateToFields(UDate date, int32_t& y, int32_t& m, int32_t& d, int32_t& hr, int32_t& min, int32_t& sec)
221
Calendar* cal = getCalendar();
222
if (cal == 0) return;
223
UErrorCode status = U_ZERO_ERROR;
224
cal->setTime(date, status);
225
y = cal->get(UCAL_YEAR, status) - 1900;
226
m = cal->get(UCAL_MONTH, status);
227
d = cal->get(UCAL_DATE, status);
228
hr = cal->get(UCAL_HOUR_OF_DAY, status);
229
min = cal->get(UCAL_MINUTE, status);
230
sec = cal->get(UCAL_SECOND, status);
231
releaseCalendar(cal);
234
void CalendarTimeZoneTest::cleanup()
242
#endif /* #if !UCONFIG_NO_FORMATTING */