001: /*
002: *******************************************************************************
003: * Copyright (C) 2004-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: *
007: */
008:
009: package com.ibm.icu.dev.tool.timescale;
010:
011: import java.util.Date;
012: import java.util.Locale;
013:
014: import com.ibm.icu.text.MessageFormat;
015: import com.ibm.icu.util.Calendar;
016: import com.ibm.icu.util.GregorianCalendar;
017: import com.ibm.icu.util.SimpleTimeZone;
018: import com.ibm.icu.util.TimeZone;
019:
020: /**
021: * This tool calculates the numeric values of the epoch offsets
022: * used in UniversalTimeScale.
023: *
024: * @see com.ibm.icu.util.UniversalTimeScale
025: *
026: *@draft ICU 3.2
027: */
028: public class EpochOffsets {
029:
030: /**
031: * The default constructor.
032: *
033: * @draft ICU 3.2
034: */
035: public EpochOffsets() {
036: }
037:
038: private static final long ticks = 1;
039: private static final long microseconds = ticks * 10;
040: private static final long milliseconds = microseconds * 1000;
041: private static final long seconds = milliseconds * 1000;
042: private static final long minutes = seconds * 60;
043: private static final long hours = minutes * 60;
044: private static final long days = hours * 24;
045: // Java measures time in milliseconds, not in 100ns ticks.
046: private static final long javaDays = days / milliseconds;
047:
048: private static int[][] epochDates = { { 1, Calendar.JANUARY, 1 },
049: { 1970, Calendar.JANUARY, 1 },
050: { 1601, Calendar.JANUARY, 1 },
051: { 1904, Calendar.JANUARY, 1 },
052: { 2001, Calendar.JANUARY, 1 },
053: { 1899, Calendar.DECEMBER, 31 },
054: { 1900, Calendar.MARCH, 1 } };
055:
056: /**
057: * The <code>main()</code> method calculates the epoch offsets used by the
058: * <code>UniversalTimeScale</code> class.
059: *
060: * The calculations are done using an ICU <code>Calendar</code> object. The first step is
061: * to calculate the Universal Time Scale's epoch date. Then the epoch offsets are calculated
062: * by calculating each epoch date, subtracting the universal epoch date from it, and converting
063: * that value to ticks.
064: *
065: * @param args - the command line arguments.
066: *
067: * @draft ICU 3.2
068: */
069: public static void main(String[] args) {
070: TimeZone utc = new SimpleTimeZone(0, "UTC");
071:
072: // Jitterbug 5211: .Net System.DateTime uses the proleptic calendar,
073: // while ICU by default uses the Julian calendar before 1582.
074: // Original code: Calendar cal = Calendar.getInstance(utc, Locale.ENGLISH);
075: // Use a proleptic Gregorian calendar for 0001AD and later by setting
076: // the Gregorian change date before 0001AD with a value
077: // that is safely before that date by any measure, i.e.,
078: // more than 719164 days before 1970.
079: long before0001AD = -1000000 * javaDays;
080: GregorianCalendar cal = new GregorianCalendar(utc,
081: Locale.ENGLISH);
082: cal.setGregorianChange(new Date(before0001AD));
083:
084: MessageFormat fmt = new MessageFormat(
085: "{0, date, full} {0, time, full} = {1}");
086: Object arguments[] = { cal, null };
087:
088: System.out.println("Epoch offsets:");
089:
090: // January 1, 0001 00:00:00 is the universal epoch date...
091: cal.set(1, Calendar.JANUARY, 1, 0, 0, 0);
092:
093: long universalEpoch = cal.getTimeInMillis();
094:
095: for (int i = 0; i < epochDates.length; i += 1) {
096: int[] date = epochDates[i];
097:
098: cal.set(date[0], date[1], date[2]);
099:
100: long millis = cal.getTimeInMillis();
101:
102: arguments[1] = Long.toString((millis - universalEpoch)
103: * milliseconds);
104:
105: System.out.println(fmt.format(arguments));
106: }
107: }
108: }
|