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