UNPKG

12.9 kBJavaScriptView Raw
1/**
2 * @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
3 * @copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
4 * @license BSD-3-Clause (see LICENSE in the root directory of this source tree)
5 */
6
7import {MathUtil} from '../MathUtil';
8
9import {Duration} from '../Duration';
10import {YearConstants} from '../YearConstants';
11import {TemporalUnit} from './TemporalUnit';
12
13/**
14 * A standard set of date periods units.
15 *
16 * This set of units provide unit-based access to manipulate a date, time or date-time.
17 * The standard set of units can be extended by implementing {@link TemporalUnit}.
18 *
19 * These units are intended to be applicable in multiple calendar systems.
20 * For example, most non-ISO calendar systems define units of years, months and days,
21 * just with slightly different rules.
22 * The documentation of each unit explains how it operates.
23 *
24 * ### Static properties:
25 *
26 * - `ChronoUnit.CENTURIES`: Unit that represents the concept of a century. For the ISO calendar
27 * system, it is equal to 100 years.
28 *
29 * - `ChronoUnit.DAYS`: Unit that represents the concept of a day. For the ISO calendar system, it
30 * is the standard day from midnight to midnight. The estimated duration of a day is 24 Hours.
31 *
32 * - `ChronoUnit.DECADES`: Unit that represents the concept of a decade. For the ISO calendar system,
33 * it is equal to 10 years.
34 *
35 * - `ChronoUnit.ERAS`: Unit that represents the concept of an era. The ISO calendar system doesn't
36 * have eras thus it is impossible to add an era to a date or date-time. The estimated duration of the
37 * era is artificially defined as 1,000,000,000 Years.
38 *
39 * - `ChronoUnit.FOREVER`: Artificial unit that represents the concept of forever. This is primarily
40 * used with {@link TemporalField} to represent unbounded fields such as the year or era. The
41 * estimated duration of the era is artificially defined as the largest duration supported by
42 * {@link Duration}.
43 *
44 * - `ChronoUnit.HALF_DAYS`: Unit that represents the concept of half a day, as used in AM/PM. For
45 * the ISO calendar system, it is equal to 12 hours.
46 *
47 * - `ChronoUnit.HOURS`: Unit that represents the concept of an hour. For the ISO calendar system,
48 * it is equal to 60 minutes.
49 *
50 * - `ChronoUnit.MICROS`: Unit that represents the concept of a microsecond. For the ISO calendar
51 * system, it is equal to the 1,000,000th part of the second unit.
52 *
53 * - `ChronoUnit.MILLENNIA`: Unit that represents the concept of a millennium. For the ISO calendar
54 * system, it is equal to 1,000 years.
55 *
56 * - `ChronoUnit.MILLIS`: Unit that represents the concept of a millisecond. For the ISO calendar
57 * system, it is equal to the 1000th part of the second unit.
58 *
59 * - `ChronoUnit.MINUTES`: Unit that represents the concept of a minute. For the ISO calendar system,
60 * it is equal to 60 seconds.
61 *
62 * - `ChronoUnit.MONTHS`: Unit that represents the concept of a month. For the ISO calendar system,
63 * the length of the month varies by month-of-year. The estimated duration of a month is one twelfth
64 * of 365.2425 Days.
65 *
66 * - `ChronoUnit.NANOS`: Unit that represents the concept of a nanosecond, the smallest supported unit
67 * of time. For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit.
68 *
69 * - `ChronoUnit.SECONDS`: Unit that represents the concept of a second. For the ISO calendar system,
70 * it is equal to the second in the SI system of units, except around a leap-second.
71 *
72 * - `ChronoUnit.WEEKS`: Unit that represents the concept of a week. For the ISO calendar system,
73 * it is equal to 7 Days.
74 *
75 * - `ChronoUnit.YEARS`: Unit that represents the concept of a year. For the ISO calendar system, it
76 * is equal to 12 months. The estimated duration of a year is 365.2425 Days.
77 */
78export class ChronoUnit extends TemporalUnit {
79
80 /**
81 *
82 * @param {String} name
83 * @param {Duration} estimatedDuration
84 * @private
85 */
86 constructor (name, estimatedDuration) {
87 super();
88 this._name = name;
89 this._duration = estimatedDuration;
90 }
91
92 //-----------------------------------------------------------------------
93 /**
94 * @return {Duration} the duration of this unit, which may be an estimate.
95 */
96 duration() {
97 return this._duration;
98 }
99
100 /**
101 * @return {boolean} `true` if the duration is estimated, `false` if accurate.
102 */
103 isDurationEstimated() {
104 return this.isDateBased() || this === ChronoUnit.FOREVER;
105 }
106
107 //-----------------------------------------------------------------------
108 /**
109 * @return {boolean} `true` if date unit, `false` if a time unit.
110 */
111 isDateBased() {
112 return this.compareTo(ChronoUnit.DAYS) >= 0 && this !== ChronoUnit.FOREVER;
113 }
114
115 /**
116 * Checks if this unit is a time unit.
117 *
118 * @return {boolean} `true` if time unit, `false` if a date unit.
119 */
120 isTimeBased() {
121 return this.compareTo(ChronoUnit.DAYS) < 0;
122 }
123
124 //-----------------------------------------------------------------------
125 /**
126 * @param {!Temporal} temporal the temporal object to check.
127 * @return {boolean} `true` if the unit is supported.
128 */
129 isSupportedBy(temporal) {
130 if (this === ChronoUnit.FOREVER) {
131 return false;
132 }
133 /* TODO: classes not implemented yet */
134 /*
135 if (temporal instanceof ChronoLocalDate) {
136 return isDateBased();
137 }
138 if (temporal instanceof ChronoLocalDateTime || temporal instanceof ChronoZonedDateTime) {
139 return true;
140 }
141*/
142 try {
143 temporal.plus(1, this);
144 return true;
145 } catch (e) {
146 try {
147 temporal.plus(-1, this);
148 return true;
149 } catch (e2) {
150 return false;
151 }
152 }
153 }
154
155 /**
156 * @param {!Temporal} dateTime the temporal object to adjust.
157 * @param {number} periodToAdd the period of this unit to add, positive or negative.
158 * @return {Temporal} the adjusted temporal object.
159 * @throws DateTimeException if the period cannot be added.
160 */
161 addTo(temporal, amount) {
162 return temporal.plus(amount, this);
163 }
164
165 //-----------------------------------------------------------------------
166 /**
167 * @param {!Temporal} temporal1 the base temporal object.
168 * @param {!Temporal} temporal2 the other temporal object.
169 * @return {number} the period between temporal1 and temporal2 in terms of this unit;
170 * positive if temporal2 is later than temporal1, negative if earlier.
171 * @throws DateTimeException if the period cannot be calculated.
172 * @throws ArithmeticException if numeric overflow occurs.
173 */
174 between(temporal1, temporal2) {
175 return temporal1.until(temporal2, this);
176 }
177
178 //-----------------------------------------------------------------------
179 toString() {
180 return this._name;
181 }
182
183 /**
184 * Compares this ChronoUnit to the specified {@link TemporalUnit}.
185 *
186 * The comparison is based on the total length of the durations.
187 *
188 * @param {!TemporalUnit} other the other unit to compare to.
189 * @return the comparator value, negative if less, positive if greater.
190 */
191 compareTo(other) {
192 return this.duration().compareTo(other.duration());
193 }
194
195}
196
197export function _init() {
198 /**
199 * Unit that represents the concept of a nanosecond, the smallest supported unit of time.
200 * For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit.
201 */
202 ChronoUnit.NANOS = new ChronoUnit('Nanos', Duration.ofNanos(1));
203 /**
204 * Unit that represents the concept of a microsecond.
205 * For the ISO calendar system, it is equal to the 1,000,000th part of the second unit.
206 */
207 ChronoUnit.MICROS = new ChronoUnit('Micros', Duration.ofNanos(1000));
208 /**
209 * Unit that represents the concept of a millisecond.
210 * For the ISO calendar system, it is equal to the 1000th part of the second unit.
211 */
212 ChronoUnit.MILLIS = new ChronoUnit('Millis', Duration.ofNanos(1000000));
213 /**
214 * Unit that represents the concept of a second.
215 * For the ISO calendar system, it is equal to the second in the SI system
216 * of units, except around a leap-second.
217 */
218 ChronoUnit.SECONDS = new ChronoUnit('Seconds', Duration.ofSeconds(1));
219 /**
220 * Unit that represents the concept of a minute.
221 * For the ISO calendar system, it is equal to 60 seconds.
222 */
223 ChronoUnit.MINUTES = new ChronoUnit('Minutes', Duration.ofSeconds(60));
224 /**
225 * Unit that represents the concept of an hour.
226 * For the ISO calendar system, it is equal to 60 minutes.
227 */
228 ChronoUnit.HOURS = new ChronoUnit('Hours', Duration.ofSeconds(3600));
229 /**
230 * Unit that represents the concept of half a day, as used in AM/PM.
231 * For the ISO calendar system, it is equal to 12 hours.
232 */
233 ChronoUnit.HALF_DAYS = new ChronoUnit('HalfDays', Duration.ofSeconds(43200));
234 /**
235 * Unit that represents the concept of a day.
236 * For the ISO calendar system, it is the standard day from midnight to midnight.
237 * The estimated duration of a day is 24 hours.
238 *
239 * When used with other calendar systems it must correspond to the day defined by
240 * the rising and setting of the Sun on Earth. It is not required that days begin
241 * at midnight - when converting between calendar systems, the date should be
242 * equivalent at midday.
243 */
244 ChronoUnit.DAYS = new ChronoUnit('Days', Duration.ofSeconds(86400));
245 /**
246 * Unit that represents the concept of a week.
247 * For the ISO calendar system, it is equal to 7 days.
248 *
249 * When used with other calendar systems it must correspond to an integral number of days.
250 */
251 ChronoUnit.WEEKS = new ChronoUnit('Weeks', Duration.ofSeconds(7 * 86400));
252 /**
253 * Unit that represents the concept of a month.
254 * For the ISO calendar system, the length of the month varies by month-of-year.
255 * The estimated duration of a month is one twelfth of 365.2425 days.
256 *
257 * When used with other calendar systems it must correspond to an integral number of days.
258 */
259 ChronoUnit.MONTHS = new ChronoUnit('Months', Duration.ofSeconds(31556952 / 12));
260 /**
261 * Unit that represents the concept of a year.
262 * For the ISO calendar system, it is equal to 12 months.
263 * The estimated duration of a year is 365.2425 days.
264 *
265 * When used with other calendar systems it must correspond to an integral number of days
266 * or months roughly equal to a year defined by the passage of the Earth around the Sun.
267 */
268 ChronoUnit.YEARS = new ChronoUnit('Years', Duration.ofSeconds(31556952));
269 /**
270 * Unit that represents the concept of a decade.
271 * For the ISO calendar system, it is equal to 10 years.
272 *
273 * When used with other calendar systems it must correspond to an integral number of days
274 * and is normally an integral number of years.
275 */
276 ChronoUnit.DECADES = new ChronoUnit('Decades', Duration.ofSeconds(31556952 * 10));
277 /**
278 * Unit that represents the concept of a century.
279 * For the ISO calendar system, it is equal to 100 years.
280 *
281 * When used with other calendar systems it must correspond to an integral number of days
282 * and is normally an integral number of years.
283 */
284 ChronoUnit.CENTURIES = new ChronoUnit('Centuries', Duration.ofSeconds(31556952 * 100));
285 /**
286 * Unit that represents the concept of a millennium.
287 * For the ISO calendar system, it is equal to 1000 years.
288 *
289 * When used with other calendar systems it must correspond to an integral number of days
290 * and is normally an integral number of years.
291 */
292 ChronoUnit.MILLENNIA = new ChronoUnit('Millennia', Duration.ofSeconds(31556952 * 1000));
293 /**
294 * Unit that represents the concept of an era.
295 * The ISO calendar system doesn't have eras thus it is impossible to add
296 * an era to a date or date-time.
297 * The estimated duration of the era is artificially defined as {Year.MAX_VALUE} + 1.
298 *
299 * When used with other calendar systems there are no restrictions on the unit.
300 */
301 ChronoUnit.ERAS = new ChronoUnit('Eras', Duration.ofSeconds(31556952 * (YearConstants.MAX_VALUE + 1)));
302 /**
303 * Artificial unit that represents the concept of forever.
304 * This is primarily used with {@link TemporalField} to represent unbounded fields
305 * such as the year or era.
306 * The estimated duration of the era is artificially defined as the largest duration
307 * supported by {@link Duration}.
308 */
309 ChronoUnit.FOREVER = new ChronoUnit('Forever', Duration.ofSeconds(MathUtil.MAX_SAFE_INTEGER, 999999999));
310}