]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/caltztst.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2010, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
11 * Modification History:
13 * Date Name Description
14 * 08/06/97 aliu Creation.
15 ********************************************************************************
18 #include "unicode/utypes.h"
20 #if !UCONFIG_NO_FORMATTING
23 #include "unicode/smpdtfmt.h"
26 DateFormat
* CalendarTimeZoneTest::fgDateFormat
= 0;
27 Calendar
* CalendarTimeZoneTest::fgCalendar
= 0;
29 UBool
CalendarTimeZoneTest::failure(UErrorCode status
, const char* msg
, UBool possibleDataError
)
31 if (U_FAILURE(status
))
33 if (possibleDataError
) {
34 dataerrln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
36 errcheckln(status
, UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
43 DateFormat
* CalendarTimeZoneTest::getDateFormat()
45 DateFormat
*theFormat
= 0;
47 if (fgDateFormat
!= 0) // if there's something in the cache
51 if (fgDateFormat
!= 0) // Someone might have grabbed it.
53 theFormat
= fgDateFormat
;
54 fgDateFormat
= 0; // We have exclusive right to this formatter.
58 if(theFormat
== 0) // If we weren't able to pull it out of the cache, then we have to create it.
60 UErrorCode status
= U_ZERO_ERROR
;
61 theFormat
= new SimpleDateFormat(UnicodeString("EEE MMM dd HH:mm:ss zzz yyyy"), status
);
62 if (U_FAILURE(status
))
66 dataerrln("FAIL: Could not create SimpleDateFormat - %s", u_errorName(status
));
73 void CalendarTimeZoneTest::releaseDateFormat(DateFormat
*adopt
)
75 if(fgDateFormat
== 0) // If the cache is empty we must add it back.
90 Calendar
* CalendarTimeZoneTest::getCalendar()
92 Calendar
*theCalendar
= 0;
94 if (fgCalendar
!= 0) // if there's something in the cache
98 if (fgCalendar
!= 0) // Someone might have grabbed it.
100 theCalendar
= fgCalendar
;
101 fgCalendar
= 0; // We have exclusive right to this calendar.
105 if(theCalendar
== 0) // If we weren't able to pull it out of the cache, then we have to create it.
107 UErrorCode status
= U_ZERO_ERROR
;
108 theCalendar
= Calendar::createInstance(status
);
109 if (U_FAILURE(status
))
113 dataerrln("FAIL: Calendar::createInstance failed: %s", u_errorName(status
));
119 void CalendarTimeZoneTest::releaseCalendar(Calendar
* adopt
)
121 if(fgCalendar
== 0) // If the cache is empty we must add it back.
137 // Utility method for formatting dates for printing; useful for Java->C++ conversion.
138 // Tries to mimic the Java Date.toString() format.
140 CalendarTimeZoneTest::dateToString(UDate d
)
143 return dateToString(d
, str
);
147 CalendarTimeZoneTest::dateToString(UDate d
, UnicodeString
& str
)
150 DateFormat
* format
= getDateFormat();
153 str
+= "DATE_FORMAT_FAILURE";
156 format
->format(d
, str
);
157 releaseDateFormat(format
);
162 CalendarTimeZoneTest::dateToString(UDate d
, UnicodeString
& str
,
166 DateFormat
* format
= getDateFormat();
169 str
+= "DATE_FORMAT_FAILURE";
172 TimeZone
* save
= format
->getTimeZone().clone();
173 format
->setTimeZone(tz
);
174 format
->format(d
, str
);
175 format
->adoptTimeZone(save
);
176 releaseDateFormat(format
);
180 // Utility methods to create a date. This is useful for converting Java constructs
181 // which create a Date object.
183 CalendarTimeZoneTest::date(int32_t y
, int32_t m
, int32_t d
, int32_t hr
, int32_t min
, int32_t sec
)
185 Calendar
* cal
= getCalendar();
186 if (cal
== 0) return 0.0;
188 cal
->set(1900 + y
, m
, d
, hr
, min
, sec
); // Add 1900 to follow java.util.Date protocol
189 UErrorCode status
= U_ZERO_ERROR
;
190 UDate dt
= cal
->getTime(status
);
191 releaseCalendar(cal
);
192 if (U_FAILURE(status
))
194 errln("FAIL: Calendar::getTime failed: %s", u_errorName(status
));
200 // Utility methods to create a date. The returned Date is UTC rather than local.
202 CalendarTimeZoneTest::utcDate(int32_t y, int32_t m, int32_t d, int32_t hr, int32_t min, int32_t sec)
204 Calendar* cal = getCalendar();
205 if (cal == 0) return 0.0;
206 UErrorCode status = U_ZERO_ERROR;
207 Date dt = date(y, m, d, hr, min, sec) +
208 cal->get(Calendar::ZONE_OFFSET, status) -
209 cal->get(Calendar::DST_OFFSET, status);
210 releaseCalendar(cal);
211 if (U_FAILURE(status))
213 errln("FAIL: Calendar::get failed");
219 // Mimics Date.getYear() etc.
221 CalendarTimeZoneTest::dateToFields(UDate date
, int32_t& y
, int32_t& m
, int32_t& d
, int32_t& hr
, int32_t& min
, int32_t& sec
)
223 Calendar
* cal
= getCalendar();
224 if (cal
== 0) return;
225 UErrorCode status
= U_ZERO_ERROR
;
226 cal
->setTime(date
, status
);
227 y
= cal
->get(UCAL_YEAR
, status
) - 1900;
228 m
= cal
->get(UCAL_MONTH
, status
);
229 d
= cal
->get(UCAL_DATE
, status
);
230 hr
= cal
->get(UCAL_HOUR_OF_DAY
, status
);
231 min
= cal
->get(UCAL_MINUTE
, status
);
232 sec
= cal
->get(UCAL_SECOND
, status
);
233 releaseCalendar(cal
);
236 void CalendarTimeZoneTest::cleanup()
244 #endif /* #if !UCONFIG_NO_FORMATTING */