Home | History | Annotate | Download | only in timescale
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 1996-2008, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  *
      9  */
     10 
     11 package com.ibm.icu.dev.tool.timescale;
     12 
     13 import com.ibm.icu.text.MessageFormat;
     14 import com.ibm.icu.util.UniversalTimeScale;
     15 
     16 /**
     17  * This class prints out the initializers needed to initialize
     18  * the time scale data in the C version of <code>UniversalTimeScale</code>.
     19  *
     20  * It just calls <code>getTimeScaleValue()</code> for all fields and prints
     21  * the initializers. Because some C compilers can't compile a literal constant for
     22  * the minimum and / or maximum values of an <code>int64_t</code>, this code will
     23  * print <code>U_INT64_MIN</code> or <code>U_INT64_MAX</code> for these values.
     24  *
     25  * @see com.ibm.icu.util.UniversalTimeScale
     26  */
     27 public class GenerateCTimeScaleData
     28 {
     29 
     30     /**
     31      * The default constructor.
     32      */
     33     public GenerateCTimeScaleData()
     34     {
     35     }
     36 
     37     private static final long ticks        = 1;
     38     private static final long microseconds = ticks * 10;
     39     private static final long milliseconds = microseconds * 1000;
     40     private static final long seconds      = milliseconds * 1000;
     41     private static final long minutes      = seconds * 60;
     42     private static final long hours        = minutes * 60;
     43     private static final long days         = hours * 24;
     44 
     45     /*
     46      * Returns <code>String</code> that is a literal representation of the given value.
     47      * This will either be a call to the <code>INT64_C()</code> macro, or the constant
     48      * <code>U_INT64_MIN</code> or <U_INT64_MAX>.
     49      */
     50     private static String minMaxFilter(long value)
     51     {
     52         if (value == Long.MIN_VALUE) {
     53             return "U_INT64_MIN";
     54         } else if (value == Long.MAX_VALUE) {
     55             return "U_INT64_MAX";
     56         }
     57 
     58         return "INT64_C(" + Long.toString(value) + ")";
     59     }
     60 
     61     /**
     62      * This method prints the C initializers for the time scale data.
     63      *
     64      * @param args - the command line arguments
     65      *
     66      * @see com.ibm.icu.util.UniversalTimeScale
     67      */
     68     public static void main(String[] args)
     69     {
     70         MessageFormat fmt = new MessageFormat("'{'{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}},");
     71         Object cargs[] = {null, null, null, null, null, null, null, null, null, null, null};
     72 
     73         System.out.println("\nC data:");
     74 
     75         for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
     76             long units = UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.UNITS_VALUE);
     77 
     78             if (units == ticks) {
     79                 cargs[0] = "ticks";
     80             } else if (units == microseconds) {
     81                 cargs[0] = "microseconds";
     82             } else if (units == milliseconds) {
     83                 cargs[0] = "milliseconds";
     84             } else if (units == seconds) {
     85                 cargs[0] = "seconds";
     86             } else if (units == minutes) {
     87                 cargs[0] = "minutes";
     88             } else if (units == hours) {
     89                 cargs[0] = "hours";
     90             } else if (units == days) {
     91                 cargs[0] = "days";
     92             } else {
     93                 cargs[0] = "INT64_C(" + Long.toString(units) + ")";
     94             }
     95 
     96             cargs[1]  = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.EPOCH_OFFSET_VALUE));
     97             cargs[2]  = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.FROM_MIN_VALUE));
     98             cargs[3]  = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.FROM_MAX_VALUE));
     99             cargs[4]  = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.TO_MIN_VALUE));
    100             cargs[5]  = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.TO_MAX_VALUE));
    101             cargs[6]  = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.EPOCH_OFFSET_PLUS_1_VALUE));
    102             cargs[7]  = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.EPOCH_OFFSET_MINUS_1_VALUE));
    103             cargs[8]  = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.UNITS_ROUND_VALUE));
    104             cargs[9]  = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.MIN_ROUND_VALUE));
    105             cargs[10] = minMaxFilter(UniversalTimeScale.getTimeScaleValue(scale, UniversalTimeScale.MAX_ROUND_VALUE));
    106 
    107             System.out.println(fmt.format(cargs));
    108         }
    109     }
    110 }
    111