1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /*
7 * File CALTZTST.H
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 08/06/97 aliu Creation.
13 ********************************************************************************
14 */
15
16 #include "unicode/utypes.h"
17
18 #if !UCONFIG_NO_FORMATTING
19
20 #include "caltztst.h"
21 #include "unicode/smpdtfmt.h"
22 #include "mutex.h"
23
24 DateFormat* CalendarTimeZoneTest::fgDateFormat = 0;
25 Calendar* CalendarTimeZoneTest::fgCalendar = 0;
26
failure(UErrorCode status,const char * msg)27 UBool CalendarTimeZoneTest::failure(UErrorCode status, const char* msg)
28 {
29 if (U_FAILURE(status))
30 {
31 errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
32 return TRUE;
33 }
34 return FALSE;
35 }
36
getDateFormat()37 DateFormat* CalendarTimeZoneTest::getDateFormat()
38 {
39 DateFormat *theFormat = 0;
40
41 if (fgDateFormat != 0) // if there's something in the cache
42 {
43 Mutex lock;
44
45 if (fgDateFormat != 0) // Someone might have grabbed it.
46 {
47 theFormat = fgDateFormat;
48 fgDateFormat = 0; // We have exclusive right to this formatter.
49 }
50 }
51
52 if(theFormat == 0) // If we weren't able to pull it out of the cache, then we have to create it.
53 {
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))
57 {
58 delete theFormat;
59 theFormat = 0;
60 errln("FAIL: Could not create SimpleDateFormat");
61 }
62 }
63
64 return theFormat;
65 }
66
releaseDateFormat(DateFormat * adopt)67 void CalendarTimeZoneTest::releaseDateFormat(DateFormat *adopt)
68 {
69 if(fgDateFormat == 0) // If the cache is empty we must add it back.
70 {
71 Mutex lock;
72
73 if(fgDateFormat == 0)
74 {
75 fgDateFormat = adopt;
76 adopt = 0;
77 }
78 }
79 else {
80 delete adopt;
81 }
82 }
83
getCalendar()84 Calendar* CalendarTimeZoneTest::getCalendar()
85 {
86 Calendar *theCalendar = 0;
87
88 if (fgCalendar != 0) // if there's something in the cache
89 {
90 Mutex lock;
91
92 if (fgCalendar != 0) // Someone might have grabbed it.
93 {
94 theCalendar = fgCalendar;
95 fgCalendar = 0; // We have exclusive right to this calendar.
96 }
97 }
98
99 if(theCalendar == 0) // If we weren't able to pull it out of the cache, then we have to create it.
100 {
101 UErrorCode status = U_ZERO_ERROR;
102 theCalendar = Calendar::createInstance(status);
103 if (U_FAILURE(status))
104 {
105 delete theCalendar;
106 theCalendar = 0;
107 errln("FAIL: Calendar::createInstance failed");
108 }
109 }
110 return theCalendar;
111 }
112
releaseCalendar(Calendar * adopt)113 void CalendarTimeZoneTest::releaseCalendar(Calendar* adopt)
114 {
115 if(fgCalendar == 0) // If the cache is empty we must add it back.
116 {
117 Mutex lock;
118
119 if(fgCalendar == 0)
120 {
121 fgCalendar = adopt;
122 adopt = 0;
123 }
124 }
125 else
126 {
127 delete adopt;
128 }
129 }
130
131 // Utility method for formatting dates for printing; useful for Java->C++ conversion.
132 // Tries to mimic the Java Date.toString() format.
133 UnicodeString
dateToString(UDate d)134 CalendarTimeZoneTest::dateToString(UDate d)
135 {
136 UnicodeString str;
137 return dateToString(d, str);
138 }
139
140 UnicodeString&
dateToString(UDate d,UnicodeString & str)141 CalendarTimeZoneTest::dateToString(UDate d, UnicodeString& str)
142 {
143 str.remove();
144 DateFormat* format = getDateFormat();
145 if (format == 0)
146 {
147 str += "DATE_FORMAT_FAILURE";
148 return str;
149 }
150 format->format(d, str);
151 releaseDateFormat(format);
152 return str;
153 }
154
155 UnicodeString&
dateToString(UDate d,UnicodeString & str,const TimeZone & tz)156 CalendarTimeZoneTest::dateToString(UDate d, UnicodeString& str,
157 const TimeZone& tz)
158 {
159 str.remove();
160 DateFormat* format = getDateFormat();
161 if (format == 0)
162 {
163 str += "DATE_FORMAT_FAILURE";
164 return str;
165 }
166 TimeZone* save = format->getTimeZone().clone();
167 format->setTimeZone(tz);
168 format->format(d, str);
169 format->adoptTimeZone(save);
170 releaseDateFormat(format);
171 return str;
172 }
173
174 // Utility methods to create a date. This is useful for converting Java constructs
175 // which create a Date object.
176 UDate
date(int32_t y,int32_t m,int32_t d,int32_t hr,int32_t min,int32_t sec)177 CalendarTimeZoneTest::date(int32_t y, int32_t m, int32_t d, int32_t hr, int32_t min, int32_t sec)
178 {
179 Calendar* cal = getCalendar();
180 if (cal == 0) return 0.0;
181 cal->clear();
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))
187 {
188 errln("FAIL: Calendar::getTime failed");
189 return 0.0;
190 }
191 return dt;
192 }
193
194 // Utility methods to create a date. The returned Date is UTC rather than local.
195 /*Date
196 CalendarTimeZoneTest::utcDate(int32_t y, int32_t m, int32_t d, int32_t hr, int32_t min, int32_t sec)
197 {
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))
206 {
207 errln("FAIL: Calendar::get failed");
208 return 0.0;
209 }
210 return dt;
211 }*/
212
213 // Mimics Date.getYear() etc.
214 void
dateToFields(UDate date,int32_t & y,int32_t & m,int32_t & d,int32_t & hr,int32_t & min,int32_t & sec)215 CalendarTimeZoneTest::dateToFields(UDate date, int32_t& y, int32_t& m, int32_t& d, int32_t& hr, int32_t& min, int32_t& sec)
216 {
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);
228 }
229
cleanup()230 void CalendarTimeZoneTest::cleanup()
231 {
232 delete fgDateFormat;
233 fgDateFormat = 0;
234 delete fgCalendar;
235 fgCalendar = 0;
236 }
237
238 #endif /* #if !UCONFIG_NO_FORMATTING */
239
240 //eof
241