mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-09 07:22:11 +00:00
ICU-5211 the Universal Time Scale must use the proleptic Gregorian calendar because it is supposed to be compatible with .Net System.DateTime which uses that calendar
X-SVN-Rev: 19778
This commit is contained in:
parent
27c7ccf73b
commit
f049a5f607
2 changed files with 155 additions and 10 deletions
|
@ -19,17 +19,18 @@
|
|||
#define hours (minutes * 60)
|
||||
#define days (hours * 24)
|
||||
|
||||
/* Constants generated by ICU4J com.ibm.icu.dev.tool.timescale.GenerateCTimeScaleData. */
|
||||
static const int64_t timeScaleTable[UDTS_MAX_SCALE][UTSV_MAX_SCALE_VALUE] = {
|
||||
/* units epochOffset fromMin fromMax toMin toMax epochOffsetP1 epochOffsetM1 unitsRound minRound maxRound */
|
||||
{milliseconds, INT64_C(62135769600000), INT64_C(-984472973285477), INT64_C(860201434085477), INT64_C(-9223372036854774999), INT64_C(9223372036854774999), INT64_C(62135769600001), INT64_C(62135769599999), INT64_C(5000), INT64_C(-9223372036854770808), INT64_C(9223372036854770807)},
|
||||
{seconds, INT64_C(62135769600), INT64_C(-984472973285), INT64_C(860201434085), U_INT64_MIN, INT64_C(9223372036854775807), INT64_C(62135769601), INT64_C(62135769599), INT64_C(5000000), INT64_C(-9223372036849775808), INT64_C(9223372036849775807)},
|
||||
{milliseconds, INT64_C(62135769600000), INT64_C(-984472973285477), INT64_C(860201434085477), INT64_C(-9223372036854774999), INT64_C(9223372036854774999), INT64_C(62135769600001), INT64_C(62135769599999), INT64_C(5000), INT64_C(-9223372036854770808), INT64_C(9223372036854770807)},
|
||||
{ticks, INT64_C(504912960000000000), U_INT64_MIN, INT64_C(8718459076854775807), INT64_C(-8718459076854775808), INT64_C(9223372036854775807), INT64_C(504912960000000000), INT64_C(504912960000000000), INT64_C(0), U_INT64_MIN, INT64_C(9223372036854775807)},
|
||||
{ticks, INT64_C(0), U_INT64_MIN, INT64_C(9223372036854775807), U_INT64_MIN, INT64_C(9223372036854775807), INT64_C(0), INT64_C(0), INT64_C(0), U_INT64_MIN, INT64_C(9223372036854775807)},
|
||||
{seconds, INT64_C(60052924800), INT64_C(-982390128485), INT64_C(862284278885), U_INT64_MIN, INT64_C(9223372036854775807), INT64_C(60052924801), INT64_C(60052924799), INT64_C(5000000), INT64_C(-9223372036849775808), INT64_C(9223372036849775807)},
|
||||
{seconds, INT64_C(63114076800), INT64_C(-985451280485), INT64_C(859223126885), U_INT64_MIN, INT64_C(9223372036854775807), INT64_C(63114076801), INT64_C(63114076799), INT64_C(5000000), INT64_C(-9223372036849775808), INT64_C(9223372036849775807)},
|
||||
{days, INT64_C(693596), INT64_C(-11368795), INT64_C(9981603), U_INT64_MIN, INT64_C(9223372036854775807), INT64_C(693597), INT64_C(693595), INT64_C(432000000000), INT64_C(-9223371604854775808), INT64_C(9223371604854775807)},
|
||||
{days, INT64_C(693596), INT64_C(-11368795), INT64_C(9981603), U_INT64_MIN, INT64_C(9223372036854775807), INT64_C(693597), INT64_C(693595), INT64_C(432000000000), INT64_C(-9223371604854775808), INT64_C(9223371604854775807)},
|
||||
{milliseconds, INT64_C(62135596800000), INT64_C(-984472800485477), INT64_C(860201606885477), INT64_C(-9223372036854774999), INT64_C(9223372036854774999), INT64_C(62135596800001), INT64_C(62135596799999), INT64_C(5000), INT64_C(-9223372036854770808), INT64_C(9223372036854770807)},
|
||||
{seconds, INT64_C(62135596800), INT64_C(-984472800485), INT64_C(860201606885), U_INT64_MIN, U_INT64_MAX, INT64_C(62135596801), INT64_C(62135596799), INT64_C(5000000), INT64_C(-9223372036849775808), INT64_C(9223372036849775807)},
|
||||
{milliseconds, INT64_C(62135596800000), INT64_C(-984472800485477), INT64_C(860201606885477), INT64_C(-9223372036854774999), INT64_C(9223372036854774999), INT64_C(62135596800001), INT64_C(62135596799999), INT64_C(5000), INT64_C(-9223372036854770808), INT64_C(9223372036854770807)},
|
||||
{ticks, INT64_C(504911232000000000), U_INT64_MIN, INT64_C(8718460804854775807), INT64_C(-8718460804854775808), U_INT64_MAX, INT64_C(504911232000000000), INT64_C(504911232000000000), INT64_C(0), U_INT64_MIN, U_INT64_MAX},
|
||||
{ticks, INT64_C(0), U_INT64_MIN, U_INT64_MAX, U_INT64_MIN, U_INT64_MAX, INT64_C(0), INT64_C(0), INT64_C(0), U_INT64_MIN, U_INT64_MAX},
|
||||
{seconds, INT64_C(60052752000), INT64_C(-982389955685), INT64_C(862284451685), U_INT64_MIN, U_INT64_MAX, INT64_C(60052752001), INT64_C(60052751999), INT64_C(5000000), INT64_C(-9223372036849775808), INT64_C(9223372036849775807)},
|
||||
{seconds, INT64_C(63113904000), INT64_C(-985451107685), INT64_C(859223299685), U_INT64_MIN, U_INT64_MAX, INT64_C(63113904001), INT64_C(63113903999), INT64_C(5000000), INT64_C(-9223372036849775808), INT64_C(9223372036849775807)},
|
||||
{days, INT64_C(693594), INT64_C(-11368793), INT64_C(9981605), U_INT64_MIN, U_INT64_MAX, INT64_C(693595), INT64_C(693593), INT64_C(432000000000), INT64_C(-9223371604854775808), INT64_C(9223371604854775807)},
|
||||
{days, INT64_C(693594), INT64_C(-11368793), INT64_C(9981605), U_INT64_MIN, U_INT64_MAX, INT64_C(693595), INT64_C(693593), INT64_C(432000000000), INT64_C(-9223371604854775808), INT64_C(9223371604854775807)},
|
||||
};
|
||||
|
||||
U_CAPI int64_t U_EXPORT2
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
****************************************************************************
|
||||
* Copyright (c) 1997-2004, International Business Machines Corporation and *
|
||||
* Copyright (c) 1997-2006, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
****************************************************************************
|
||||
*/
|
||||
|
@ -10,17 +10,21 @@
|
|||
#if !UCONFIG_NO_FORMATTING
|
||||
|
||||
#include "unicode/utmscale.h"
|
||||
#include "unicode/ucal.h"
|
||||
|
||||
#include "cintltst.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
|
||||
|
||||
#define LOOP_COUNT 10000
|
||||
|
||||
static void TestAPI(void);
|
||||
static void TestData(void);
|
||||
static void TestMonkey(void);
|
||||
static void TestDotNet(void);
|
||||
|
||||
void addUtmsTest(TestNode** root);
|
||||
|
||||
|
@ -29,6 +33,7 @@ void addUtmsTest(TestNode** root)
|
|||
addTest(root, &TestAPI, "tsformat/utmstest/TestAPI");
|
||||
addTest(root, &TestData, "tsformat/utmstest/TestData");
|
||||
addTest(root, &TestMonkey, "tsformat/utmstest/TestMonkey");
|
||||
addTest(root, &TestDotNet, "tsformat/utmstest/TestDotNet");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -339,4 +344,143 @@ static void TestMonkey(void)
|
|||
}
|
||||
}
|
||||
|
||||
struct DotNetDateTimeTicks {
|
||||
int32_t year;
|
||||
int32_t month;
|
||||
int32_t day;
|
||||
int64_t ticks;
|
||||
};
|
||||
typedef struct DotNetDateTimeTicks DotNetDateTimeTicks;
|
||||
|
||||
/*
|
||||
* This data was generated by C++.Net code like
|
||||
* Console::WriteLine(L" {{ {0}, 1, 1, INT64_C({1}) }},", year, DateTime(year, 1, 1).Ticks);
|
||||
* with the DateTime constructor taking int values for year, month, and date.
|
||||
*/
|
||||
static const DotNetDateTimeTicks dotNetDateTimeTicks[]={
|
||||
/* year, month, day, ticks */
|
||||
{ 100, 1, 1, INT64_C(31241376000000000) },
|
||||
{ 100, 3, 1, INT64_C(31292352000000000) },
|
||||
{ 200, 1, 1, INT64_C(62798112000000000) },
|
||||
{ 200, 3, 1, INT64_C(62849088000000000) },
|
||||
{ 300, 1, 1, INT64_C(94354848000000000) },
|
||||
{ 300, 3, 1, INT64_C(94405824000000000) },
|
||||
{ 400, 1, 1, INT64_C(125911584000000000) },
|
||||
{ 400, 3, 1, INT64_C(125963424000000000) },
|
||||
{ 500, 1, 1, INT64_C(157469184000000000) },
|
||||
{ 500, 3, 1, INT64_C(157520160000000000) },
|
||||
{ 600, 1, 1, INT64_C(189025920000000000) },
|
||||
{ 600, 3, 1, INT64_C(189076896000000000) },
|
||||
{ 700, 1, 1, INT64_C(220582656000000000) },
|
||||
{ 700, 3, 1, INT64_C(220633632000000000) },
|
||||
{ 800, 1, 1, INT64_C(252139392000000000) },
|
||||
{ 800, 3, 1, INT64_C(252191232000000000) },
|
||||
{ 900, 1, 1, INT64_C(283696992000000000) },
|
||||
{ 900, 3, 1, INT64_C(283747968000000000) },
|
||||
{ 1000, 1, 1, INT64_C(315253728000000000) },
|
||||
{ 1000, 3, 1, INT64_C(315304704000000000) },
|
||||
{ 1100, 1, 1, INT64_C(346810464000000000) },
|
||||
{ 1100, 3, 1, INT64_C(346861440000000000) },
|
||||
{ 1200, 1, 1, INT64_C(378367200000000000) },
|
||||
{ 1200, 3, 1, INT64_C(378419040000000000) },
|
||||
{ 1300, 1, 1, INT64_C(409924800000000000) },
|
||||
{ 1300, 3, 1, INT64_C(409975776000000000) },
|
||||
{ 1400, 1, 1, INT64_C(441481536000000000) },
|
||||
{ 1400, 3, 1, INT64_C(441532512000000000) },
|
||||
{ 1500, 1, 1, INT64_C(473038272000000000) },
|
||||
{ 1500, 3, 1, INT64_C(473089248000000000) },
|
||||
{ 1600, 1, 1, INT64_C(504595008000000000) },
|
||||
{ 1600, 3, 1, INT64_C(504646848000000000) },
|
||||
{ 1700, 1, 1, INT64_C(536152608000000000) },
|
||||
{ 1700, 3, 1, INT64_C(536203584000000000) },
|
||||
{ 1800, 1, 1, INT64_C(567709344000000000) },
|
||||
{ 1800, 3, 1, INT64_C(567760320000000000) },
|
||||
{ 1900, 1, 1, INT64_C(599266080000000000) },
|
||||
{ 1900, 3, 1, INT64_C(599317056000000000) },
|
||||
{ 2000, 1, 1, INT64_C(630822816000000000) },
|
||||
{ 2000, 3, 1, INT64_C(630874656000000000) },
|
||||
{ 2100, 1, 1, INT64_C(662380416000000000) },
|
||||
{ 2100, 3, 1, INT64_C(662431392000000000) },
|
||||
{ 2200, 1, 1, INT64_C(693937152000000000) },
|
||||
{ 2200, 3, 1, INT64_C(693988128000000000) },
|
||||
{ 2300, 1, 1, INT64_C(725493888000000000) },
|
||||
{ 2300, 3, 1, INT64_C(725544864000000000) },
|
||||
{ 2400, 1, 1, INT64_C(757050624000000000) },
|
||||
{ 2400, 3, 1, INT64_C(757102464000000000) },
|
||||
{ 2500, 1, 1, INT64_C(788608224000000000) },
|
||||
{ 2500, 3, 1, INT64_C(788659200000000000) },
|
||||
{ 2600, 1, 1, INT64_C(820164960000000000) },
|
||||
{ 2600, 3, 1, INT64_C(820215936000000000) },
|
||||
{ 2700, 1, 1, INT64_C(851721696000000000) },
|
||||
{ 2700, 3, 1, INT64_C(851772672000000000) },
|
||||
{ 2800, 1, 1, INT64_C(883278432000000000) },
|
||||
{ 2800, 3, 1, INT64_C(883330272000000000) },
|
||||
{ 2900, 1, 1, INT64_C(914836032000000000) },
|
||||
{ 2900, 3, 1, INT64_C(914887008000000000) },
|
||||
{ 3000, 1, 1, INT64_C(946392768000000000) },
|
||||
{ 3000, 3, 1, INT64_C(946443744000000000) },
|
||||
{ 1, 1, 1, INT64_C(0) },
|
||||
{ 1601, 1, 1, INT64_C(504911232000000000) },
|
||||
{ 1899, 12, 31, INT64_C(599265216000000000) },
|
||||
{ 1904, 1, 1, INT64_C(600527520000000000) },
|
||||
{ 1970, 1, 1, INT64_C(621355968000000000) },
|
||||
{ 2001, 1, 1, INT64_C(631139040000000000) },
|
||||
{ 9900, 3, 1, INT64_C(3123873216000000000) },
|
||||
{ 9999, 12, 31, INT64_C(3155378112000000000) }
|
||||
};
|
||||
|
||||
/*
|
||||
* ICU's Universal Time Scale is designed to be tick-for-tick compatible with
|
||||
* .Net System.DateTime. Verify that this is so for the
|
||||
* .Net-supported date range (years 1-9999 AD).
|
||||
* This requires a proleptic Gregorian calendar because that's what .Net uses.
|
||||
* Proleptic: No Julian/Gregorian switchover, or a switchover before
|
||||
* any date that we test, that is, before 0001 AD.
|
||||
*/
|
||||
static void
|
||||
TestDotNet() {
|
||||
static const UChar utc[] = { 0x45, 0x74, 0x63, 0x2f, 0x47, 0x4d, 0x54, 0 }; /* "Etc/GMT" */
|
||||
const int32_t dayMillis = 86400 * INT64_C(1000); /* 1 day = 86400 seconds */
|
||||
const int64_t dayTicks = 86400 * INT64_C(10000000);
|
||||
const DotNetDateTimeTicks *dt;
|
||||
UCalendar *cal;
|
||||
UErrorCode errorCode;
|
||||
UDate icuDate;
|
||||
int64_t ticks, millis;
|
||||
int32_t i;
|
||||
|
||||
/* Open a proleptic Gregorian calendar. */
|
||||
errorCode = U_ZERO_ERROR;
|
||||
cal = ucal_open(utc, -1, "", UCAL_GREGORIAN, &errorCode);
|
||||
ucal_setGregorianChange(cal, -1000000 * (dayMillis * (UDate)1), &errorCode);
|
||||
if(U_FAILURE(errorCode)) {
|
||||
log_err("ucal_open(UTC/proleptic Gregorian) failed: %s\n", u_errorName(errorCode));
|
||||
ucal_close(cal);
|
||||
return;
|
||||
}
|
||||
for(i = 0; i < LENGTHOF(dotNetDateTimeTicks); ++i) {
|
||||
/* Test conversion from .Net/Universal time to ICU time. */
|
||||
dt = dotNetDateTimeTicks + i;
|
||||
millis = utmscale_toInt64(dt->ticks, UDTS_ICU4C_TIME, &errorCode);
|
||||
ucal_clear(cal);
|
||||
ucal_setDate(cal, dt->year, dt->month - 1, dt->day, &errorCode); /* Java & ICU use January = month 0. */
|
||||
icuDate = ucal_getMillis(cal, &errorCode);
|
||||
if(millis != icuDate) {
|
||||
/* Print days not millis to stay within printf() range. */
|
||||
log_err("utmscale_toInt64(ticks[%d], ICU4C)=%dd != %dd=ucal_getMillis(%04d-%02d-%02d)\n",
|
||||
(int)i, (int)(millis/dayMillis), (int)(icuDate/dayMillis), (int)dt->year, (int)dt->month, (int)dt->day);
|
||||
}
|
||||
|
||||
/* Test conversion from ICU time to .Net/Universal time. */
|
||||
ticks = utmscale_fromInt64((int64_t)icuDate, UDTS_ICU4C_TIME, &errorCode);
|
||||
if(ticks != dt->ticks) {
|
||||
/* Print days not ticks to stay within printf() range. */
|
||||
log_err("utmscale_fromInt64(date[%d], ICU4C)=%dd != %dd=.Net System.DateTime(%04d-%02d-%02d).Ticks\n",
|
||||
(int)i, (int)(ticks/dayTicks), (int)(dt->ticks/dayTicks), (int)dt->year, (int)dt->month, (int)dt->day);
|
||||
}
|
||||
}
|
||||
|
||||
ucal_close(cal);
|
||||
}
|
||||
|
||||
#endif /* #if !UCONFIG_NO_FORMATTING */
|
||||
|
|
Loading…
Add table
Reference in a new issue