]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/tools/misc/src/com/ibm/icu/dev/tool/timescale/EpochOffsets.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / tools / misc / src / com / ibm / icu / dev / tool / timescale / EpochOffsets.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2004-2008, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  *\r
7  */\r
8 \r
9 package com.ibm.icu.dev.tool.timescale;\r
10 \r
11 import java.util.Date;\r
12 import java.util.Locale;\r
13 \r
14 import com.ibm.icu.text.MessageFormat;\r
15 import com.ibm.icu.util.Calendar;\r
16 import com.ibm.icu.util.GregorianCalendar;\r
17 import com.ibm.icu.util.SimpleTimeZone;\r
18 import com.ibm.icu.util.TimeZone;\r
19 \r
20 /**\r
21  * This tool calculates the numeric values of the epoch offsets\r
22  * used in UniversalTimeScale.\r
23  * \r
24  * @see com.ibm.icu.util.UniversalTimeScale\r
25  */\r
26 public class EpochOffsets\r
27 {\r
28 \r
29     /**\r
30      * The default constructor.\r
31      */\r
32     public EpochOffsets()\r
33     {\r
34     }\r
35 \r
36     private static final long ticks        = 1;\r
37     private static final long microseconds = ticks * 10;\r
38     private static final long milliseconds = microseconds * 1000;\r
39     private static final long seconds      = milliseconds * 1000;\r
40     private static final long minutes      = seconds * 60;\r
41     private static final long hours        = minutes * 60;\r
42     private static final long days         = hours * 24;\r
43     // Java measures time in milliseconds, not in 100ns ticks.\r
44     private static final long javaDays     = days / milliseconds;\r
45 \r
46     private static int[][] epochDates = {\r
47             {   1, Calendar.JANUARY,   1},\r
48             {1970, Calendar.JANUARY,   1},\r
49             {1601, Calendar.JANUARY,   1},\r
50             {1904, Calendar.JANUARY,   1},\r
51             {2001, Calendar.JANUARY,   1},\r
52             {1899, Calendar.DECEMBER, 31},\r
53             {1900, Calendar.MARCH,     1}\r
54     };\r
55     \r
56     /**\r
57      * The <code>main()</code> method calculates the epoch offsets used by the\r
58      * <code>UniversalTimeScale</code> class.\r
59      * \r
60      * The calculations are done using an ICU <code>Calendar</code> object. The first step is\r
61      * to calculate the Universal Time Scale's epoch date. Then the epoch offsets are calculated\r
62      * by calculating each epoch date, subtracting the universal epoch date from it, and converting\r
63      * that value to ticks.\r
64      * \r
65      * @param args - the command line arguments.\r
66      */\r
67     public static void main(String[] args)\r
68     {\r
69         TimeZone utc = new SimpleTimeZone(0, "UTC");\r
70 \r
71         // Jitterbug 5211: .Net System.DateTime uses the proleptic calendar,\r
72         // while ICU by default uses the Julian calendar before 1582.\r
73         // Original code: Calendar cal = Calendar.getInstance(utc, Locale.ENGLISH);\r
74         // Use a proleptic Gregorian calendar for 0001AD and later by setting\r
75         // the Gregorian change date before 0001AD with a value\r
76         // that is safely before that date by any measure, i.e.,\r
77         // more than 719164 days before 1970.\r
78         long before0001AD = -1000000 * javaDays;\r
79         GregorianCalendar cal = new GregorianCalendar(utc, Locale.ENGLISH);\r
80         cal.setGregorianChange(new Date(before0001AD));\r
81 \r
82         MessageFormat fmt = new MessageFormat("{0, date, full} {0, time, full} = {1}");\r
83         Object arguments[] = {cal, null};\r
84         \r
85         System.out.println("Epoch offsets:");\r
86         \r
87         // January 1, 0001 00:00:00 is the universal epoch date...\r
88         cal.set(1, Calendar.JANUARY, 1, 0, 0, 0);\r
89 \r
90         long universalEpoch = cal.getTimeInMillis();\r
91         \r
92         for (int i = 0; i < epochDates.length; i += 1) {\r
93             int[] date = epochDates[i];\r
94             \r
95             cal.set(date[0], date[1], date[2]);\r
96             \r
97             long millis = cal.getTimeInMillis();\r
98             \r
99             arguments[1] = Long.toString((millis - universalEpoch) * milliseconds);\r
100             \r
101             System.out.println(fmt.format(arguments));\r
102          }\r
103     }\r
104 }\r