UNPKG

7.75 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 {abstractMethodFail} from '../assert';
8
9/**
10 * A unit of date-time, such as Days or Hours.
11 *
12 * Measurement of time is built on units, such as years, months, days, hours, minutes and seconds.
13 * Implementations of this interface represent those units.
14 *
15 * An instance of this interface represents the unit itself, rather than an amount of the unit.
16 * See {@link Period} for a class that represents an amount in terms of the common units.
17 *
18 * The most commonly used units are defined in {@link ChronoUnit}.
19 * Further units are supplied in {@link IsoFields}.
20 * Units can also be written by application code by implementing this interface.
21 *
22 * The unit works using double dispatch. Client code calls methods on a date-time like
23 * {@link LocalDateTime} which check if the unit is a {@link ChronoUnit}.
24 * If it is, then the date-time must handle it.
25 * Otherwise, the method call is re-dispatched to the matching method in this interface.
26 *
27 * @interface
28 */
29export class TemporalUnit {
30 /**
31 * Gets the duration of this unit, which may be an estimate.
32 *
33 * All units return a duration measured in standard nanoseconds from this method.
34 * The duration will be positive and non-zero.
35 * For example, an hour has a duration of `60 * 60 * 1,000,000,000 ns`.
36 *
37 * Some units may return an accurate duration while others return an estimate.
38 * For example, days have an estimated duration due to the possibility of
39 * daylight saving time changes.
40 * To determine if the duration is an estimate, use {@link isDurationEstimated}.
41 *
42 * @return {Duration} the duration of this unit, which may be an estimate.
43 */
44 duration() {
45 abstractMethodFail('duration');
46 }
47
48 /**
49 * Checks if the duration of the unit is an estimate.
50 *
51 * All units have a duration, however the duration is not always accurate.
52 * For example, days have an estimated duration due to the possibility of
53 * daylight saving time changes.
54 * This method returns true if the duration is an estimate and false if it is
55 * accurate. Note that accurate/estimated ignores leap seconds.
56 *
57 * @return {boolean} `true` if the duration is estimated, `false` if accurate.
58 */
59 isDurationEstimated() {
60 abstractMethodFail('isDurationEstimated');
61 }
62
63 /**
64 * Checks if this unit is date-based.
65 *
66 * @return {boolean} `true` if date unit, `false` if a time unit.
67 */
68 isDateBased() {
69 abstractMethodFail('isDateBased');
70 }
71
72 /**
73 * Checks if this unit is time-based.
74 *
75 * @return {boolean} `true` if time unit, `false` if a date unit.
76 */
77 isTimeBased() {
78 abstractMethodFail('isTimeBased');
79 }
80
81 //-----------------------------------------------------------------------
82 /**
83 * Checks if this unit is supported by the specified temporal object.
84 *
85 * This checks that the implementing date-time can add/subtract this unit.
86 * This can be used to avoid throwing an exception.
87 *
88 * @param {!Temporal} temporal the temporal object to check.
89 * @return {boolean} `true` if the unit is supported.
90 */
91 // eslint-disable-next-line no-unused-vars
92 isSupportedBy(temporal) {
93 abstractMethodFail('isSupportedBy');
94 }
95
96 /**
97 * Returns a copy of the specified temporal object with the specified period added.
98 *
99 * The period added is a multiple of this unit. For example, this method
100 * could be used to add "3 days" to a date by calling this method on the
101 * instance representing "days", passing the date and the period "3".
102 * The period to be added may be negative, which is equivalent to subtraction.
103 *
104 * There are two equivalent ways of using this method.
105 * The first is to invoke this method directly.
106 * The second is to use {@link Temporal#plus}:
107 * <pre>
108 * // these two lines are equivalent, but the second approach is recommended
109 * temporal = thisUnit.doPlus(temporal);
110 * temporal = temporal.plus(thisUnit);
111 * </pre>
112 * It is recommended to use the second approach, {@link plus},
113 * as it is a lot clearer to read in code.
114 *
115 * Implementations should perform any queries or calculations using the units
116 * available in {@link ChronoUnit} or the fields available in {@link ChronoField}.
117 * If the field is not supported a {@link DateTimeException} must be thrown.
118 *
119 * Implementations must not alter the specified temporal object.
120 * Instead, an adjusted copy of the original must be returned.
121 * This provides equivalent, safe behavior for immutable and mutable implementations.
122 *
123 * @param {!Temporal} dateTime the temporal object to adjust.
124 * @param {number} periodToAdd the period of this unit to add, positive or negative.
125 * @return {Temporal} the adjusted temporal object.
126 * @throws DateTimeException if the period cannot be added.
127 */
128 // eslint-disable-next-line no-unused-vars
129 addTo(dateTime, periodToAdd) {
130 abstractMethodFail('addTo');
131 }
132
133 //-----------------------------------------------------------------------
134 /**
135 * Calculates the period in terms of this unit between two temporal objects of the same type.
136 *
137 * This calculates the period between two temporals in terms of this unit.
138 * The start and end points are supplied as temporal objects and must be of the same type.
139 * The result will be negative if the end is before the start.
140 * For example, the period in hours between two temporal objects can be calculated
141 * using {@link HOURS.between}.
142 *
143 * The calculation returns a whole number, representing the number of complete units between the two temporals.
144 * For example, the period in hours between the times 11:30 and 13:29 will only be
145 * one hour as it is one minute short of two hours.
146 *
147 * There are two equivalent ways of using this method.
148 * The first is to invoke this method directly.
149 * The second is to use {@link Temporal#until}:
150 * <pre>
151 * // these two lines are equivalent
152 * between = thisUnit.between(start, end);
153 * between = start.until(end, thisUnit);
154 * </pre>
155 * The choice should be made based on which makes the code more readable.
156 *
157 * For example, this method allows the number of days between two dates to be calculated:
158 * <pre>
159 * long daysBetween = DAYS.between(start, end);
160 * // or alternatively
161 * long daysBetween = start.until(end, DAYS);
162 * </pre>
163 * Implementations should perform any queries or calculations using the units available in
164 * {@link ChronoUnit} or the fields available in {@link ChronoField}.
165 * If the unit is not supported a {@link DateTimeException} must be thrown.
166 * Implementations must not alter the specified temporal objects.
167 *
168 * @param {!Temporal} temporal1 the base temporal object.
169 * @param {!Temporal} temporal2 the other temporal object.
170 * @return {number} the period between temporal1 and temporal2 in terms of this unit;
171 * positive if temporal2 is later than temporal1, negative if earlier.
172 * @throws DateTimeException if the period cannot be calculated.
173 * @throws ArithmeticException if numeric overflow occurs.
174 */
175 // eslint-disable-next-line no-unused-vars
176 between(temporal1, temporal2) {
177 abstractMethodFail('between');
178 }
179}