• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp.temporal;
33 
34 import org.threeten.bp.Duration;
35 import org.threeten.bp.chrono.ChronoLocalDate;
36 import org.threeten.bp.chrono.ChronoLocalDateTime;
37 import org.threeten.bp.chrono.ChronoZonedDateTime;
38 
39 /**
40  * A standard set of date periods units.
41  * <p>
42  * This set of units provide unit-based access to manipulate a date, time or date-time.
43  * The standard set of units can be extended by implementing {@link TemporalUnit}.
44  * <p>
45  * These units are intended to be applicable in multiple calendar systems.
46  * For example, most non-ISO calendar systems define units of years, months and days,
47  * just with slightly different rules.
48  * The documentation of each unit explains how it operates.
49  *
50  * <h3>Specification for implementors</h3>
51  * This is a final, immutable and thread-safe enum.
52  */
53 public enum ChronoUnit implements TemporalUnit {
54 
55     /**
56      * Unit that represents the concept of a nanosecond, the smallest supported unit of time.
57      * For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit.
58      */
59     NANOS("Nanos", Duration.ofNanos(1)),
60     /**
61      * Unit that represents the concept of a microsecond.
62      * For the ISO calendar system, it is equal to the 1,000,000th part of the second unit.
63      */
64     MICROS("Micros", Duration.ofNanos(1000)),
65     /**
66      * Unit that represents the concept of a millisecond.
67      * For the ISO calendar system, it is equal to the 1000th part of the second unit.
68      */
69     MILLIS("Millis", Duration.ofNanos(1000000)),
70     /**
71      * Unit that represents the concept of a second.
72      * For the ISO calendar system, it is equal to the second in the SI system
73      * of units, except around a leap-second.
74      */
75     SECONDS("Seconds", Duration.ofSeconds(1)),
76     /**
77      * Unit that represents the concept of a minute.
78      * For the ISO calendar system, it is equal to 60 seconds.
79      */
80     MINUTES("Minutes", Duration.ofSeconds(60)),
81     /**
82      * Unit that represents the concept of an hour.
83      * For the ISO calendar system, it is equal to 60 minutes.
84      */
85     HOURS("Hours", Duration.ofSeconds(3600)),
86     /**
87      * Unit that represents the concept of half a day, as used in AM/PM.
88      * For the ISO calendar system, it is equal to 12 hours.
89      */
90     HALF_DAYS("HalfDays", Duration.ofSeconds(43200)),
91     /**
92      * Unit that represents the concept of a day.
93      * For the ISO calendar system, it is the standard day from midnight to midnight.
94      * The estimated duration of a day is {@code 24 Hours}.
95      * <p>
96      * When used with other calendar systems it must correspond to the day defined by
97      * the rising and setting of the Sun on Earth. It is not required that days begin
98      * at midnight - when converting between calendar systems, the date should be
99      * equivalent at midday.
100      */
101     DAYS("Days", Duration.ofSeconds(86400)),
102     /**
103      * Unit that represents the concept of a week.
104      * For the ISO calendar system, it is equal to 7 days.
105      * <p>
106      * When used with other calendar systems it must correspond to an integral number of days.
107      */
108     WEEKS("Weeks", Duration.ofSeconds(7 * 86400L)),
109     /**
110      * Unit that represents the concept of a month.
111      * For the ISO calendar system, the length of the month varies by month-of-year.
112      * The estimated duration of a month is one twelfth of {@code 365.2425 Days}.
113      * <p>
114      * When used with other calendar systems it must correspond to an integral number of days.
115      */
116     MONTHS("Months", Duration.ofSeconds(31556952L / 12)),
117     /**
118      * Unit that represents the concept of a year.
119      * For the ISO calendar system, it is equal to 12 months.
120      * The estimated duration of a year is {@code 365.2425 Days}.
121      * <p>
122      * When used with other calendar systems it must correspond to an integral number of days
123      * or months roughly equal to a year defined by the passage of the Earth around the Sun.
124      */
125     YEARS("Years", Duration.ofSeconds(31556952L)),
126     /**
127      * Unit that represents the concept of a decade.
128      * For the ISO calendar system, it is equal to 10 years.
129      * <p>
130      * When used with other calendar systems it must correspond to an integral number of days
131      * and is normally an integral number of years.
132      */
133     DECADES("Decades", Duration.ofSeconds(31556952L * 10L)),
134     /**
135      * Unit that represents the concept of a century.
136      * For the ISO calendar system, it is equal to 100 years.
137      * <p>
138      * When used with other calendar systems it must correspond to an integral number of days
139      * and is normally an integral number of years.
140      */
141     CENTURIES("Centuries", Duration.ofSeconds(31556952L * 100L)),
142     /**
143      * Unit that represents the concept of a millennium.
144      * For the ISO calendar system, it is equal to 1000 years.
145      * <p>
146      * When used with other calendar systems it must correspond to an integral number of days
147      * and is normally an integral number of years.
148      */
149     MILLENNIA("Millennia", Duration.ofSeconds(31556952L * 1000L)),
150     /**
151      * Unit that represents the concept of an era.
152      * The ISO calendar system doesn't have eras thus it is impossible to add
153      * an era to a date or date-time.
154      * The estimated duration of the era is artificially defined as {@code 1,000,000,000 Years}.
155      * <p>
156      * When used with other calendar systems there are no restrictions on the unit.
157      */
158     ERAS("Eras", Duration.ofSeconds(31556952L * 1000000000L)),
159     /**
160      * Artificial unit that represents the concept of forever.
161      * This is primarily used with {@link TemporalField} to represent unbounded fields
162      * such as the year or era.
163      * The estimated duration of the era is artificially defined as the largest duration
164      * supported by {@code Duration}.
165      */
166     FOREVER("Forever", Duration.ofSeconds(Long.MAX_VALUE, 999999999));
167 
168     private final String name;
169     private final Duration duration;
170 
ChronoUnit(String name, Duration estimatedDuration)171     private ChronoUnit(String name, Duration estimatedDuration) {
172         this.name = name;
173         this.duration = estimatedDuration;
174     }
175 
176     //-----------------------------------------------------------------------
177     /**
178      * Gets the estimated duration of this unit in the ISO calendar system.
179      * <p>
180      * All of the units in this class have an estimated duration.
181      * Days vary due to daylight saving time, while months have different lengths.
182      *
183      * @return the estimated duration of this unit, not null
184      */
185     @Override
getDuration()186     public Duration getDuration() {
187         return duration;
188     }
189 
190     /**
191      * Checks if the duration of the unit is an estimate.
192      * <p>
193      * All time units in this class are considered to be accurate, while all date
194      * units in this class are considered to be estimated.
195      * <p>
196      * This definition ignores leap seconds, but considers that Days vary due to
197      * daylight saving time and months have different lengths.
198      *
199      * @return true if the duration is estimated, false if accurate
200      */
201     @Override
isDurationEstimated()202     public boolean isDurationEstimated() {
203         return isDateBased() || this == FOREVER;
204     }
205 
206     //-----------------------------------------------------------------------
207     /**
208      * Checks if this unit is a date unit.
209      *
210      * @return true if a date unit, false if a time unit
211      */
isDateBased()212     public boolean isDateBased() {
213         return this.compareTo(DAYS) >= 0 && this != FOREVER;
214     }
215 
216     /**
217      * Checks if this unit is a time unit.
218      *
219      * @return true if a time unit, false if a date unit
220      */
isTimeBased()221     public boolean isTimeBased() {
222         return this.compareTo(DAYS) < 0;
223     }
224 
225     //-----------------------------------------------------------------------
226     @Override
isSupportedBy(Temporal temporal)227     public boolean isSupportedBy(Temporal temporal) {
228         if (this == FOREVER) {
229             return false;
230         }
231         if (temporal instanceof ChronoLocalDate) {
232             return isDateBased();
233         }
234         if (temporal instanceof ChronoLocalDateTime || temporal instanceof ChronoZonedDateTime) {
235             return true;
236         }
237         try {
238             temporal.plus(1, this);
239             return true;
240         } catch (RuntimeException ex) {
241             try {
242                 temporal.plus(-1, this);
243                 return true;
244             } catch (RuntimeException ex2) {
245                 return false;
246             }
247         }
248     }
249 
250     @SuppressWarnings("unchecked")
251     @Override
addTo(R dateTime, long periodToAdd)252     public <R extends Temporal> R addTo(R dateTime, long periodToAdd) {
253         return (R) dateTime.plus(periodToAdd, this);
254     }
255 
256     //-----------------------------------------------------------------------
257     @Override
between(Temporal temporal1, Temporal temporal2)258     public long between(Temporal temporal1, Temporal temporal2) {
259         return temporal1.until(temporal2, this);
260     }
261 
262     //-----------------------------------------------------------------------
263     @Override
toString()264     public String toString() {
265         return name;
266     }
267 
268 }
269