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