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