]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/caltztst.cpp
1 /********************************************************************
3 * Copyright (c) 1997-2003, 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
)
29 if (U_FAILURE(status
))
31 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
37 DateFormat
* CalendarTimeZoneTest::getDateFormat()
39 DateFormat
*theFormat
= 0;
41 if (fgDateFormat
!= 0) // if there's something in the cache
45 if (fgDateFormat
!= 0) // Someone might have grabbed it.
47 theFormat
= fgDateFormat
;
48 fgDateFormat
= 0; // We have exclusive right to this formatter.
52 if(theFormat
== 0) // If we weren't able to pull it out of the cache, then we have to create it.
54 UErrorCode status
= U_ZERO_ERROR
;
55 theFormat
= new SimpleDateFormat(UnicodeString("EEE MMM dd HH:mm:ss zzz yyyy"), status
);
56 if (U_FAILURE(status
))
60 errln("FAIL: Could not create SimpleDateFormat");
67 void CalendarTimeZoneTest::releaseDateFormat(DateFormat
*adopt
)
69 if(fgDateFormat
== 0) // If the cache is empty we must add it back.
84 Calendar
* CalendarTimeZoneTest::getCalendar()
86 Calendar
*theCalendar
= 0;
88 if (fgCalendar
!= 0) // if there's something in the cache
92 if (fgCalendar
!= 0) // Someone might have grabbed it.
94 theCalendar
= fgCalendar
;
95 fgCalendar
= 0; // We have exclusive right to this calendar.
99 if(theCalendar
== 0) // If we weren't able to pull it out of the cache, then we have to create it.
101 UErrorCode status
= U_ZERO_ERROR
;
102 theCalendar
= Calendar::createInstance(status
);
103 if (U_FAILURE(status
))
107 errln("FAIL: Calendar::createInstance failed");
113 void CalendarTimeZoneTest::releaseCalendar(Calendar
* adopt
)
115 if(fgCalendar
== 0) // If the cache is empty we must add it back.
131 // Utility method for formatting dates for printing; useful for Java->C++ conversion.
132 // Tries to mimic the Java Date.toString() format.
134 CalendarTimeZoneTest::dateToString(UDate d
)
137 return dateToString(d
, str
);
141 CalendarTimeZoneTest::dateToString(UDate d
, UnicodeString
& str
)
144 DateFormat
* format
= getDateFormat();
147 str
+= "DATE_FORMAT_FAILURE";
150 format
->format(d
, str
);
151 releaseDateFormat(format
);
156 CalendarTimeZoneTest::dateToString(UDate d
, UnicodeString
& str
,
160 DateFormat
* format
= getDateFormat();
163 str
+= "DATE_FORMAT_FAILURE";
166 TimeZone
* save
= format
->getTimeZone().clone();
167 format
->setTimeZone(tz
);
168 format
->format(d
, str
);
169 format
->adoptTimeZone(save
);
170 releaseDateFormat(format
);
174 // Utility methods to create a date. This is useful for converting Java constructs
175 // which create a Date object.
177 CalendarTimeZoneTest::date(int32_t y
, int32_t m
, int32_t d
, int32_t hr
, int32_t min
, int32_t sec
)
179 Calendar
* cal
= getCalendar();
180 if (cal
== 0) return 0.0;
182 cal
->set(1900 + y
, m
, d
, hr
, min
, sec
); // Add 1900 to follow java.util.Date protocol
183 UErrorCode status
= U_ZERO_ERROR
;
184 UDate dt
= cal
->getTime(status
);
185 releaseCalendar(cal
);
186 if (U_FAILURE(status
))
188 errln("FAIL: Calendar::getTime failed");
194 // Utility methods to create a date. The returned Date is UTC rather than local.
196 CalendarTimeZoneTest::utcDate(int32_t y, int32_t m, int32_t d, int32_t hr, int32_t min, int32_t sec)
198 Calendar* cal = getCalendar();
199 if (cal == 0) return 0.0;
200 UErrorCode status = U_ZERO_ERROR;
201 Date dt = date(y, m, d, hr, min, sec) +
202 cal->get(Calendar::ZONE_OFFSET, status) -
203 cal->get(Calendar::DST_OFFSET, status);
204 releaseCalendar(cal);
205 if (U_FAILURE(status))
207 errln("FAIL: Calendar::get failed");
213 // Mimics Date.getYear() etc.
215 CalendarTimeZoneTest::dateToFields(UDate date
, int32_t& y
, int32_t& m
, int32_t& d
, int32_t& hr
, int32_t& min
, int32_t& sec
)
217 Calendar
* cal
= getCalendar();
218 if (cal
== 0) return;
219 UErrorCode status
= U_ZERO_ERROR
;
220 cal
->setTime(date
, status
);
221 y
= cal
->get(UCAL_YEAR
, status
) - 1900;
222 m
= cal
->get(UCAL_MONTH
, status
);
223 d
= cal
->get(UCAL_DATE
, status
);
224 hr
= cal
->get(UCAL_HOUR_OF_DAY
, status
);
225 min
= cal
->get(UCAL_MINUTE
, status
);
226 sec
= cal
->get(UCAL_SECOND
, status
);
227 releaseCalendar(cal
);
230 void CalendarTimeZoneTest::cleanup()
238 #endif /* #if !UCONFIG_NO_FORMATTING */