int32_t RuleBasedTimeZone::getRawOffset(void) const { // Note: This implementation returns standard GMT offset // as of current time. UErrorCode status = U_ZERO_ERROR; int32_t raw, dst; getOffset(uprv_getUTCtime() * U_MILLIS_PER_SECOND, FALSE, raw, dst, status); return raw; }
U_CDECL_END /** * Function used for removing unreferrenced cache entries exceeding * the expiration time. This function must be called with in the mutex * block. */ static void sweepCache() { int32_t pos = UHASH_FIRST; const UHashElement* elem; double now = (double)uprv_getUTCtime(); while ((elem = uhash_nextElement(gTZGNCoreCache, &pos))) { TZGNCoreRef *entry = (TZGNCoreRef *)elem->value.pointer; if (entry->refCount <= 0 && (now - entry->lastAccess) > CACHE_EXPIRATION) { // delete this entry uhash_removeElement(gTZGNCoreCache, elem); } } }
UBool RuleBasedTimeZone::useDaylightTime(void) const { // Note: This implementation returns true when // daylight saving time is used as of now or // after the next transition. UErrorCode status = U_ZERO_ERROR; UDate now = uprv_getUTCtime() * U_MILLIS_PER_SECOND; int32_t raw, dst; getOffset(now, FALSE, raw, dst, status); if (dst != 0) { return TRUE; } // If DST is not used now, check if DST is used after the next transition UDate time; TimeZoneRule *from, *to; UBool avail = findNext(now, FALSE, time, from, to); if (avail && to->getDSTSavings() != 0) { return TRUE; } return FALSE; }
UDate Calendar::getNow() { return (UDate)uprv_getUTCtime() * U_MILLIS_PER_SECOND; // return as milliseconds }
TimeZoneGenericNames* TimeZoneGenericNames::createInstance(const Locale& locale, UErrorCode& status) { if (U_FAILURE(status)) { return NULL; } TimeZoneGenericNames* instance = new TimeZoneGenericNames(); if (instance == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return NULL; } TZGNCoreRef *cacheEntry = NULL; { Mutex lock(&gTZGNLock); if (!gTZGNCoreCacheInitialized) { // Create empty hashtable gTZGNCoreCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status); if (U_SUCCESS(status)) { uhash_setKeyDeleter(gTZGNCoreCache, uprv_free); uhash_setValueDeleter(gTZGNCoreCache, deleteTZGNCoreRef); gTZGNCoreCacheInitialized = TRUE; ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONEGENERICNAMES, tzgnCore_cleanup); } } if (U_FAILURE(status)) { return NULL; } // Check the cache, if not available, create new one and cache const char *key = locale.getName(); cacheEntry = (TZGNCoreRef *)uhash_get(gTZGNCoreCache, key); if (cacheEntry == NULL) { TZGNCore *tzgnCore = NULL; char *newKey = NULL; tzgnCore = new TZGNCore(locale, status); if (tzgnCore == NULL) { status = U_MEMORY_ALLOCATION_ERROR; } if (U_SUCCESS(status)) { newKey = (char *)uprv_malloc(uprv_strlen(key) + 1); if (newKey == NULL) { status = U_MEMORY_ALLOCATION_ERROR; } else { uprv_strcpy(newKey, key); } } if (U_SUCCESS(status)) { cacheEntry = (TZGNCoreRef *)uprv_malloc(sizeof(TZGNCoreRef)); if (cacheEntry == NULL) { status = U_MEMORY_ALLOCATION_ERROR; } else { cacheEntry->obj = tzgnCore; cacheEntry->refCount = 1; cacheEntry->lastAccess = (double)uprv_getUTCtime(); uhash_put(gTZGNCoreCache, newKey, cacheEntry, &status); } } if (U_FAILURE(status)) { if (tzgnCore != NULL) { delete tzgnCore; } if (newKey != NULL) { uprv_free(newKey); } if (cacheEntry != NULL) { uprv_free(cacheEntry); } cacheEntry = NULL; } } else { // Update the reference count cacheEntry->refCount++; cacheEntry->lastAccess = (double)uprv_getUTCtime(); } gAccessCount++; if (gAccessCount >= SWEEP_INTERVAL) { // sweep sweepCache(); gAccessCount = 0; } } // End of mutex locked block if (cacheEntry == NULL) { delete instance; return NULL; } instance->fRef = cacheEntry; return instance; }
void cmd_millis() { printf("Milliseconds since Epoch: %.0f\n", uprv_getUTCtime()); }
U_CDECL_END int main(int argc, char* argv[]) { int32_t nerrors = 0; TestNode *root = NULL; UErrorCode errorCode = U_ZERO_ERROR; UDate startTime, endTime; int32_t diffTime; startTime = uprv_getUTCtime(); /* Check whether ICU will initialize without forcing the build data directory into * the ICU_DATA path. Success here means either the data dll contains data, or that * this test program was run with ICU_DATA set externally. Failure of this check * is normal when ICU data is not packaged into a shared library. * * Whether or not this test succeeds, we want to cleanup and reinitialize * with a data path so that data loading from individual files can be tested. */ u_init(&errorCode); if (U_FAILURE(errorCode)) { fprintf(stderr, "#### Note: ICU Init without build-specific setDataDirectory() failed.\n"); } u_cleanup(); errorCode = U_ZERO_ERROR; if (!initArgs(argc, argv, argHandler, (void *) &STANDARD_TEST_FILE)) { /* Error already displayed. */ return -1; } /* Initialize ICU */ ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */ u_init(&errorCode); if (U_FAILURE(errorCode)) { fprintf(stderr, "#### ERROR! %s: u_init() failed with status = \"%s\".\n" "*** Check the ICU_DATA environment variable and \n" "*** check that the data files are present.\n", argv[0], u_errorName(errorCode)); return 1; } fprintf(stdout, "Default charset for this run is %s\n", ucnv_getDefaultName()); addAllTests(&root); nerrors = runTestRequest(root, argc, argv); #if 1 { FILE* fileToRemove = fopen(STANDARD_TEST_FILE, "r"); /* This should delete any temporary files. */ if (fileToRemove) { fclose(fileToRemove); if (remove(STANDARD_TEST_FILE) != 0) { /* Maybe someone didn't close the file correctly. */ fprintf(stderr, "FAIL: Could not delete %s\n", STANDARD_TEST_FILE); nerrors += 1; } } } #endif cleanUpTestTree(root); DataDrivenLogger::cleanUp(); u_cleanup(); endTime = uprv_getUTCtime(); diffTime = (int32_t)(endTime - startTime); printf("Elapsed Time: %02d:%02d:%02d.%03d\n", (int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR), (int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE), (int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND), (int)(diffTime%U_MILLIS_PER_SECOND)); return nerrors; }
static UBool getSystemTimeInformation(TimeZone *tz, SYSTEMTIME &daylightDate, SYSTEMTIME &standardDate, int32_t &bias, int32_t &daylightBias, int32_t &standardBias) { UErrorCode status = U_ZERO_ERROR; UBool result = TRUE; BasicTimeZone *btz = (BasicTimeZone*)tz; // we should check type InitialTimeZoneRule *initial = NULL; AnnualTimeZoneRule *std = NULL, *dst = NULL; btz->getSimpleRulesNear(uprv_getUTCtime(), initial, std, dst, status); if (U_SUCCESS(status)) { if (std == NULL || dst == NULL) { bias = -1 * (initial->getRawOffset()/60000); daylightBias = 0; // Do not use DST. Set 0 to all stadardDate/daylightDate fields standardDate.wYear = standardDate.wMonth = standardDate.wDayOfWeek = standardDate.wDay = standardDate.wHour = standardDate.wMinute = standardDate.wSecond = standardDate.wMilliseconds = 0; daylightDate.wYear = daylightDate.wMonth = daylightDate.wDayOfWeek = daylightDate.wDay = daylightDate.wHour = daylightDate.wMinute = daylightDate.wSecond = daylightDate.wMilliseconds = 0; } else { U_ASSERT(std->getRule()->getDateRuleType() == DateTimeRule::DOW); U_ASSERT(dst->getRule()->getDateRuleType() == DateTimeRule::DOW); bias = -1 * (std->getRawOffset()/60000); daylightBias = -1 * (dst->getDSTSavings()/60000); // Always use DOW type rule int32_t hour, min, sec, mil; standardDate.wYear = 0; standardDate.wMonth = std->getRule()->getRuleMonth() + 1; standardDate.wDay = std->getRule()->getRuleWeekInMonth(); if (standardDate.wDay < 0) { standardDate.wDay = 5; } standardDate.wDayOfWeek = std->getRule()->getRuleDayOfWeek() - 1; mil = std->getRule()->getRuleMillisInDay(); hour = mil/3600000; mil %= 3600000; min = mil/60000; mil %= 60000; sec = mil/1000; mil %= 1000; standardDate.wHour = hour; standardDate.wMinute = min; standardDate.wSecond = sec; standardDate.wMilliseconds = mil; daylightDate.wYear = 0; daylightDate.wMonth = dst->getRule()->getRuleMonth() + 1; daylightDate.wDay = dst->getRule()->getRuleWeekInMonth(); if (daylightDate.wDay < 0) { daylightDate.wDay = 5; } daylightDate.wDayOfWeek = dst->getRule()->getRuleDayOfWeek() - 1; mil = dst->getRule()->getRuleMillisInDay(); hour = mil/3600000; mil %= 3600000; min = mil/60000; mil %= 60000; sec = mil/1000; mil %= 1000; daylightDate.wHour = hour; daylightDate.wMinute = min; daylightDate.wSecond = sec; daylightDate.wMilliseconds = mil; } } else { result = FALSE; } delete initial; delete std; delete dst; return result; }
static void TestPUtilAPI(void){ double n1=0.0, y1=0.0, expn1, expy1; double value1 = 0.021; UVersionInfo versionArray = {0x01, 0x00, 0x02, 0x02}; char versionString[17]; /* xxx.xxx.xxx.xxx\0 */ char *str=0; UBool isTrue=FALSE; log_verbose("Testing the API uprv_modf()\n"); y1 = uprv_modf(value1, &n1); expn1=0; expy1=0.021; if(y1 != expy1 || n1 != expn1){ log_err("Error in uprv_modf. Expected IntegralValue=%f, Got=%f, \n Expected FractionalValue=%f, Got=%f\n", expn1, n1, expy1, y1); } if(VERBOSITY){ log_verbose("[float] x = %f n = %f y = %f\n", value1, n1, y1); } log_verbose("Testing the API uprv_fmod()\n"); expn1=uprv_fmod(30.50, 15.00); doAssert(expn1, 0.5, "uprv_fmod(30.50, 15.00) failed."); log_verbose("Testing the API uprv_ceil()\n"); expn1=uprv_ceil(value1); doAssert(expn1, 1, "uprv_ceil(0.021) failed."); log_verbose("Testing the API uprv_floor()\n"); expn1=uprv_floor(value1); doAssert(expn1, 0, "uprv_floor(0.021) failed."); log_verbose("Testing the API uprv_fabs()\n"); expn1=uprv_fabs((2.02-1.345)); doAssert(expn1, 0.675, "uprv_fabs(2.02-1.345) failed."); log_verbose("Testing the API uprv_fmax()\n"); doAssert(uprv_fmax(2.4, 1.2), 2.4, "uprv_fmax(2.4, 1.2) failed."); log_verbose("Testing the API uprv_fmax() with x value= NaN\n"); expn1=uprv_fmax(uprv_getNaN(), 1.2); doAssert(expn1, uprv_getNaN(), "uprv_fmax(uprv_getNaN(), 1.2) failed. when one parameter is NaN"); log_verbose("Testing the API uprv_fmin()\n"); doAssert(uprv_fmin(2.4, 1.2), 1.2, "uprv_fmin(2.4, 1.2) failed."); log_verbose("Testing the API uprv_fmin() with x value= NaN\n"); expn1=uprv_fmin(uprv_getNaN(), 1.2); doAssert(expn1, uprv_getNaN(), "uprv_fmin(uprv_getNaN(), 1.2) failed. when one parameter is NaN"); log_verbose("Testing the API uprv_max()\n"); doAssert(uprv_max(4, 2), 4, "uprv_max(4, 2) failed."); log_verbose("Testing the API uprv_min()\n"); doAssert(uprv_min(-4, 2), -4, "uprv_min(-4, 2) failed."); log_verbose("Testing the API uprv_trunc()\n"); doAssert(uprv_trunc(12.3456), 12, "uprv_trunc(12.3456) failed."); doAssert(uprv_trunc(12.234E2), 1223, "uprv_trunc(12.234E2) failed."); doAssert(uprv_trunc(uprv_getNaN()), uprv_getNaN(), "uprv_trunc(uprv_getNaN()) failed. with parameter=NaN"); doAssert(uprv_trunc(uprv_getInfinity()), uprv_getInfinity(), "uprv_trunc(uprv_getInfinity()) failed. with parameter=Infinity"); log_verbose("Testing the API uprv_pow10()\n"); doAssert(uprv_pow10(4), 10000, "uprv_pow10(4) failed."); log_verbose("Testing the API uprv_log10()\n"); doAssert(uprv_log10(3456), 3, "uprv_log10(3456) failed."); #ifdef OS390 doAssert(uprv_log10(1.0e55), 55, "uprv_log10(1.0e55) failed."); #else doAssert(uprv_log10(1.0e300), 300, "uprv_log10(1.0e300) failed."); #endif log_verbose("Testing the API uprv_isNegativeInfinity()\n"); isTrue=uprv_isNegativeInfinity(uprv_getInfinity() * -1); if(isTrue != TRUE){ log_err("ERROR: uprv_isNegativeInfinity failed.\n"); } log_verbose("Testing the API uprv_isPositiveInfinity()\n"); isTrue=uprv_isPositiveInfinity(uprv_getInfinity()); if(isTrue != TRUE){ log_err("ERROR: uprv_isPositiveInfinity failed.\n"); } log_verbose("Testing the API uprv_isInfinite()\n"); isTrue=uprv_isInfinite(uprv_getInfinity()); if(isTrue != TRUE){ log_err("ERROR: uprv_isInfinite failed.\n"); } #if 0 log_verbose("Testing the API uprv_digitsAfterDecimal()....\n"); doAssert(uprv_digitsAfterDecimal(value1), 3, "uprv_digitsAfterDecimal() failed."); doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed."); doAssert(uprv_digitsAfterDecimal(1.2345E-2), 6, "uprv_digitsAfterDecimal(1.2345E-2) failed."); doAssert(uprv_digitsAfterDecimal(1.2345E2), 2, "uprv_digitsAfterDecimal(1.2345E2) failed."); doAssert(uprv_digitsAfterDecimal(-1.2345E-20), 24, "uprv_digitsAfterDecimal(1.2345E-20) failed."); doAssert(uprv_digitsAfterDecimal(1.2345E20), 0, "uprv_digitsAfterDecimal(1.2345E20) failed."); doAssert(uprv_digitsAfterDecimal(-0.021), 3, "uprv_digitsAfterDecimal(-0.021) failed."); doAssert(uprv_digitsAfterDecimal(23.0), 0, "uprv_digitsAfterDecimal(23.0) failed."); doAssert(uprv_digitsAfterDecimal(0.022223333321), 9, "uprv_digitsAfterDecimal(0.022223333321) failed."); #endif log_verbose("Testing the API u_versionToString().....\n"); u_versionToString(versionArray, versionString); if(strcmp(versionString, "1.0.2.2") != 0){ log_err("ERROR: u_versionToString() failed. Expected: 1.0.2.2, Got=%s\n", versionString); } log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n"); u_versionToString(NULL, versionString); if(strcmp(versionString, "") != 0){ log_err("ERROR: u_versionToString() failed. with versionArray=NULL. It should just return\n"); } log_verbose("Testing the API u_versionToString().....with versionArray=NULL\n"); u_versionToString(NULL, versionString); if(strcmp(versionString, "") != 0){ log_err("ERROR: u_versionToString() failed . It should just return\n"); } log_verbose("Testing the API u_versionToString().....with versionString=NULL\n"); u_versionToString(versionArray, NULL); if(strcmp(versionString, "") != 0){ log_err("ERROR: u_versionToString() failed. with versionArray=NULL It should just return\n"); } versionArray[0] = 0x0a; log_verbose("Testing the API u_versionToString().....\n"); u_versionToString(versionArray, versionString); if(strcmp(versionString, "10.0.2.2") != 0){ log_err("ERROR: u_versionToString() failed. Expected: 10.0.2.2, Got=%s\n", versionString); } versionArray[0] = 0xa0; u_versionToString(versionArray, versionString); if(strcmp(versionString, "160.0.2.2") != 0){ log_err("ERROR: u_versionToString() failed. Expected: 160.0.2.2, Got=%s\n", versionString); } versionArray[0] = 0xa0; versionArray[1] = 0xa0; u_versionToString(versionArray, versionString); if(strcmp(versionString, "160.160.2.2") != 0){ log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString); } versionArray[0] = 0x01; versionArray[1] = 0x0a; u_versionToString(versionArray, versionString); if(strcmp(versionString, "1.10.2.2") != 0){ log_err("ERROR: u_versionToString() failed. Expected: 160.160.2.2, Got=%s\n", versionString); } log_verbose("Testing the API u_versionFromString() ....\n"); u_versionFromString(versionArray, "1.3.5.6"); u_versionToString(versionArray, versionString); if(strcmp(versionString, "1.3.5.6") != 0){ log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString); } log_verbose("Testing the API u_versionFromString() where versionArray=NULL....\n"); u_versionFromString(NULL, "1.3.5.6"); u_versionToString(versionArray, versionString); if(strcmp(versionString, "1.3.5.6") != 0){ log_err("ERROR: u_getVersion() failed. Expected: 1.3.5.6, Got=%s\n", versionString); } log_verbose("Testing the API u_getVersion().....\n"); u_getVersion(versionArray); u_versionToString(versionArray, versionString); if(strcmp(versionString, U_ICU_VERSION) != 0){ log_err("ERROR: u_getVersion() failed. Got=%s, expected %s\n", versionString, U_ICU_VERSION); } log_verbose("Testing the API u_errorName()...\n"); str=(char*)u_errorName((UErrorCode)0); if(strcmp(str, "U_ZERO_ERROR") != 0){ log_err("ERROR: u_getVersion() failed. Expected: U_ZERO_ERROR Got=%s\n", str); } log_verbose("Testing the API u_errorName()...\n"); str=(char*)u_errorName((UErrorCode)-127); if(strcmp(str, "U_USING_DEFAULT_WARNING") != 0){ log_err("ERROR: u_getVersion() failed. Expected: U_USING_DEFAULT_WARNING Got=%s\n", str); } log_verbose("Testing the API u_errorName().. with BOGUS ERRORCODE...\n"); str=(char*)u_errorName((UErrorCode)200); if(strcmp(str, "[BOGUS UErrorCode]") != 0){ log_err("ERROR: u_getVersion() failed. Expected: [BOGUS UErrorCode] Got=%s\n", str); } { const char* dataDirectory; UChar *udataDir=0; UChar temp[100]; char *charvalue=0; log_verbose("Testing chars to UChars\n"); /* This cannot really work on a japanese system. u_uastrcpy will have different results than */ /* u_charsToUChars when there is a backslash in the string! */ /*dataDirectory=u_getDataDirectory();*/ dataDirectory="directory1"; /*no backslashes*/ udataDir=(UChar*)malloc(sizeof(UChar) * (strlen(dataDirectory) + 1)); u_charsToUChars(dataDirectory, udataDir, (strlen(dataDirectory)+1)); u_uastrcpy(temp, dataDirectory); if(u_strcmp(temp, udataDir) != 0){ log_err("ERROR: u_charsToUChars failed. Expected %s, Got %s\n", austrdup(temp), austrdup(udataDir)); } log_verbose("Testing UChars to chars\n"); charvalue=(char*)malloc(sizeof(char) * (u_strlen(udataDir) + 1)); u_UCharsToChars(udataDir, charvalue, (u_strlen(udataDir)+1)); if(strcmp(charvalue, dataDirectory) != 0){ log_err("ERROR: u_UCharsToChars failed. Expected %s, Got %s\n", charvalue, dataDirectory); } free(charvalue); free(udataDir); } log_verbose("Testing uprv_timezone()....\n"); { int32_t tzoffset = uprv_timezone(); log_verbose("Value returned from uprv_timezone = %d\n", tzoffset); if (tzoffset != 28800) { log_verbose("***** WARNING: If testing in the PST timezone, t_timezone should return 28800! *****"); } if ((tzoffset % 1800 != 0)) { log_err("FAIL: t_timezone may be incorrect. It is not a multiple of 30min."); } tzoffset=uprv_getUTCtime(); } }
TimeZoneNamesDelegate::TimeZoneNamesDelegate(const Locale& locale, UErrorCode& status) { Mutex lock(&gTimeZoneNamesLock); if (!gTimeZoneNamesCacheInitialized) { // Create empty hashtable if it is not already initialized. gTimeZoneNamesCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status); if (U_SUCCESS(status)) { uhash_setKeyDeleter(gTimeZoneNamesCache, uprv_free); uhash_setValueDeleter(gTimeZoneNamesCache, deleteTimeZoneNamesCacheEntry); gTimeZoneNamesCacheInitialized = TRUE; ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONENAMES, timeZoneNames_cleanup); } } if (U_FAILURE(status)) { return; } // Check the cache, if not available, create new one and cache TimeZoneNamesCacheEntry *cacheEntry = NULL; const char *key = locale.getName(); cacheEntry = (TimeZoneNamesCacheEntry *)uhash_get(gTimeZoneNamesCache, key); if (cacheEntry == NULL) { TimeZoneNames *tznames = NULL; char *newKey = NULL; tznames = new TimeZoneNamesImpl(locale, status); if (tznames == NULL) { status = U_MEMORY_ALLOCATION_ERROR; } if (U_SUCCESS(status)) { newKey = (char *)uprv_malloc(uprv_strlen(key) + 1); if (newKey == NULL) { status = U_MEMORY_ALLOCATION_ERROR; } else { uprv_strcpy(newKey, key); } } if (U_SUCCESS(status)) { cacheEntry = (TimeZoneNamesCacheEntry *)uprv_malloc(sizeof(TimeZoneNamesCacheEntry)); if (cacheEntry == NULL) { status = U_MEMORY_ALLOCATION_ERROR; } else { cacheEntry->names = tznames; cacheEntry->refCount = 1; cacheEntry->lastAccess = (double)uprv_getUTCtime(); uhash_put(gTimeZoneNamesCache, newKey, cacheEntry, &status); } } if (U_FAILURE(status)) { if (tznames != NULL) { delete tznames; } if (newKey != NULL) { uprv_free(newKey); } if (cacheEntry != NULL) { uprv_free(cacheEntry); } cacheEntry = NULL; } } else { // Update the reference count cacheEntry->refCount++; cacheEntry->lastAccess = (double)uprv_getUTCtime(); } gAccessCount++; if (gAccessCount >= SWEEP_INTERVAL) { // sweep sweepCache(); gAccessCount = 0; } fTZnamesCacheEntry = cacheEntry; }