2 *******************************************************************************
3 * Copyright (C) 2004-2008, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 package com.ibm.icu.dev.tool.timescale;
11 import java.util.Date;
12 import java.util.Locale;
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;
21 * This tool calculates the numeric values of the epoch offsets
22 * used in UniversalTimeScale.
24 * @see com.ibm.icu.util.UniversalTimeScale
26 public class EpochOffsets
30 * The default constructor.
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;
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}
57 * The <code>main()</code> method calculates the epoch offsets used by the
58 * <code>UniversalTimeScale</code> class.
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.
65 * @param args - the command line arguments.
67 public static void main(String[] args)
69 TimeZone utc = new SimpleTimeZone(0, "UTC");
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));
82 MessageFormat fmt = new MessageFormat("{0, date, full} {0, time, full} = {1}");
83 Object arguments[] = {cal, null};
85 System.out.println("Epoch offsets:");
87 // January 1, 0001 00:00:00 is the universal epoch date...
88 cal.set(1, Calendar.JANUARY, 1, 0, 0, 0);
90 long universalEpoch = cal.getTimeInMillis();
92 for (int i = 0; i < epochDates.length; i += 1) {
93 int[] date = epochDates[i];
95 cal.set(date[0], date[1], date[2]);
97 long millis = cal.getTimeInMillis();
99 arguments[1] = Long.toString((millis - universalEpoch) * milliseconds);
101 System.out.println(fmt.format(arguments));