2 ******************************************************************************
3 * Copyright (C) 2007, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
8 package com.ibm.icu.impl.duration;
11 * 'Enum' for individual time units. Not an actual enum so that it can be
14 public final class TimeUnit {
15 /** The name of this unit, a key, not for localization. */
18 /** The ordinal of the unit, in order from largest to smallest. */
21 /** Private constructor */
22 private TimeUnit(String name, int ordinal) {
24 this.ordinal = (byte) ordinal;
27 public String toString() {
31 /** Represents a year. */
32 public static final TimeUnit YEAR = new TimeUnit("year", 0);
34 /** Represents a month. */
35 public static final TimeUnit MONTH = new TimeUnit("month", 1);
37 /** Represents a week. */
38 public static final TimeUnit WEEK = new TimeUnit("week", 2);
40 /** Represents a day. */
41 public static final TimeUnit DAY = new TimeUnit("day", 3);
43 /** Represents an hour. */
44 public static final TimeUnit HOUR = new TimeUnit("hour", 4);
46 /** Represents a minute. */
47 public static final TimeUnit MINUTE = new TimeUnit("minute", 5);
49 /** Represents a second. */
50 public static final TimeUnit SECOND = new TimeUnit("second", 6);
52 /** Represents a millisecond. */
53 public static final TimeUnit MILLISECOND = new TimeUnit("millisecond", 7);
55 /** Returns the next larger time unit, or null if this is the largest. */
56 public TimeUnit larger() {
57 return ordinal == 0 ? null : units[ordinal - 1];
60 /** Returns the next smaller time unit, or null if this is the smallest. */
61 public TimeUnit smaller() {
62 return ordinal == units.length - 1 ? null : units[ordinal + 1];
65 /** The list of units, in order from largest to smallest. */
66 static final TimeUnit[] units = {
67 YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, MILLISECOND
70 /** Returns the ordinal value of this time unit, largest is 0. **/
71 public int ordinal() {
75 /** Approximate, durations for the units independent of the time at which
78 // hack, initialization long array using expressions with 'L' at end doesn't
79 // compute entire expression using 'long'. differs from initializtion of
81 static final long[] approxDurations = {
82 36525L*24*60*60*10, 3045*24*60*60*10L, 7*24*60*60*1000L, 24*60*60*1000L,
83 60*60*1000L, 60*1000L, 1000L, 1L