001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2004, 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 com.ibm.icu.text.MessageFormat;
012: import com.ibm.icu.util.UniversalTimeScale;
013:
014: /**
015: * This class prints out the initializers needed to initialize
016: * the time scale data in the C version of <code>UniversalTimeScale</code>.
017: *
018: * It just calls <code>getTimeScaleValue()</code> for all fields and prints
019: * the initializers. Because some C compilers can't compile a literal constant for
020: * the minimum and / or maximum values of an <code>int64_t</code>, this code will
021: * print <code>U_INT64_MIN</code> or <code>U_INT64_MAX</code> for these values.
022: *
023: * @see com.ibm.icu.util.UniversalTimeScale
024: *
025: * @draft ICU 3.2
026: */
027: public class GenerateCTimeScaleData {
028:
029: /**
030: * The default constructor.
031: *
032: * @draft ICU 3.2
033: */
034: public GenerateCTimeScaleData() {
035: }
036:
037: private static final long ticks = 1;
038: private static final long microseconds = ticks * 10;
039: private static final long milliseconds = microseconds * 1000;
040: private static final long seconds = milliseconds * 1000;
041: private static final long minutes = seconds * 60;
042: private static final long hours = minutes * 60;
043: private static final long days = hours * 24;
044:
045: /*
046: * Returns <code>String</code> that is a literal representation of the given value.
047: * This will either be a call to the <code>INT64_C()</code> macro, or the constant
048: * <code>U_INT64_MIN</code> or <U_INT64_MAX>.
049: */
050: private static String minMaxFilter(long value) {
051: if (value == Long.MIN_VALUE) {
052: return "U_INT64_MIN";
053: } else if (value == Long.MAX_VALUE) {
054: return "U_INT64_MAX";
055: }
056:
057: return "INT64_C(" + Long.toString(value) + ")";
058: }
059:
060: /**
061: * This method prints the C initializers for the time scale data.
062: *
063: * @param args - the command line arguments
064: *
065: * @see com.ibm.icu.util.UniversalTimeScale
066: *
067: * @draft ICU 3.2
068: */
069: public static void main(String[] args) {
070: MessageFormat fmt = new MessageFormat(
071: "'{'{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}},");
072: Object cargs[] = { null, null, null, null, null, null, null,
073: null, null, null, null };
074:
075: System.out.println("\nC data:");
076:
077: for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
078: long units = UniversalTimeScale.getTimeScaleValue(scale,
079: UniversalTimeScale.UNITS_VALUE);
080:
081: if (units == ticks) {
082: cargs[0] = "ticks";
083: } else if (units == microseconds) {
084: cargs[0] = "microseconds";
085: } else if (units == milliseconds) {
086: cargs[0] = "milliseconds";
087: } else if (units == seconds) {
088: cargs[0] = "seconds";
089: } else if (units == minutes) {
090: cargs[0] = "minutes";
091: } else if (units == hours) {
092: cargs[0] = "hours";
093: } else if (units == days) {
094: cargs[0] = "days";
095: } else {
096: cargs[0] = "INT64_C(" + Long.toString(units) + ")";
097: }
098:
099: cargs[1] = minMaxFilter(UniversalTimeScale
100: .getTimeScaleValue(scale,
101: UniversalTimeScale.EPOCH_OFFSET_VALUE));
102: cargs[2] = minMaxFilter(UniversalTimeScale
103: .getTimeScaleValue(scale,
104: UniversalTimeScale.FROM_MIN_VALUE));
105: cargs[3] = minMaxFilter(UniversalTimeScale
106: .getTimeScaleValue(scale,
107: UniversalTimeScale.FROM_MAX_VALUE));
108: cargs[4] = minMaxFilter(UniversalTimeScale
109: .getTimeScaleValue(scale,
110: UniversalTimeScale.TO_MIN_VALUE));
111: cargs[5] = minMaxFilter(UniversalTimeScale
112: .getTimeScaleValue(scale,
113: UniversalTimeScale.TO_MAX_VALUE));
114: cargs[6] = minMaxFilter(UniversalTimeScale
115: .getTimeScaleValue(
116: scale,
117: UniversalTimeScale.EPOCH_OFFSET_PLUS_1_VALUE));
118: cargs[7] = minMaxFilter(UniversalTimeScale
119: .getTimeScaleValue(
120: scale,
121: UniversalTimeScale.EPOCH_OFFSET_MINUS_1_VALUE));
122: cargs[8] = minMaxFilter(UniversalTimeScale
123: .getTimeScaleValue(scale,
124: UniversalTimeScale.UNITS_ROUND_VALUE));
125: cargs[9] = minMaxFilter(UniversalTimeScale
126: .getTimeScaleValue(scale,
127: UniversalTimeScale.MIN_ROUND_VALUE));
128: cargs[10] = minMaxFilter(UniversalTimeScale
129: .getTimeScaleValue(scale,
130: UniversalTimeScale.MAX_ROUND_VALUE));
131:
132: System.out.println(fmt.format(cargs));
133: }
134: }
135: }
|