@parischap/conversions 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1304 -0
- package/cjs/BigDecimal.js +16 -0
- package/cjs/BigDecimal.js.map +1 -0
- package/cjs/BigInt.js +34 -0
- package/cjs/BigInt.js.map +1 -0
- package/cjs/DateTime.js +2202 -0
- package/cjs/DateTime.js.map +1 -0
- package/cjs/DateTimeFormat.js +269 -0
- package/cjs/DateTimeFormat.js.map +1 -0
- package/cjs/DateTimeFormatContext.js +327 -0
- package/cjs/DateTimeFormatContext.js.map +1 -0
- package/cjs/Email.js +73 -0
- package/cjs/Email.js.map +1 -0
- package/cjs/Integer.js +155 -0
- package/cjs/Integer.js.map +1 -0
- package/cjs/NumberBase10Format.js +759 -0
- package/cjs/NumberBase10Format.js.map +1 -0
- package/cjs/PositiveInteger.js +191 -0
- package/cjs/PositiveInteger.js.map +1 -0
- package/cjs/PositiveReal.js +162 -0
- package/cjs/PositiveReal.js.map +1 -0
- package/cjs/Real.js +113 -0
- package/cjs/Real.js.map +1 -0
- package/cjs/RoundingMode.js +86 -0
- package/cjs/RoundingMode.js.map +1 -0
- package/cjs/RoundingOption.js +152 -0
- package/cjs/RoundingOption.js.map +1 -0
- package/cjs/Schema.js +218 -0
- package/cjs/Schema.js.map +1 -0
- package/cjs/SemVer.js +73 -0
- package/cjs/SemVer.js.map +1 -0
- package/cjs/Template.js +189 -0
- package/cjs/Template.js.map +1 -0
- package/cjs/TemplatePart.js +51 -0
- package/cjs/TemplatePart.js.map +1 -0
- package/cjs/TemplateParts.js +46 -0
- package/cjs/TemplateParts.js.map +1 -0
- package/cjs/TemplatePlaceholder.js +381 -0
- package/cjs/TemplatePlaceholder.js.map +1 -0
- package/cjs/TemplateSeparator.js +116 -0
- package/cjs/TemplateSeparator.js.map +1 -0
- package/cjs/index.js +67 -0
- package/cjs/index.js.map +1 -0
- package/cjs/internal/Int.js +50 -0
- package/cjs/internal/Int.js.map +1 -0
- package/cjs/internal/Positive.js +50 -0
- package/cjs/internal/Positive.js.map +1 -0
- package/dts/BigDecimal.d.ts +11 -0
- package/dts/BigDecimal.d.ts.map +1 -0
- package/dts/BigInt.d.ts +30 -0
- package/dts/BigInt.d.ts.map +1 -0
- package/dts/DateTime.d.ts +1421 -0
- package/dts/DateTime.d.ts.map +1 -0
- package/dts/DateTimeFormat.d.ts +257 -0
- package/dts/DateTimeFormat.d.ts.map +1 -0
- package/dts/DateTimeFormatContext.d.ts +112 -0
- package/dts/DateTimeFormatContext.d.ts.map +1 -0
- package/dts/Email.d.ts +61 -0
- package/dts/Email.d.ts.map +1 -0
- package/dts/Integer.d.ts +123 -0
- package/dts/Integer.d.ts.map +1 -0
- package/dts/NumberBase10Format.d.ts +641 -0
- package/dts/NumberBase10Format.d.ts.map +1 -0
- package/dts/PositiveInteger.d.ts +161 -0
- package/dts/PositiveInteger.d.ts.map +1 -0
- package/dts/PositiveReal.d.ts +130 -0
- package/dts/PositiveReal.d.ts.map +1 -0
- package/dts/Real.d.ts +102 -0
- package/dts/Real.d.ts.map +1 -0
- package/dts/RoundingMode.d.ts +72 -0
- package/dts/RoundingMode.d.ts.map +1 -0
- package/dts/RoundingOption.d.ts +79 -0
- package/dts/RoundingOption.d.ts.map +1 -0
- package/dts/Schema.d.ts +159 -0
- package/dts/Schema.d.ts.map +1 -0
- package/dts/SemVer.d.ts +61 -0
- package/dts/SemVer.d.ts.map +1 -0
- package/dts/Template.d.ts +113 -0
- package/dts/Template.d.ts.map +1 -0
- package/dts/TemplatePart.d.ts +28 -0
- package/dts/TemplatePart.d.ts.map +1 -0
- package/dts/TemplateParts.d.ts +23 -0
- package/dts/TemplateParts.d.ts.map +1 -0
- package/dts/TemplatePlaceholder.d.ts +274 -0
- package/dts/TemplatePlaceholder.d.ts.map +1 -0
- package/dts/TemplateSeparator.d.ts +130 -0
- package/dts/TemplateSeparator.d.ts.map +1 -0
- package/dts/index.d.ts +21 -0
- package/dts/index.d.ts.map +1 -0
- package/dts/internal/Int.d.ts +6 -0
- package/dts/internal/Int.d.ts.map +1 -0
- package/dts/internal/Positive.d.ts +6 -0
- package/dts/internal/Positive.d.ts.map +1 -0
- package/esm/BigDecimal.js +9 -0
- package/esm/BigDecimal.js.map +1 -0
- package/esm/BigInt.js +27 -0
- package/esm/BigInt.js.map +1 -0
- package/esm/DateTime.js +2130 -0
- package/esm/DateTime.js.map +1 -0
- package/esm/DateTimeFormat.js +238 -0
- package/esm/DateTimeFormat.js.map +1 -0
- package/esm/DateTimeFormatContext.js +296 -0
- package/esm/DateTimeFormatContext.js.map +1 -0
- package/esm/Email.js +65 -0
- package/esm/Email.js.map +1 -0
- package/esm/Integer.js +128 -0
- package/esm/Integer.js.map +1 -0
- package/esm/NumberBase10Format.js +717 -0
- package/esm/NumberBase10Format.js.map +1 -0
- package/esm/PositiveInteger.js +164 -0
- package/esm/PositiveInteger.js.map +1 -0
- package/esm/PositiveReal.js +135 -0
- package/esm/PositiveReal.js.map +1 -0
- package/esm/Real.js +106 -0
- package/esm/Real.js.map +1 -0
- package/esm/RoundingMode.js +79 -0
- package/esm/RoundingMode.js.map +1 -0
- package/esm/RoundingOption.js +120 -0
- package/esm/RoundingOption.js.map +1 -0
- package/esm/Schema.js +203 -0
- package/esm/Schema.js.map +1 -0
- package/esm/SemVer.js +65 -0
- package/esm/SemVer.js.map +1 -0
- package/esm/Template.js +158 -0
- package/esm/Template.js.map +1 -0
- package/esm/TemplatePart.js +22 -0
- package/esm/TemplatePart.js.map +1 -0
- package/esm/TemplateParts.js +19 -0
- package/esm/TemplateParts.js.map +1 -0
- package/esm/TemplatePlaceholder.js +342 -0
- package/esm/TemplatePlaceholder.js.map +1 -0
- package/esm/TemplateSeparator.js +107 -0
- package/esm/TemplateSeparator.js.map +1 -0
- package/esm/index.js +21 -0
- package/esm/index.js.map +1 -0
- package/esm/internal/Int.js +43 -0
- package/esm/internal/Int.js.map +1 -0
- package/esm/internal/Positive.js +43 -0
- package/esm/internal/Positive.js.map +1 -0
- package/esm/package.json +3 -0
- package/package.json +49 -0
package/esm/DateTime.js
ADDED
@@ -0,0 +1,2130 @@
|
|
1
|
+
/**
|
2
|
+
* This module implements an immutable DateTime object.
|
3
|
+
*
|
4
|
+
* DateTime objects keep an internal state. But all provided functions look pure insofar as they
|
5
|
+
* will always yield the same result whatever the state the object is in. The state is only used to
|
6
|
+
* improve performance but does not alter the results.
|
7
|
+
*
|
8
|
+
* Unlike the Javascript Date objects and the Effect DateTime objects, DateTime objects handle both
|
9
|
+
* the Gregorian and Iso calendars. So you can easily get/set the iso year and iso week of a
|
10
|
+
* DateTime object.
|
11
|
+
*
|
12
|
+
* A DateTime object has a `zoneOffset` which is the difference in hours between the time in the
|
13
|
+
* local zone and UTC time (e.g zoneOffset=1 for timezone +1:00). All the data in a DateTime object
|
14
|
+
* is `zoneOffset-dependent`, except `timestamp`. An important thing to note is that a DateTime
|
15
|
+
* object with a timestamp t and a zoneOffset zo has exactly the same date parts (year, ordinalDay,
|
16
|
+
* month, monthDay, isoYear...) as a DateTime object with a timestamp t+zox3600 and a 0 zoneOffset.
|
17
|
+
* That's the reason for the _zonedTimestamp field which is equal to t+zox3600. All calculations are
|
18
|
+
* performed UTC using _zonedTimestamp instead of timestamp.
|
19
|
+
*/
|
20
|
+
import { MArray, MFunction, MInputError, MInspectable, MNumber, MPipeable, MStruct, MTypes } from '@parischap/effect-lib';
|
21
|
+
import { DateTime, Either, Equal, Function, Hash, Number, Option, Predicate, Struct, flow, pipe } from 'effect';
|
22
|
+
import * as CVNumberBase10Format from './NumberBase10Format.js';
|
23
|
+
import * as CVTemplate from './Template.js';
|
24
|
+
import * as CVTemplatePlaceholder from './TemplatePlaceholder.js';
|
25
|
+
import * as CVTemplateSeparator from './TemplateSeparator.js';
|
26
|
+
/**
|
27
|
+
* Module tag
|
28
|
+
*
|
29
|
+
* @category Module markers
|
30
|
+
*/
|
31
|
+
export const moduleTag = '@parischap/conversions/DateTime/';
|
32
|
+
const _TypeId = /*#__PURE__*/Symbol.for(moduleTag);
|
33
|
+
/**
|
34
|
+
* Duration of a second in milliseconds
|
35
|
+
*
|
36
|
+
* @category Constants
|
37
|
+
*/
|
38
|
+
export const SECOND_MS = 1_000;
|
39
|
+
/**
|
40
|
+
* Duration of a minute in milliseconds
|
41
|
+
*
|
42
|
+
* @category Constants
|
43
|
+
*/
|
44
|
+
export const MINUTE_MS = 60 * SECOND_MS;
|
45
|
+
/**
|
46
|
+
* Duration of an hour in milliseconds
|
47
|
+
*
|
48
|
+
* @category Constants
|
49
|
+
*/
|
50
|
+
export const HOUR_MS = 60 * MINUTE_MS;
|
51
|
+
/**
|
52
|
+
* Duration of a day in milliseconds
|
53
|
+
*
|
54
|
+
* @category Constants
|
55
|
+
*/
|
56
|
+
export const DAY_MS = 24 * HOUR_MS;
|
57
|
+
/**
|
58
|
+
* Duration of a week in milliseconds
|
59
|
+
*
|
60
|
+
* @category Constants
|
61
|
+
*/
|
62
|
+
export const WEEK_MS = 7 * DAY_MS;
|
63
|
+
/**
|
64
|
+
* Duration of a normal year in milliseconds
|
65
|
+
*
|
66
|
+
* @category Constants
|
67
|
+
*/
|
68
|
+
export const COMMON_YEAR_MS = 365 * DAY_MS;
|
69
|
+
/**
|
70
|
+
* Duration of a leap year in milliseconds
|
71
|
+
*
|
72
|
+
* @category Constants
|
73
|
+
*/
|
74
|
+
export const LEAP_YEAR_MS = COMMON_YEAR_MS + DAY_MS;
|
75
|
+
/**
|
76
|
+
* Duration of a short iso year in milliseconds
|
77
|
+
*
|
78
|
+
* @category Constants
|
79
|
+
*/
|
80
|
+
export const SHORT_YEAR_MS = 52 * WEEK_MS;
|
81
|
+
/**
|
82
|
+
* Duration of a long iso year in milliseconds
|
83
|
+
*
|
84
|
+
* @category Constants
|
85
|
+
*/
|
86
|
+
export const LONG_YEAR_MS = SHORT_YEAR_MS + WEEK_MS;
|
87
|
+
/**
|
88
|
+
* Local time zone offset in hours of the machine on which this code runs. The value is calculated
|
89
|
+
* once at startup.
|
90
|
+
*
|
91
|
+
* @category Constants
|
92
|
+
*/
|
93
|
+
export const LOCAL_TIME_ZONE_OFFSET = -(/*#__PURE__*/new Date().getTimezoneOffset() / 60);
|
94
|
+
/**
|
95
|
+
* Namespace for the data relative to a Month
|
96
|
+
*
|
97
|
+
* @category Models
|
98
|
+
*/
|
99
|
+
const MAX_FULL_YEAR_OFFSET = 273_790;
|
100
|
+
/**
|
101
|
+
* Maximal usable year (ECMA-262)
|
102
|
+
*
|
103
|
+
* @category Constants
|
104
|
+
*/
|
105
|
+
export const MAX_FULL_YEAR = 1970 + MAX_FULL_YEAR_OFFSET;
|
106
|
+
/**
|
107
|
+
* Minimal usable year (ECMA-262)
|
108
|
+
*
|
109
|
+
* @category Constants
|
110
|
+
*/
|
111
|
+
export const MIN_FULL_YEAR = 1970 - MAX_FULL_YEAR_OFFSET - 1;
|
112
|
+
/**
|
113
|
+
* Maximal usable timestamp (ECMA-262)
|
114
|
+
*
|
115
|
+
* @category Constants
|
116
|
+
*/
|
117
|
+
export const MAX_TIMESTAMP = 8_640_000_000_000_000;
|
118
|
+
/**
|
119
|
+
* Minimal usable timestamp (ECMA-262)
|
120
|
+
*
|
121
|
+
* @category Constants
|
122
|
+
*/
|
123
|
+
export const MIN_TIMESTAMP = -MAX_TIMESTAMP;
|
124
|
+
const _integer = CVNumberBase10Format.integer;
|
125
|
+
const _params = {
|
126
|
+
fillChar: '0',
|
127
|
+
numberBase10Format: _integer
|
128
|
+
};
|
129
|
+
const _fixedLengthToReal = CVTemplatePlaceholder.fixedLengthToReal;
|
130
|
+
const _sep = CVTemplateSeparator;
|
131
|
+
/**
|
132
|
+
* Namespace for the a Gregorian date
|
133
|
+
*
|
134
|
+
* It is important to note that the Gregorian calendar is periodic with a 400-year period as far as
|
135
|
+
* leap years are concerned. Leap years are those that can be divided by 4, except those that can be
|
136
|
+
* divided by 100 except those that can be divided by 400. So 2100, 2200, 2300 are not leap years.
|
137
|
+
* But 2400 is a leap year.
|
138
|
+
*
|
139
|
+
* @category Models
|
140
|
+
*/
|
141
|
+
var GregorianDate;
|
142
|
+
(function (GregorianDate) {
|
143
|
+
const _namespaceTag = moduleTag + 'GregorianDate/';
|
144
|
+
const _TypeId = /*#__PURE__*/Symbol.for(_namespaceTag);
|
145
|
+
/**
|
146
|
+
* Duration in milliseconds of a four-year period containing a leap year
|
147
|
+
*
|
148
|
+
* @category Constants
|
149
|
+
*/
|
150
|
+
const FOUR_YEARS_MS = 3 * COMMON_YEAR_MS + LEAP_YEAR_MS;
|
151
|
+
/**
|
152
|
+
* Duration in milliseconds of a 100-year period that has a leap year every 4th year except the
|
153
|
+
* 100th year
|
154
|
+
*
|
155
|
+
* @category Constants
|
156
|
+
*/
|
157
|
+
const HUNDRED_YEARS_MS = 25 * FOUR_YEARS_MS - DAY_MS;
|
158
|
+
/**
|
159
|
+
* Duration in milliseconds of a 400-year period that has a leap year every 4th year except the
|
160
|
+
* 100th year. But the 400th year is a leap year
|
161
|
+
*
|
162
|
+
* @category Constants
|
163
|
+
*/
|
164
|
+
const FOUR_HUNDRED_YEARS_MS = 4 * HUNDRED_YEARS_MS + DAY_MS;
|
165
|
+
/** Timestamp of 1/1/2001 00:00:00:000+0:00 */
|
166
|
+
const YEAR_START_2001_MS = 978_307_200_000;
|
167
|
+
/** Number of days in each month of a leap year */
|
168
|
+
const LEAP_YEAR_DAYS_IN_MONTH = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
169
|
+
/** Number of days in each month of a leap year */
|
170
|
+
const COMMON_YEAR_DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
171
|
+
/**
|
172
|
+
* Type guard
|
173
|
+
*
|
174
|
+
* @category Guards
|
175
|
+
*/
|
176
|
+
GregorianDate.has = u => Predicate.hasProperty(u, _TypeId);
|
177
|
+
/** Proto */
|
178
|
+
const proto = {
|
179
|
+
[_TypeId]: _TypeId,
|
180
|
+
... /*#__PURE__*/MInspectable.BaseProto(_namespaceTag),
|
181
|
+
...MPipeable.BaseProto
|
182
|
+
};
|
183
|
+
/** Constructor */
|
184
|
+
const _make = params => MTypes.objectFromDataAndProto(proto, params);
|
185
|
+
const _makeWithInternals = params => _make({
|
186
|
+
...params,
|
187
|
+
_daysInMonth: params.yearIsLeap ? LEAP_YEAR_DAYS_IN_MONTH : COMMON_YEAR_DAYS_IN_MONTH
|
188
|
+
});
|
189
|
+
/**
|
190
|
+
* Constructs a GregorianDate from a timestamp
|
191
|
+
*
|
192
|
+
* @category Constructors
|
193
|
+
*/
|
194
|
+
GregorianDate.fromTimestamp = timestamp => {
|
195
|
+
/**
|
196
|
+
* The 100-year periods [2001, 2100], [2101, 2200], and [2201, 2300] all last HUNDRED_YEARS_MS.
|
197
|
+
* Those three 100-year periods can be divided in 24 periods that last FOUR_YEARS_MS
|
198
|
+
* (4xCOMMON_YEAR_MS + DAY_MS) and a final 4-year period that lasts FOUR_YEARS_MS - DAY_MS
|
199
|
+
* (4xCOMMON_YEAR_MS).
|
200
|
+
*
|
201
|
+
* The 100-year period [2301, 2400] lasts HUNDRED_YEARS_MS + DAY_MS. This period can be divided
|
202
|
+
* in 25 periods that last FOUR_YEARS_MS (4xCOMMON_YEAR_MS + DAY_MS).
|
203
|
+
*/
|
204
|
+
const offset2001 = timestamp - YEAR_START_2001_MS;
|
205
|
+
const q400Years = Math.floor(offset2001 / FOUR_HUNDRED_YEARS_MS);
|
206
|
+
const offset400Years = q400Years * FOUR_HUNDRED_YEARS_MS;
|
207
|
+
const r400Years = offset2001 - offset400Years;
|
208
|
+
// q100Years is equal to 4 on the last day of the 400-year period.
|
209
|
+
const q100Years = Math.min(3, Math.floor(r400Years / HUNDRED_YEARS_MS));
|
210
|
+
const offset100Years = q100Years * HUNDRED_YEARS_MS;
|
211
|
+
// r100Years is superior to HUNDRED_YEARS_MS on the last day of the 400-year period
|
212
|
+
const r100Years = r400Years - offset100Years;
|
213
|
+
const q4Years = Math.floor(r100Years / FOUR_YEARS_MS);
|
214
|
+
const offset4Years = q4Years * FOUR_YEARS_MS;
|
215
|
+
const r4Years = r100Years - offset4Years;
|
216
|
+
// q1Year is equal to 4 on the last day of each 4-year period except the last day of years 2100, 2200 and 2300.
|
217
|
+
const q1Year = Math.min(3, Math.floor(r4Years / COMMON_YEAR_MS));
|
218
|
+
const offset1Year = q1Year * COMMON_YEAR_MS;
|
219
|
+
const yearIsLeap = q1Year === 3 && (q4Years !== 24 || q100Years === 3);
|
220
|
+
const yearStartTimestamp = YEAR_START_2001_MS + offset400Years + offset100Years + offset4Years + offset1Year;
|
221
|
+
return _makeWithInternals({
|
222
|
+
timestamp,
|
223
|
+
year: 2001 + 400 * q400Years + 100 * q100Years + 4 * q4Years + q1Year,
|
224
|
+
yearIsLeap,
|
225
|
+
yearStartTimestamp,
|
226
|
+
ordinalDay: Math.floor((timestamp - yearStartTimestamp) / DAY_MS) + 1,
|
227
|
+
month: Option.none(),
|
228
|
+
monthDay: Option.none()
|
229
|
+
});
|
230
|
+
};
|
231
|
+
/**
|
232
|
+
* If possible, returns a new GregorianDate having `year` set to `year` and the same `month` and
|
233
|
+
* `monthDay` as `self`. Returns a left of an error otherwise. `year` must be an integer comprised
|
234
|
+
* in the range [MIN_FULL_YEAR, MAX_FULL_YEAR]. If `self` represents a 29th of february, `year`
|
235
|
+
* must be a leap year.
|
236
|
+
*
|
237
|
+
* @category Setters
|
238
|
+
*/
|
239
|
+
GregorianDate.setYear = year => self => Either.gen(function* () {
|
240
|
+
const validatedYear = yield* pipe(year, MInputError.assertInRange({
|
241
|
+
min: MIN_FULL_YEAR,
|
242
|
+
max: MAX_FULL_YEAR,
|
243
|
+
minIncluded: true,
|
244
|
+
maxIncluded: true,
|
245
|
+
offset: 0,
|
246
|
+
name: "'year'"
|
247
|
+
}));
|
248
|
+
const offset2001 = validatedYear - 2001;
|
249
|
+
const q400Years = Math.floor(offset2001 / 400);
|
250
|
+
const r400Years = offset2001 - 400 * q400Years;
|
251
|
+
const q100Years = Math.floor(r400Years / 100);
|
252
|
+
const r100Years = r400Years - 100 * q100Years;
|
253
|
+
const q4Years = Math.floor(r100Years / 4);
|
254
|
+
const r4Years = r100Years - 4 * q4Years;
|
255
|
+
const yearIsLeap = r4Years === 3 && (r100Years !== 99 || r400Years === 399);
|
256
|
+
const yearStartTimestamp = YEAR_START_2001_MS + q400Years * FOUR_HUNDRED_YEARS_MS + q100Years * HUNDRED_YEARS_MS + q4Years * FOUR_YEARS_MS + r4Years * COMMON_YEAR_MS;
|
257
|
+
const selfYearIsLeap = self.yearIsLeap;
|
258
|
+
const selfOrdinalDay = self.ordinalDay;
|
259
|
+
const ordinalDayOffset = yield* selfYearIsLeap === yearIsLeap || selfOrdinalDay < 60 ? Either.right(0) : selfYearIsLeap ? selfOrdinalDay === 60 ? Either.left(new MInputError.Type({
|
260
|
+
message: `No February 29th on year ${year} which is not a leap year`
|
261
|
+
})) : Either.right(-1) : Either.right(1);
|
262
|
+
return _makeWithInternals({
|
263
|
+
timestamp: self.timestamp + yearStartTimestamp - self.yearStartTimestamp + ordinalDayOffset * DAY_MS,
|
264
|
+
year: validatedYear,
|
265
|
+
yearIsLeap,
|
266
|
+
yearStartTimestamp,
|
267
|
+
ordinalDay: selfOrdinalDay + ordinalDayOffset,
|
268
|
+
month: self.month,
|
269
|
+
monthDay: self.monthDay
|
270
|
+
});
|
271
|
+
});
|
272
|
+
/**
|
273
|
+
* If possible, returns a new GregorianDate having `month` set to `month` and the same `year` and
|
274
|
+
* `monthDay` as `self`. Returns a left of an error otherwise. `month` must be an integer greater
|
275
|
+
* than or equal to 1 (January) and less than or equal to 12 (December). `month` must also have at
|
276
|
+
* least `monthDay` days.
|
277
|
+
*
|
278
|
+
* @category Setters
|
279
|
+
*/
|
280
|
+
GregorianDate.setMonth = month => self => Either.gen(function* () {
|
281
|
+
const validatedMonth = yield* pipe(month, MInputError.assertInRange({
|
282
|
+
min: 1,
|
283
|
+
max: 12,
|
284
|
+
minIncluded: true,
|
285
|
+
maxIncluded: true,
|
286
|
+
offset: 0,
|
287
|
+
name: "'month'"
|
288
|
+
}));
|
289
|
+
const monthDay = yield* pipe(self, GregorianDate.getMonthDay, Either.liftPredicate(Predicate.or(Number.lessThanOrEqualTo(28), Number.lessThanOrEqualTo(GregorianDate.getNumberOfDaysInMonth(validatedMonth)(self))), selfMonthDay => new MInputError.Type({
|
290
|
+
message: `Month ${month} of year ${self.year} does not have ${selfMonthDay} days`
|
291
|
+
})));
|
292
|
+
const ordinalDay = GregorianDate.getMonthOffset(validatedMonth)(self) + monthDay;
|
293
|
+
return pipe(self, MStruct.append({
|
294
|
+
timestamp: self.timestamp + (ordinalDay - self.ordinalDay) * DAY_MS,
|
295
|
+
ordinalDay,
|
296
|
+
month: Option.some(validatedMonth)
|
297
|
+
}), _make);
|
298
|
+
});
|
299
|
+
/**
|
300
|
+
* If possible, returns a new GregorianDate having `monthDay` set to `monthDay` and the same
|
301
|
+
* `year` and `month` as `self`. Returns a left of an error otherwise. `monthDay` must be an
|
302
|
+
* integer greater than or equal to 1 and less than or equal to the number of days in the current
|
303
|
+
* month.
|
304
|
+
*
|
305
|
+
* @category Setters
|
306
|
+
*/
|
307
|
+
GregorianDate.setMonthDay = monthDay => self => Either.gen(function* () {
|
308
|
+
const validatedMonthDay = monthDay <= 28 ? monthDay : yield* pipe(monthDay, MInputError.assertInRange({
|
309
|
+
min: 1,
|
310
|
+
max: GregorianDate.getNumberOfDaysInMonth(GregorianDate.getMonth(self))(self),
|
311
|
+
minIncluded: true,
|
312
|
+
maxIncluded: true,
|
313
|
+
offset: 0,
|
314
|
+
name: "'monthDay'"
|
315
|
+
}));
|
316
|
+
const ordinalDayOffset = validatedMonthDay - GregorianDate.getMonthDay(self);
|
317
|
+
return pipe(self, MStruct.append({
|
318
|
+
timestamp: self.timestamp + ordinalDayOffset * DAY_MS,
|
319
|
+
ordinalDay: self.ordinalDay + ordinalDayOffset,
|
320
|
+
monthDay: Option.some(validatedMonthDay)
|
321
|
+
}), _make);
|
322
|
+
});
|
323
|
+
/**
|
324
|
+
* If possible, returns a new GregorianDate having `ordinalDay` set to `ordinalDay` and the same
|
325
|
+
* `year` as `self`. Returns a left of an error otherwise. `ordinalDay` must be an integer greater
|
326
|
+
* than or equal to 1 and less than or equal to the number of days in the current year
|
327
|
+
*
|
328
|
+
* @category Setters
|
329
|
+
*/
|
330
|
+
GregorianDate.setOrdinalDay = ordinalDay => self => Either.gen(function* () {
|
331
|
+
const validatedOrdinalDay = yield* pipe(ordinalDay, MInputError.assertInRange({
|
332
|
+
min: 1,
|
333
|
+
max: GregorianDate.getYearDurationInDays(self),
|
334
|
+
minIncluded: true,
|
335
|
+
maxIncluded: true,
|
336
|
+
offset: 0,
|
337
|
+
name: "'ordinalDay'"
|
338
|
+
}));
|
339
|
+
return pipe(self, MStruct.append({
|
340
|
+
timestamp: self.timestamp + (validatedOrdinalDay - self.ordinalDay) * DAY_MS,
|
341
|
+
ordinalDay: validatedOrdinalDay,
|
342
|
+
month: Option.none(),
|
343
|
+
monthDay: Option.none()
|
344
|
+
}), _make);
|
345
|
+
});
|
346
|
+
/**
|
347
|
+
* Returns the `timestamp` property of `self`
|
348
|
+
*
|
349
|
+
* @category Destructors
|
350
|
+
*/
|
351
|
+
GregorianDate.timestamp = /*#__PURE__*/Struct.get('timestamp');
|
352
|
+
/**
|
353
|
+
* Returns the `year` property of `self`
|
354
|
+
*
|
355
|
+
* @category Destructors
|
356
|
+
*/
|
357
|
+
GregorianDate.year = /*#__PURE__*/Struct.get('year');
|
358
|
+
/**
|
359
|
+
* Returns the `yearIsLeap` property of `self`
|
360
|
+
*
|
361
|
+
* @category Destructors
|
362
|
+
*/
|
363
|
+
GregorianDate.yearIsLeap = /*#__PURE__*/Struct.get('yearIsLeap');
|
364
|
+
/**
|
365
|
+
* Returns the `yearStartTimestamp` property of `self`
|
366
|
+
*
|
367
|
+
* @category Destructors
|
368
|
+
*/
|
369
|
+
GregorianDate.yearStartTimestamp = /*#__PURE__*/Struct.get('yearStartTimestamp');
|
370
|
+
/**
|
371
|
+
* Returns the `ordinalDay` property of `self`
|
372
|
+
*
|
373
|
+
* @category Destructors
|
374
|
+
*/
|
375
|
+
GregorianDate.ordinalDay = /*#__PURE__*/Struct.get('ordinalDay');
|
376
|
+
/**
|
377
|
+
* Returns the `month` of `self`
|
378
|
+
*
|
379
|
+
* @category Destructors
|
380
|
+
*/
|
381
|
+
GregorianDate.getMonth = self => pipe(self.month, Option.getOrElse(() => {
|
382
|
+
const ordinalDay = self.ordinalDay;
|
383
|
+
const yearIsLeap = self.yearIsLeap;
|
384
|
+
const adjustedOrdinalDay = ordinalDay - (yearIsLeap ? 1 : 0);
|
385
|
+
const result = ordinalDay <= 31 ? 1 : adjustedOrdinalDay <= 59 ? 2 : Math.floor((adjustedOrdinalDay - 59) / 30.6 - 0.018) + 3;
|
386
|
+
/* eslint-disable-next-line functional/immutable-data, functional/no-expression-statements */
|
387
|
+
self.month = Option.some(result);
|
388
|
+
return result;
|
389
|
+
}));
|
390
|
+
/**
|
391
|
+
* Returns the `monthDay` of `self`
|
392
|
+
*
|
393
|
+
* @category Destructors
|
394
|
+
*/
|
395
|
+
GregorianDate.getMonthDay = self => pipe(self.monthDay, Option.getOrElse(() => {
|
396
|
+
const result = self.ordinalDay - GregorianDate.getMonthOffset(GregorianDate.getMonth(self))(self);
|
397
|
+
/* eslint-disable-next-line functional/immutable-data, functional/no-expression-statements */
|
398
|
+
self.monthDay = Option.some(result);
|
399
|
+
return result;
|
400
|
+
}));
|
401
|
+
/**
|
402
|
+
* Returns the duration of the year described by `self` in milliseconds
|
403
|
+
*
|
404
|
+
* @category Destructors
|
405
|
+
*/
|
406
|
+
GregorianDate.getYearDurationInMs = self => self.yearIsLeap ? LEAP_YEAR_MS : COMMON_YEAR_MS;
|
407
|
+
/**
|
408
|
+
* Returns the duration of the year described by `self` in days
|
409
|
+
*
|
410
|
+
* @category Destructors
|
411
|
+
*/
|
412
|
+
GregorianDate.getYearDurationInDays = self => self.yearIsLeap ? 366 : 365;
|
413
|
+
/**
|
414
|
+
* Returns the number of days from the start of the `year` property of `self` to the day before
|
415
|
+
* the first day of month `month`
|
416
|
+
*
|
417
|
+
* @category Destructors
|
418
|
+
*/
|
419
|
+
GregorianDate.getMonthOffset = month => self => month === 1 ? 0 : month === 2 ? 31 : 30 * (month - 1) + Math.floor(0.6 * (month + 1)) - (self.yearIsLeap ? 2 : 3);
|
420
|
+
/**
|
421
|
+
* Returns the number of days of month `month` of the `year` property of `self`
|
422
|
+
*
|
423
|
+
* @category Destructors
|
424
|
+
*/
|
425
|
+
GregorianDate.getNumberOfDaysInMonth = month => flow(Struct.get('_daysInMonth'), MArray.unsafeGet(month - 1));
|
426
|
+
const _formatter = /*#__PURE__*/flow(/*#__PURE__*/CVTemplate.toFormatter(/*#__PURE__*/CVTemplate.make(/*#__PURE__*/_fixedLengthToReal({
|
427
|
+
..._params,
|
428
|
+
name: 'year',
|
429
|
+
length: 4
|
430
|
+
}), _sep.hyphen, /*#__PURE__*/_fixedLengthToReal({
|
431
|
+
..._params,
|
432
|
+
name: 'month',
|
433
|
+
length: 2
|
434
|
+
}), _sep.hyphen, /*#__PURE__*/_fixedLengthToReal({
|
435
|
+
..._params,
|
436
|
+
name: 'monthDay',
|
437
|
+
length: 2
|
438
|
+
}))), /*#__PURE__*/Either.getOrThrowWith(Function.identity));
|
439
|
+
/**
|
440
|
+
* Returns the ISO representation of this Gregorian Date
|
441
|
+
*
|
442
|
+
* @category Destructors
|
443
|
+
*/
|
444
|
+
GregorianDate.getIsoString = /*#__PURE__*/flow(/*#__PURE__*/MStruct.enrichWith({
|
445
|
+
month: GregorianDate.getMonth,
|
446
|
+
monthDay: GregorianDate.getMonthDay
|
447
|
+
}), _formatter);
|
448
|
+
})(GregorianDate || (GregorianDate = {}));
|
449
|
+
/**
|
450
|
+
* Namespace for an IsoDate.
|
451
|
+
*
|
452
|
+
* An iso year starts on the first day of the first iso week. An iso week starts on a monday and
|
453
|
+
* ends on a sunday. The first iso week of the year is the one that contains January 4th (see
|
454
|
+
* Wikipedia).
|
455
|
+
*
|
456
|
+
* @category Models
|
457
|
+
*/
|
458
|
+
var IsoDate;
|
459
|
+
(function (IsoDate) {
|
460
|
+
const _namespaceTag = moduleTag + 'IsoDate/';
|
461
|
+
const _TypeId = /*#__PURE__*/Symbol.for(_namespaceTag);
|
462
|
+
/**
|
463
|
+
* Duration in milliseconds of a 6-iso-year period comprised of 1 long year and 5 short years (see
|
464
|
+
* Wikipedia)
|
465
|
+
*
|
466
|
+
* @category Constants
|
467
|
+
*/
|
468
|
+
const SIX_YEARS_MS = LONG_YEAR_MS + 5 * SHORT_YEAR_MS;
|
469
|
+
/**
|
470
|
+
* Duration in milliseconds of an 11-iso-year period comprised of 2 long years and 9 short years
|
471
|
+
* (see Wikipedia)
|
472
|
+
*
|
473
|
+
* @category Constants
|
474
|
+
*/
|
475
|
+
const ELEVEN_YEARS_MS = 2 * LONG_YEAR_MS + 9 * SHORT_YEAR_MS;
|
476
|
+
/**
|
477
|
+
* Duration in milliseconds of a 28-iso-year period comprised of 5 long years and 23 short years
|
478
|
+
* (see Wikipedia)
|
479
|
+
*
|
480
|
+
* @category Constants
|
481
|
+
*/
|
482
|
+
const TWENTY_EIGHT_YEARS_MS = 5 * LONG_YEAR_MS + 23 * SHORT_YEAR_MS;
|
483
|
+
/**
|
484
|
+
* Duration in milliseconds of a 96-iso-year period comprised of 17 long years and 79 short years
|
485
|
+
* (see Wikipedia)
|
486
|
+
*
|
487
|
+
* @category Constants
|
488
|
+
*/
|
489
|
+
const NINETY_SIX_YEARS_MS = 17 * LONG_YEAR_MS + 79 * SHORT_YEAR_MS;
|
490
|
+
/**
|
491
|
+
* Duration in milliseconds of a 100-iso-year period comprised of 18 long years and 82 short years
|
492
|
+
* (see Wikipedia)
|
493
|
+
*
|
494
|
+
* @category Constants
|
495
|
+
*/
|
496
|
+
const ONE_HUNDRED_YEARS_MS = 18 * LONG_YEAR_MS + 82 * SHORT_YEAR_MS;
|
497
|
+
/**
|
498
|
+
* Duration in milliseconds of a 400-iso-year period comprised of 71 long years and 329 short
|
499
|
+
* years (see Wikipedia)
|
500
|
+
*
|
501
|
+
* @category Constants
|
502
|
+
*/
|
503
|
+
const FOUR_HUNDRED_YEARS_MS = 71 * LONG_YEAR_MS + 329 * SHORT_YEAR_MS;
|
504
|
+
/**
|
505
|
+
* Timestamp of 03/01/2000 00:00:00:000+0:00
|
506
|
+
*
|
507
|
+
* @category Constants
|
508
|
+
*/
|
509
|
+
const YEAR_START_2000_MS = 946_857_600_000;
|
510
|
+
/**
|
511
|
+
* Timestamp of 04/01/2010 00:00:00:000+0:00
|
512
|
+
*
|
513
|
+
* @category Constants
|
514
|
+
*/
|
515
|
+
const YEAR_START_2010_MS = 1_262_563_200_000;
|
516
|
+
/**
|
517
|
+
* Type guard
|
518
|
+
*
|
519
|
+
* @category Guards
|
520
|
+
*/
|
521
|
+
IsoDate.has = u => Predicate.hasProperty(u, _TypeId);
|
522
|
+
/** Proto */
|
523
|
+
const proto = {
|
524
|
+
[_TypeId]: _TypeId,
|
525
|
+
... /*#__PURE__*/MInspectable.BaseProto(_namespaceTag),
|
526
|
+
...MPipeable.BaseProto
|
527
|
+
};
|
528
|
+
/** Constructor */
|
529
|
+
const _make = params => MTypes.objectFromDataAndProto(proto, params);
|
530
|
+
/**
|
531
|
+
* Constructs an IsoDate from a timestamp
|
532
|
+
*
|
533
|
+
* @category Constructors
|
534
|
+
*/
|
535
|
+
IsoDate.fromTimestamp = timestamp => {
|
536
|
+
const offset = timestamp - YEAR_START_2000_MS;
|
537
|
+
const q400Years = Math.floor(offset / FOUR_HUNDRED_YEARS_MS);
|
538
|
+
const r400Years = offset - q400Years * FOUR_HUNDRED_YEARS_MS;
|
539
|
+
// The second one-hundred year period is a week shorter because it has 17 long years instead of 18
|
540
|
+
// Also the hundred-th year must be put in the first one-hundred year period because it is not long
|
541
|
+
const q100Years = r400Years < ONE_HUNDRED_YEARS_MS + SHORT_YEAR_MS ? 0 : Math.floor((r400Years + WEEK_MS) / ONE_HUNDRED_YEARS_MS);
|
542
|
+
const adjustedR400Years = r400Years - q100Years * NINETY_SIX_YEARS_MS + SHORT_YEAR_MS;
|
543
|
+
const q28Years = Math.floor(adjustedR400Years / TWENTY_EIGHT_YEARS_MS);
|
544
|
+
const r28Years = adjustedR400Years - q28Years * TWENTY_EIGHT_YEARS_MS;
|
545
|
+
const adjustedR28Years = r28Years - ELEVEN_YEARS_MS;
|
546
|
+
const q11Years = Math.floor(adjustedR28Years / ELEVEN_YEARS_MS);
|
547
|
+
const r11Years = adjustedR28Years - q11Years * ELEVEN_YEARS_MS;
|
548
|
+
const q6Years = Math.floor(r11Years / SIX_YEARS_MS);
|
549
|
+
const r6Years = r11Years - q6Years * SIX_YEARS_MS;
|
550
|
+
const isFirstSixYearPeriod = q6Years === 0;
|
551
|
+
const q1Year = Math.min(Math.floor(r6Years / SHORT_YEAR_MS), isFirstSixYearPeriod ? 5 : 4);
|
552
|
+
//console.log(q400Years, q100Years, q28Years, q11Years, q6Years, q1Year);
|
553
|
+
return _make({
|
554
|
+
timestamp,
|
555
|
+
year: 2010 + q400Years * 400 + q100Years * 96 + q28Years * 28 + q11Years * 11 + q6Years * 6 + q1Year,
|
556
|
+
yearStartTimestamp: YEAR_START_2010_MS + q400Years * FOUR_HUNDRED_YEARS_MS + q100Years * NINETY_SIX_YEARS_MS + q28Years * TWENTY_EIGHT_YEARS_MS + q11Years * ELEVEN_YEARS_MS + q6Years * SIX_YEARS_MS + q1Year * SHORT_YEAR_MS,
|
557
|
+
yearIsLong: isFirstSixYearPeriod && q1Year == 5 || !isFirstSixYearPeriod && q1Year == 4,
|
558
|
+
isoWeek: Option.none(),
|
559
|
+
weekday: Option.none()
|
560
|
+
});
|
561
|
+
};
|
562
|
+
/**
|
563
|
+
* Constructs an IsoDate from a GregorianDate
|
564
|
+
*
|
565
|
+
* @category Constructors
|
566
|
+
*/
|
567
|
+
IsoDate.fromGregorianDate = gregorianDate => {
|
568
|
+
// 0 is friday, 6 is thursday
|
569
|
+
const yearStartWeekday = MNumber.intModulo(7)(Math.floor((gregorianDate.yearStartTimestamp - DAY_MS) / DAY_MS));
|
570
|
+
const yearIsLeap = gregorianDate.yearIsLeap;
|
571
|
+
const minOrdinalDayIndex = 3 - yearStartWeekday;
|
572
|
+
const ordinalDay = gregorianDate.ordinalDay;
|
573
|
+
if (ordinalDay <= minOrdinalDayIndex) {
|
574
|
+
const year = gregorianDate.year - 1;
|
575
|
+
const yearIsLong = yearStartWeekday === 0 || yearStartWeekday === 1 && !yearIsLeap && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
|
576
|
+
return _make({
|
577
|
+
timestamp: gregorianDate.timestamp,
|
578
|
+
year,
|
579
|
+
yearStartTimestamp: gregorianDate.yearStartTimestamp + (minOrdinalDayIndex - (yearIsLong ? 371 : 364)) * DAY_MS,
|
580
|
+
yearIsLong,
|
581
|
+
isoWeek: Option.none(),
|
582
|
+
weekday: Option.none()
|
583
|
+
});
|
584
|
+
}
|
585
|
+
const yearIsLong = yearStartWeekday === 6 || yearStartWeekday === 5 && yearIsLeap;
|
586
|
+
const maxOrdinalDay = minOrdinalDayIndex + (yearIsLong ? 371 : 364);
|
587
|
+
if (ordinalDay > maxOrdinalDay) {
|
588
|
+
const year = gregorianDate.year + 1;
|
589
|
+
const yearIsLong = yearIsLeap ? yearStartWeekday === 4 : yearStartWeekday === 5 || yearStartWeekday === 4 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
|
590
|
+
return _make({
|
591
|
+
timestamp: gregorianDate.timestamp,
|
592
|
+
year,
|
593
|
+
yearStartTimestamp: gregorianDate.yearStartTimestamp + maxOrdinalDay * DAY_MS,
|
594
|
+
yearIsLong,
|
595
|
+
isoWeek: Option.none(),
|
596
|
+
weekday: Option.none()
|
597
|
+
});
|
598
|
+
}
|
599
|
+
return _make({
|
600
|
+
timestamp: gregorianDate.timestamp,
|
601
|
+
year: gregorianDate.year,
|
602
|
+
yearStartTimestamp: gregorianDate.yearStartTimestamp + minOrdinalDayIndex * DAY_MS,
|
603
|
+
yearIsLong,
|
604
|
+
isoWeek: Option.none(),
|
605
|
+
weekday: Option.none()
|
606
|
+
});
|
607
|
+
};
|
608
|
+
/**
|
609
|
+
* If possible, returns a new IsoDate having `year` set to `year` and the same `isoWeek` and
|
610
|
+
* `weekday` as `self`. Returns a left of an error otherwise. `year` must be an integer comprised
|
611
|
+
* in the range [MIN_FULL_YEAR, MAX_FULL_YEAR]. If the isoWeek of `self` is equal to 53, `year`
|
612
|
+
* must be a long year.
|
613
|
+
*
|
614
|
+
* @category Setters
|
615
|
+
*/
|
616
|
+
IsoDate.setYear = year => self => Either.gen(function* () {
|
617
|
+
const validatedYear = yield* pipe(year, MInputError.assertInRange({
|
618
|
+
min: MIN_FULL_YEAR,
|
619
|
+
max: MAX_FULL_YEAR,
|
620
|
+
minIncluded: true,
|
621
|
+
maxIncluded: true,
|
622
|
+
offset: 0,
|
623
|
+
name: "'year'"
|
624
|
+
}));
|
625
|
+
const offset = validatedYear - 2000;
|
626
|
+
const q400Years = Math.floor(offset / 400);
|
627
|
+
const r400Years = offset - q400Years * 400;
|
628
|
+
// year 100 needs to be treated in the first one-hundred year period because it is not a long year
|
629
|
+
const q100Years = r400Years === 100 ? 0 : Math.floor(r400Years / 100);
|
630
|
+
const adjustedR400Years = r400Years - q100Years * 96 + 1;
|
631
|
+
const q28Years = Math.floor(adjustedR400Years / 28);
|
632
|
+
const r28Years = adjustedR400Years - q28Years * 28;
|
633
|
+
const adjustedR28Years = r28Years - 11;
|
634
|
+
const q11Years = Math.floor(adjustedR28Years / 11);
|
635
|
+
const r11Years = adjustedR28Years - q11Years * 11;
|
636
|
+
const yearStartTimestamp = YEAR_START_2010_MS + q400Years * FOUR_HUNDRED_YEARS_MS + q100Years * NINETY_SIX_YEARS_MS + q28Years * TWENTY_EIGHT_YEARS_MS + q11Years * ELEVEN_YEARS_MS + r11Years * SHORT_YEAR_MS + (r11Years > 5 ? WEEK_MS : 0);
|
637
|
+
return yield* pipe(_make({
|
638
|
+
timestamp: self.timestamp + yearStartTimestamp - self.yearStartTimestamp,
|
639
|
+
year: validatedYear,
|
640
|
+
yearStartTimestamp,
|
641
|
+
yearIsLong: r11Years === 5 || r11Years === 10,
|
642
|
+
isoWeek: Option.some(IsoDate.getIsoWeek(self)),
|
643
|
+
weekday: Option.some(IsoDate.getWeekday(self))
|
644
|
+
}), Either.liftPredicate(Predicate.or(IsoDate.yearIsLong, flow(IsoDate.getIsoWeek, Number.lessThan(53))), () => new MInputError.Type({
|
645
|
+
message: `No 53rd week on iso year ${year} which is not a short year`
|
646
|
+
})));
|
647
|
+
});
|
648
|
+
/**
|
649
|
+
* If possible, returns a new IsoDate having `isoWeek` set to `isoWeek` and the same `year` and
|
650
|
+
* `weekday` as `self`. Returns a left of an error otherwise. `isoWeek` must be an integer greater
|
651
|
+
* than or equal to 1 and less than or equal to the number of iso weeks in the current year.
|
652
|
+
*
|
653
|
+
* @category Setters
|
654
|
+
*/
|
655
|
+
IsoDate.setIsoWeek = isoWeek => self => Either.gen(function* () {
|
656
|
+
const validatedIsoWeek = yield* pipe(isoWeek, MInputError.assertInRange({
|
657
|
+
min: 1,
|
658
|
+
max: IsoDate.getLastIsoWeek(self),
|
659
|
+
minIncluded: true,
|
660
|
+
maxIncluded: true,
|
661
|
+
offset: 0,
|
662
|
+
name: "'isoWeek'"
|
663
|
+
}));
|
664
|
+
const offset = validatedIsoWeek - IsoDate.getIsoWeek(self);
|
665
|
+
return pipe(self, MStruct.evolve({
|
666
|
+
timestamp: Number.sum(offset * WEEK_MS),
|
667
|
+
isoWeek: pipe(validatedIsoWeek, Option.some, Function.constant)
|
668
|
+
}), _make);
|
669
|
+
});
|
670
|
+
/**
|
671
|
+
* If possible, returns a new IsoDate having `weekday` set to `weekday` and the same `year` and
|
672
|
+
* `isoWeek` as `self`. Returns a left of an error otherwise. `weekday` must be an integer greater
|
673
|
+
* than or equal to 1 (monday) and less than or equal to 7 (sunday).
|
674
|
+
*
|
675
|
+
* @category Setters
|
676
|
+
*/
|
677
|
+
IsoDate.setWeekday = weekday => self => Either.gen(function* () {
|
678
|
+
const validatedWeekday = yield* pipe(weekday, MInputError.assertInRange({
|
679
|
+
min: 1,
|
680
|
+
max: 7,
|
681
|
+
minIncluded: true,
|
682
|
+
maxIncluded: true,
|
683
|
+
offset: 0,
|
684
|
+
name: "'weekday'"
|
685
|
+
}));
|
686
|
+
const offset = validatedWeekday - IsoDate.getWeekday(self);
|
687
|
+
return pipe(self, MStruct.evolve({
|
688
|
+
timestamp: Number.sum(offset * DAY_MS),
|
689
|
+
weekday: pipe(validatedWeekday, Option.some, Function.constant)
|
690
|
+
}), _make);
|
691
|
+
});
|
692
|
+
/**
|
693
|
+
* Returns the `timestamp` property of `self`
|
694
|
+
*
|
695
|
+
* @category Destructors
|
696
|
+
*/
|
697
|
+
IsoDate.timestamp = /*#__PURE__*/Struct.get('timestamp');
|
698
|
+
/**
|
699
|
+
* Returns the `year` property of `self`
|
700
|
+
*
|
701
|
+
* @category Destructors
|
702
|
+
*/
|
703
|
+
IsoDate.year = /*#__PURE__*/Struct.get('year');
|
704
|
+
/**
|
705
|
+
* Returns the `yearStartTimestamp` property of `self`
|
706
|
+
*
|
707
|
+
* @category Destructors
|
708
|
+
*/
|
709
|
+
IsoDate.yearStartTimestamp = /*#__PURE__*/Struct.get('yearStartTimestamp');
|
710
|
+
/**
|
711
|
+
* Returns the `yearIsLong` property of `self`
|
712
|
+
*
|
713
|
+
* @category Destructors
|
714
|
+
*/
|
715
|
+
IsoDate.yearIsLong = /*#__PURE__*/Struct.get('yearIsLong');
|
716
|
+
/**
|
717
|
+
* Returns the `isoWeek` of `self`
|
718
|
+
*
|
719
|
+
* @category Destructors
|
720
|
+
*/
|
721
|
+
IsoDate.getIsoWeek = self => pipe(self.isoWeek, Option.getOrElse(() => {
|
722
|
+
const result = Math.floor((self.timestamp - self.yearStartTimestamp) / WEEK_MS) + 1;
|
723
|
+
/* eslint-disable-next-line functional/immutable-data, functional/no-expression-statements */
|
724
|
+
self.isoWeek = Option.some(result);
|
725
|
+
return result;
|
726
|
+
}));
|
727
|
+
/**
|
728
|
+
* Returns the `weekday` of `self`
|
729
|
+
*
|
730
|
+
* @category Destructors
|
731
|
+
*/
|
732
|
+
IsoDate.getWeekday = self => pipe(self.weekday, Option.getOrElse(() => {
|
733
|
+
const result = Math.floor((self.timestamp - self.yearStartTimestamp - (IsoDate.getIsoWeek(self) - 1) * WEEK_MS) / DAY_MS) + 1;
|
734
|
+
/* eslint-disable-next-line functional/immutable-data, functional/no-expression-statements */
|
735
|
+
self.weekday = Option.some(result);
|
736
|
+
return result;
|
737
|
+
}));
|
738
|
+
/**
|
739
|
+
* Returns the duration of the year described by `self` in milliseconds
|
740
|
+
*
|
741
|
+
* @category Destructors
|
742
|
+
*/
|
743
|
+
IsoDate.getMsDuration = self => self.yearIsLong ? LONG_YEAR_MS : SHORT_YEAR_MS;
|
744
|
+
/**
|
745
|
+
* Returns the duration of the year described by `self` in milliseconds
|
746
|
+
*
|
747
|
+
* @category Destructors
|
748
|
+
*/
|
749
|
+
IsoDate.getLastIsoWeek = self => self.yearIsLong ? 53 : 52;
|
750
|
+
const _formatter = /*#__PURE__*/flow(/*#__PURE__*/CVTemplate.toFormatter(/*#__PURE__*/CVTemplate.make(/*#__PURE__*/_fixedLengthToReal({
|
751
|
+
..._params,
|
752
|
+
name: 'year',
|
753
|
+
length: 4
|
754
|
+
}), /*#__PURE__*/_sep.make('-W'), /*#__PURE__*/_fixedLengthToReal({
|
755
|
+
..._params,
|
756
|
+
name: 'isoWeek',
|
757
|
+
length: 2
|
758
|
+
}), _sep.hyphen, /*#__PURE__*/_fixedLengthToReal({
|
759
|
+
..._params,
|
760
|
+
name: 'weekday',
|
761
|
+
length: 2
|
762
|
+
}))), /*#__PURE__*/Either.getOrThrowWith(Function.identity));
|
763
|
+
/**
|
764
|
+
* Returns the ISO representation of this Gregorian Date
|
765
|
+
*
|
766
|
+
* @category Destructors
|
767
|
+
*/
|
768
|
+
IsoDate.getIsoString = /*#__PURE__*/flow(/*#__PURE__*/MStruct.enrichWith({
|
769
|
+
isoWeek: IsoDate.getIsoWeek,
|
770
|
+
weekday: IsoDate.getWeekday
|
771
|
+
}), _formatter);
|
772
|
+
})(IsoDate || (IsoDate = {}));
|
773
|
+
/**
|
774
|
+
* Namespace for the data relative to the time
|
775
|
+
*
|
776
|
+
* @category Models
|
777
|
+
*/
|
778
|
+
var Time;
|
779
|
+
(function (Time) {
|
780
|
+
const _namespaceTag = moduleTag + 'Time/';
|
781
|
+
const _TypeId = /*#__PURE__*/Symbol.for(_namespaceTag);
|
782
|
+
/**
|
783
|
+
* Type guard
|
784
|
+
*
|
785
|
+
* @category Guards
|
786
|
+
*/
|
787
|
+
Time.has = u => Predicate.hasProperty(u, _TypeId);
|
788
|
+
/** Proto */
|
789
|
+
const proto = {
|
790
|
+
[_TypeId]: _TypeId,
|
791
|
+
... /*#__PURE__*/MInspectable.BaseProto(_namespaceTag),
|
792
|
+
...MPipeable.BaseProto
|
793
|
+
};
|
794
|
+
/** Constructor */
|
795
|
+
const _make = params => MTypes.objectFromDataAndProto(proto, params);
|
796
|
+
/**
|
797
|
+
* Constructs the Time that corresponds to the passed `timestampOffset` which is the number of
|
798
|
+
* milliseconds from the start of the current day
|
799
|
+
*
|
800
|
+
* @category Constructors
|
801
|
+
*/
|
802
|
+
Time.fromTimestamp = timestampOffset => {
|
803
|
+
const hour23 = Math.floor(timestampOffset / HOUR_MS);
|
804
|
+
const rHour23 = timestampOffset - hour23 * HOUR_MS;
|
805
|
+
const [hour11, meridiem] = hour23 >= 12 ? [hour23 - 12, 12] : [hour23, 0];
|
806
|
+
const minute = Math.floor(rHour23 / MINUTE_MS);
|
807
|
+
const rMinute = rHour23 - minute * MINUTE_MS;
|
808
|
+
const second = Math.floor(rMinute / SECOND_MS);
|
809
|
+
return _make({
|
810
|
+
timestampOffset,
|
811
|
+
hour23,
|
812
|
+
hour11,
|
813
|
+
meridiem,
|
814
|
+
minute,
|
815
|
+
second,
|
816
|
+
millisecond: rMinute - second * SECOND_MS
|
817
|
+
});
|
818
|
+
};
|
819
|
+
/**
|
820
|
+
* If possible, returns a right of a copy of `self` with `hour23` set to `hour23`. Returns a left
|
821
|
+
* of an error otherwise. `hour23` must be an integer greater than or equal to 0 and less than or
|
822
|
+
* equal to 23
|
823
|
+
*
|
824
|
+
* @category Setters
|
825
|
+
*/
|
826
|
+
Time.setHour23 = hour23 => self => Either.gen(function* () {
|
827
|
+
const validatedHour23 = yield* pipe(hour23, MInputError.assertInRange({
|
828
|
+
min: 0,
|
829
|
+
max: 23,
|
830
|
+
minIncluded: true,
|
831
|
+
maxIncluded: true,
|
832
|
+
offset: 0,
|
833
|
+
name: "'hour23'"
|
834
|
+
}));
|
835
|
+
const isPast12 = validatedHour23 >= 12;
|
836
|
+
return _make({
|
837
|
+
...self,
|
838
|
+
timestampOffset: self.timestampOffset + (validatedHour23 - self.hour23) * HOUR_MS,
|
839
|
+
hour23: validatedHour23,
|
840
|
+
hour11: isPast12 ? hour23 - 12 : hour23,
|
841
|
+
meridiem: isPast12 ? 12 : 0
|
842
|
+
});
|
843
|
+
});
|
844
|
+
/**
|
845
|
+
* If possible, returns a right of a copy of `self` with `hour11` set to `hour11`. Returns a left
|
846
|
+
* of an error otherwise. `hour11` must be an integer greater than or equal to 0 and less than or
|
847
|
+
* equal to 11
|
848
|
+
*
|
849
|
+
* @category Setters
|
850
|
+
*/
|
851
|
+
Time.setHour11 = hour11 => self => Either.gen(function* () {
|
852
|
+
const validatedHour11 = yield* pipe(hour11, MInputError.assertInRange({
|
853
|
+
min: 0,
|
854
|
+
max: 11,
|
855
|
+
minIncluded: true,
|
856
|
+
maxIncluded: true,
|
857
|
+
offset: 0,
|
858
|
+
name: "'hour11'"
|
859
|
+
}));
|
860
|
+
const validatedHour23 = self.meridiem + validatedHour11;
|
861
|
+
return _make({
|
862
|
+
...self,
|
863
|
+
timestampOffset: self.timestampOffset + (validatedHour23 - self.hour23) * HOUR_MS,
|
864
|
+
hour23: validatedHour23,
|
865
|
+
hour11: validatedHour11
|
866
|
+
});
|
867
|
+
});
|
868
|
+
/**
|
869
|
+
* Returns a copy of `self` with `meridiem` set to `merdiem`.
|
870
|
+
*
|
871
|
+
* @category Setters
|
872
|
+
*/
|
873
|
+
Time.setMeridiem = meridiem => self => {
|
874
|
+
const validatedHour23 = self.hour11 + meridiem;
|
875
|
+
return _make({
|
876
|
+
...self,
|
877
|
+
timestampOffset: self.timestampOffset + (validatedHour23 - self.hour23) * HOUR_MS,
|
878
|
+
hour23: validatedHour23,
|
879
|
+
meridiem
|
880
|
+
});
|
881
|
+
};
|
882
|
+
/**
|
883
|
+
* If possible, returns a right of a copy of `self` with `minute` set to `minute`. Returns a left
|
884
|
+
* of an error otherwise. `minute` must be an integer greater than or equal to 0 and less than or
|
885
|
+
* equal to 59
|
886
|
+
*
|
887
|
+
* @category Setters
|
888
|
+
*/
|
889
|
+
Time.setMinute = minute => self => Either.gen(function* () {
|
890
|
+
const validatedMinute = yield* pipe(minute, MInputError.assertInRange({
|
891
|
+
min: 0,
|
892
|
+
max: 59,
|
893
|
+
minIncluded: true,
|
894
|
+
maxIncluded: true,
|
895
|
+
offset: 0,
|
896
|
+
name: "'minute'"
|
897
|
+
}));
|
898
|
+
return _make({
|
899
|
+
...self,
|
900
|
+
timestampOffset: self.timestampOffset + (validatedMinute - self.minute) * MINUTE_MS,
|
901
|
+
minute: validatedMinute
|
902
|
+
});
|
903
|
+
});
|
904
|
+
/**
|
905
|
+
* If possible, returns a right of a copy of `self` with `second` set to `second`. Returns a left
|
906
|
+
* of an error otherwise. `second` must be an integer greater than or equal to 0 and less than or
|
907
|
+
* equal to 59
|
908
|
+
*
|
909
|
+
* @category Setters
|
910
|
+
*/
|
911
|
+
Time.setSecond = second => self => Either.gen(function* () {
|
912
|
+
const validatedSecond = yield* pipe(second, MInputError.assertInRange({
|
913
|
+
min: 0,
|
914
|
+
max: 59,
|
915
|
+
minIncluded: true,
|
916
|
+
maxIncluded: true,
|
917
|
+
offset: 0,
|
918
|
+
name: "'second'"
|
919
|
+
}));
|
920
|
+
return _make({
|
921
|
+
...self,
|
922
|
+
timestampOffset: self.timestampOffset + (validatedSecond - self.second) * SECOND_MS,
|
923
|
+
second: validatedSecond
|
924
|
+
});
|
925
|
+
});
|
926
|
+
/**
|
927
|
+
* If possible, returns a right of a copy of `self` with `millisecond` set to `millisecond`.
|
928
|
+
* Returns a left of an error otherwise. `millisecond` must be an integer greater than or equal to
|
929
|
+
* 0 and less than or equal to 999
|
930
|
+
*
|
931
|
+
* @category Setters
|
932
|
+
*/
|
933
|
+
Time.setMillisecond = millisecond => self => Either.gen(function* () {
|
934
|
+
const validatedMillisecond = yield* pipe(millisecond, MInputError.assertInRange({
|
935
|
+
min: 0,
|
936
|
+
max: 999,
|
937
|
+
minIncluded: true,
|
938
|
+
maxIncluded: true,
|
939
|
+
offset: 0,
|
940
|
+
name: "'millisecond'"
|
941
|
+
}));
|
942
|
+
return _make({
|
943
|
+
...self,
|
944
|
+
timestampOffset: self.timestampOffset + validatedMillisecond - self.millisecond,
|
945
|
+
millisecond: validatedMillisecond
|
946
|
+
});
|
947
|
+
});
|
948
|
+
/**
|
949
|
+
* Returns the `timestampOffset` property of `self`
|
950
|
+
*
|
951
|
+
* @category Destructors
|
952
|
+
*/
|
953
|
+
Time.timestampOffset = /*#__PURE__*/Struct.get('timestampOffset');
|
954
|
+
/**
|
955
|
+
* Returns the `hour23` property of `self`
|
956
|
+
*
|
957
|
+
* @category Destructors
|
958
|
+
*/
|
959
|
+
Time.hour23 = /*#__PURE__*/Struct.get('hour23');
|
960
|
+
/**
|
961
|
+
* Returns the `hour11` property of `self`
|
962
|
+
*
|
963
|
+
* @category Destructors
|
964
|
+
*/
|
965
|
+
Time.hour11 = /*#__PURE__*/Struct.get('hour11');
|
966
|
+
/**
|
967
|
+
* Returns the `meridiem` property of `self`
|
968
|
+
*
|
969
|
+
* @category Destructors
|
970
|
+
*/
|
971
|
+
Time.meridiem = /*#__PURE__*/Struct.get('meridiem');
|
972
|
+
/**
|
973
|
+
* Returns the `minute` property of `self`
|
974
|
+
*
|
975
|
+
* @category Destructors
|
976
|
+
*/
|
977
|
+
Time.minute = /*#__PURE__*/Struct.get('minute');
|
978
|
+
/**
|
979
|
+
* Returns the `second` property of `self`
|
980
|
+
*
|
981
|
+
* @category Destructors
|
982
|
+
*/
|
983
|
+
Time.second = /*#__PURE__*/Struct.get('second');
|
984
|
+
/**
|
985
|
+
* Returns the `millisecond` property of `self`
|
986
|
+
*
|
987
|
+
* @category Destructors
|
988
|
+
*/
|
989
|
+
Time.millisecond = /*#__PURE__*/Struct.get('millisecond');
|
990
|
+
const _formatter = /*#__PURE__*/flow(/*#__PURE__*/CVTemplate.toFormatter(/*#__PURE__*/CVTemplate.make(/*#__PURE__*/_fixedLengthToReal({
|
991
|
+
..._params,
|
992
|
+
name: 'hour23',
|
993
|
+
length: 2
|
994
|
+
}), _sep.colon, /*#__PURE__*/_fixedLengthToReal({
|
995
|
+
..._params,
|
996
|
+
name: 'minute',
|
997
|
+
length: 2
|
998
|
+
}), _sep.colon, /*#__PURE__*/_fixedLengthToReal({
|
999
|
+
..._params,
|
1000
|
+
name: 'second',
|
1001
|
+
length: 2
|
1002
|
+
}), _sep.dot, /*#__PURE__*/_fixedLengthToReal({
|
1003
|
+
..._params,
|
1004
|
+
name: 'millisecond',
|
1005
|
+
length: 3
|
1006
|
+
}))), /*#__PURE__*/Either.getOrThrowWith(Function.identity));
|
1007
|
+
/**
|
1008
|
+
* Returns the ISO representation of this Gregorian Date
|
1009
|
+
*
|
1010
|
+
* @category Destructors
|
1011
|
+
*/
|
1012
|
+
Time.getIsoString = _formatter;
|
1013
|
+
})(Time || (Time = {}));
|
1014
|
+
/**
|
1015
|
+
* Namespace for the data relative to the parts of a zone offset
|
1016
|
+
*
|
1017
|
+
* @category Models
|
1018
|
+
*/
|
1019
|
+
var ZoneOffsetParts;
|
1020
|
+
(function (ZoneOffsetParts) {
|
1021
|
+
const _namespaceTag = moduleTag + 'ZoneOffsetParts/';
|
1022
|
+
const _TypeId = /*#__PURE__*/Symbol.for(_namespaceTag);
|
1023
|
+
/**
|
1024
|
+
* Type guard
|
1025
|
+
*
|
1026
|
+
* @category Guards
|
1027
|
+
*/
|
1028
|
+
ZoneOffsetParts.has = u => Predicate.hasProperty(u, _TypeId);
|
1029
|
+
/** Proto */
|
1030
|
+
const proto = {
|
1031
|
+
[_TypeId]: _TypeId,
|
1032
|
+
... /*#__PURE__*/MInspectable.BaseProto(_namespaceTag),
|
1033
|
+
...MPipeable.BaseProto
|
1034
|
+
};
|
1035
|
+
ZoneOffsetParts._make = params => MTypes.objectFromDataAndProto(proto, params);
|
1036
|
+
/**
|
1037
|
+
* Builds a ZoneOffsetParts from `zoneOffset`
|
1038
|
+
*
|
1039
|
+
* @category Constructors
|
1040
|
+
*/
|
1041
|
+
ZoneOffsetParts.fromZoneOffset = zoneOffset => {
|
1042
|
+
const zoneHour = Math.trunc(zoneOffset);
|
1043
|
+
const minutesSeconds = Math.abs(zoneOffset - zoneHour) * 60;
|
1044
|
+
const zoneMinute = Math.trunc(minutesSeconds);
|
1045
|
+
const zoneSecond = Math.trunc((minutesSeconds - zoneMinute) * 60);
|
1046
|
+
return ZoneOffsetParts._make({
|
1047
|
+
zoneHour,
|
1048
|
+
zoneMinute,
|
1049
|
+
zoneSecond
|
1050
|
+
});
|
1051
|
+
};
|
1052
|
+
/**
|
1053
|
+
* Tries to build a ZoneOffsetParts from `zoneHour`, `zoneMinute`, `zoneSecond`. Returns a `some`
|
1054
|
+
* if successful. A `none` otherwise.
|
1055
|
+
*
|
1056
|
+
* - `zoneHour` must be greater than or equal to -12 and less than or equal to 14.
|
1057
|
+
* - `zoneMinute` must be greater than or equal to 0 and less than or equal to 59.
|
1058
|
+
* - `zoneSecond` must be greater than or equal to 0 and less than or equal to 59.
|
1059
|
+
*
|
1060
|
+
* @category Constructors
|
1061
|
+
*/
|
1062
|
+
ZoneOffsetParts.fromParts = ({
|
1063
|
+
zoneHour,
|
1064
|
+
zoneMinute,
|
1065
|
+
zoneSecond
|
1066
|
+
}) => Either.gen(function* () {
|
1067
|
+
const validatedHour = yield* pipe(zoneHour, MInputError.assertInRange({
|
1068
|
+
min: -12,
|
1069
|
+
max: 14,
|
1070
|
+
minIncluded: true,
|
1071
|
+
maxIncluded: true,
|
1072
|
+
offset: 0,
|
1073
|
+
name: "'zoneHour'"
|
1074
|
+
}));
|
1075
|
+
const validatedMinute = yield* pipe(zoneMinute, MInputError.assertInRange({
|
1076
|
+
min: 0,
|
1077
|
+
max: 59,
|
1078
|
+
minIncluded: true,
|
1079
|
+
maxIncluded: true,
|
1080
|
+
offset: 0,
|
1081
|
+
name: "'zoneMinute'"
|
1082
|
+
}));
|
1083
|
+
const validatedSecond = yield* pipe(zoneSecond, MInputError.assertInRange({
|
1084
|
+
min: 0,
|
1085
|
+
max: 59,
|
1086
|
+
minIncluded: true,
|
1087
|
+
maxIncluded: true,
|
1088
|
+
offset: 0,
|
1089
|
+
name: "'zoneSecond'"
|
1090
|
+
}));
|
1091
|
+
return ZoneOffsetParts._make({
|
1092
|
+
zoneHour: validatedHour,
|
1093
|
+
zoneMinute: validatedMinute,
|
1094
|
+
zoneSecond: validatedSecond
|
1095
|
+
});
|
1096
|
+
});
|
1097
|
+
/**
|
1098
|
+
* Returns the `zoneHour` property of `self`
|
1099
|
+
*
|
1100
|
+
* @category Destructors
|
1101
|
+
*/
|
1102
|
+
ZoneOffsetParts.zoneHour = /*#__PURE__*/Struct.get('zoneHour');
|
1103
|
+
/**
|
1104
|
+
* Returns the `zoneMinute` property of `self`
|
1105
|
+
*
|
1106
|
+
* @category Destructors
|
1107
|
+
*/
|
1108
|
+
ZoneOffsetParts.zoneMinute = /*#__PURE__*/Struct.get('zoneMinute');
|
1109
|
+
/**
|
1110
|
+
* Returns the `zoneSecond` property of `self`
|
1111
|
+
*
|
1112
|
+
* @category Destructors
|
1113
|
+
*/
|
1114
|
+
ZoneOffsetParts.zoneSecond = /*#__PURE__*/Struct.get('zoneSecond');
|
1115
|
+
const _formatter = /*#__PURE__*/flow(/*#__PURE__*/CVTemplate.toFormatter(/*#__PURE__*/CVTemplate.make(/*#__PURE__*/_fixedLengthToReal({
|
1116
|
+
..._params,
|
1117
|
+
name: 'zoneHour',
|
1118
|
+
length: 3,
|
1119
|
+
numberBase10Format: /*#__PURE__*/pipe(_integer, CVNumberBase10Format.withSignDisplay)
|
1120
|
+
}), _sep.colon, /*#__PURE__*/_fixedLengthToReal({
|
1121
|
+
..._params,
|
1122
|
+
name: 'zoneMinute',
|
1123
|
+
length: 2
|
1124
|
+
}))), /*#__PURE__*/Either.getOrThrowWith(Function.identity));
|
1125
|
+
/**
|
1126
|
+
* Returns the ISO representation of this Gregorian Date
|
1127
|
+
*
|
1128
|
+
* @category Destructors
|
1129
|
+
*/
|
1130
|
+
ZoneOffsetParts.getIsoString = _formatter;
|
1131
|
+
/**
|
1132
|
+
* Returns the value of `self` expressed in hours
|
1133
|
+
*
|
1134
|
+
* @category Destructors
|
1135
|
+
*/
|
1136
|
+
ZoneOffsetParts.toHour = self => {
|
1137
|
+
const hour = self.zoneHour;
|
1138
|
+
const sign = MNumber.sign2(hour);
|
1139
|
+
return hour + sign * (self.zoneMinute / 60 + self.zoneSecond / 3600);
|
1140
|
+
};
|
1141
|
+
})(ZoneOffsetParts || (ZoneOffsetParts = {}));
|
1142
|
+
/**
|
1143
|
+
* Type guard
|
1144
|
+
*
|
1145
|
+
* @category Guards
|
1146
|
+
*/
|
1147
|
+
export const has = u => Predicate.hasProperty(u, _TypeId);
|
1148
|
+
/**
|
1149
|
+
* Equivalence
|
1150
|
+
*
|
1151
|
+
* @category Equivalences
|
1152
|
+
*/
|
1153
|
+
export const equivalence = (self, that) => self.timestamp === that.timestamp;
|
1154
|
+
/** Proto */
|
1155
|
+
const _TypeIdHash = /*#__PURE__*/Hash.hash(_TypeId);
|
1156
|
+
const proto = {
|
1157
|
+
[_TypeId]: _TypeId,
|
1158
|
+
[Equal.symbol](that) {
|
1159
|
+
return has(that) && equivalence(this, that);
|
1160
|
+
},
|
1161
|
+
[Hash.symbol]() {
|
1162
|
+
return pipe(this.timestamp, Hash.hash, Hash.combine(_TypeIdHash), Hash.cached(this));
|
1163
|
+
},
|
1164
|
+
[MInspectable.IdSymbol]() {
|
1165
|
+
return getIsoString(this);
|
1166
|
+
},
|
1167
|
+
... /*#__PURE__*/MInspectable.BaseProto(moduleTag),
|
1168
|
+
...MPipeable.BaseProto
|
1169
|
+
};
|
1170
|
+
/** Constructor */
|
1171
|
+
const _make = params => MTypes.objectFromDataAndProto(proto, params);
|
1172
|
+
/**
|
1173
|
+
* Returns the ISO representation of this DateTime
|
1174
|
+
*
|
1175
|
+
* @category Destructors
|
1176
|
+
*/
|
1177
|
+
export const getIsoString = self => GregorianDate.getIsoString(_gregorianDate(self)) + 'T' + Time.getIsoString(_time(self)) + ZoneOffsetParts.getIsoString(_zoneOffsetParts(self));
|
1178
|
+
const _uncalculated = {
|
1179
|
+
gregorianDate: /*#__PURE__*/Option.none(),
|
1180
|
+
isoDate: /*#__PURE__*/Option.none(),
|
1181
|
+
time: /*#__PURE__*/Option.none(),
|
1182
|
+
zoneOffsetParts: /*#__PURE__*/Option.none()
|
1183
|
+
};
|
1184
|
+
/**
|
1185
|
+
* Constructor that creates a DateTime from a timestamp and a zoneOffset for which no calculations
|
1186
|
+
* have been carried out yet. The `_zonedTimestamp` field is automatically calculated. Does not
|
1187
|
+
* check any input parameters
|
1188
|
+
*/
|
1189
|
+
const _uncalculatedFromTimestamp = (timestamp, zoneOffset) => _make({
|
1190
|
+
..._uncalculated,
|
1191
|
+
timestamp,
|
1192
|
+
zoneOffset,
|
1193
|
+
_zonedTimestamp: timestamp + zoneOffset * HOUR_MS
|
1194
|
+
});
|
1195
|
+
/**
|
1196
|
+
* Constructor that creates a DateTime from a zonedTimestamp and a zoneOffset for which no
|
1197
|
+
* calculations have been carried out yet. The `timestamp` field is automatically calculated. Does
|
1198
|
+
* not check any input parameters
|
1199
|
+
*/
|
1200
|
+
const _uncalculatedFromZonedTimestamp = (zonedTimestamp, zoneOffset) => _make({
|
1201
|
+
..._uncalculated,
|
1202
|
+
timestamp: zonedTimestamp - zoneOffset * HOUR_MS,
|
1203
|
+
zoneOffset,
|
1204
|
+
_zonedTimestamp: zonedTimestamp
|
1205
|
+
});
|
1206
|
+
/** Instance of an uncalculated DateTime that represents 1/1/1970 00:00:00:000+0:00 */
|
1207
|
+
const _uncalculatedOrigin = /*#__PURE__*/_uncalculatedFromTimestamp(0, 0);
|
1208
|
+
/**
|
1209
|
+
* Tries to build a DateTime from `timestamp`, the number of milliseconds since 1/1/1970
|
1210
|
+
* 00:00:00:000+0:00, and `zoneOffset` which gives the offset between the local time and the UTC
|
1211
|
+
* time. Returns a `right` of a DateTime if successful.
|
1212
|
+
*
|
1213
|
+
* `timestamp` must be greater than or equal to MIN_TIMESTAMP and less than or equal to
|
1214
|
+
* MAX_TIMESTAMP.
|
1215
|
+
*
|
1216
|
+
* If `zoneOffset` is omitted, the local time zone offset of the machine this code is running on is
|
1217
|
+
* used.
|
1218
|
+
*
|
1219
|
+
* `zoneOffset` can be expressed as as a number of hours. In this case, it must be strictly greater
|
1220
|
+
* to -13 and strictly less than 15.
|
1221
|
+
*
|
1222
|
+
* It can also be expressed as an object containing three components:
|
1223
|
+
*
|
1224
|
+
* - `zoneHour` which must be greater than or equal to -12 and less than or equal to 14.
|
1225
|
+
* - `zoneMinute` which must be greater than or equal to 0 and less than or equal to 59.
|
1226
|
+
* - `zoneSecond` which must be greater than or equal to 0 and less than or equal to 59.
|
1227
|
+
*
|
1228
|
+
* Note that zoneHour=-0, zoneMinute=10, zoneSecond=0 is different from zoneHour=0, zoneMinute=10,
|
1229
|
+
* zoneSecond=0. The first corresponds to the string 'GMT-00:10', a negative 10-minute offset, the
|
1230
|
+
* second one to the string 'GMT+00:10', a positive 10-minute offset.
|
1231
|
+
*
|
1232
|
+
* `timestamp`, `zoneHour`, `zoneMinute` and `zoneSecond` should be integers. `zoneOffset`, when
|
1233
|
+
* expressed as a number of hours, does not need to be an integer.
|
1234
|
+
*
|
1235
|
+
* @category Constructors
|
1236
|
+
*/
|
1237
|
+
export const fromTimestamp = (timestamp, zoneOffset) => pipe(_uncalculatedOrigin, _setTimestamp(timestamp), Either.flatMap(setZoneOffsetKeepTimestamp(zoneOffset)));
|
1238
|
+
/**
|
1239
|
+
* Same as fromTimestamp but returns directly the DateTime or throws if it cannot be built
|
1240
|
+
*
|
1241
|
+
* @category Constructors
|
1242
|
+
*/
|
1243
|
+
export const fromTimestampOrThrow = /*#__PURE__*/flow(fromTimestamp, /*#__PURE__*/Either.getOrThrowWith(Function.identity));
|
1244
|
+
/**
|
1245
|
+
* Builds a DateTime using Date.now() as timestamp. `zoneOffset` is set to 0.
|
1246
|
+
*
|
1247
|
+
* @category Constructors
|
1248
|
+
*/
|
1249
|
+
export const now = () => _uncalculatedFromTimestamp(Date.now(), 0);
|
1250
|
+
/**
|
1251
|
+
* Tries to build a DateTime from the provided DateTime parts. Returns a `right` of this DateTime if
|
1252
|
+
* successful. Returns a `left` of an error otherwise.
|
1253
|
+
*
|
1254
|
+
* `year` must comprised in the range [MIN_FULL_YEAR, MAX_FULL_YEAR]. `ordinalDay` must be greater
|
1255
|
+
* than or equal to 1 and less than or equal to the number of days in the current year. `month` must
|
1256
|
+
* be greater than or equal to 1 (January) and less than or equal to 12 (December). `monthDay` must
|
1257
|
+
* be greater than or equal to 1 and less than or equal to the number of days in the current month.
|
1258
|
+
*
|
1259
|
+
* `isoYear` must be comprised in the range [MIN_FULL_YEAR, MAX_FULL_YEAR]. `isoWeek` must be
|
1260
|
+
* greater than or equal to 1 and less than or equal to the number of iso weeks in the current year.
|
1261
|
+
* `weekday` must be greater than or equal to 1 (monday) and less than or equal to 7 (sunday).
|
1262
|
+
*
|
1263
|
+
* If there is not sufficient information to determine the exact day of the year, i.e. none of the
|
1264
|
+
* three following tuples is fully determined [year, ordinalDay], [year, month, monthDay], [isoYear,
|
1265
|
+
* isoWeek, weekday], default values are determined in the following order (the first match stops
|
1266
|
+
* the process):
|
1267
|
+
*
|
1268
|
+
* - If `year` and `month` are set, `monthDay` is taken equal to 1.
|
1269
|
+
* - If `year` and `monthDay` are set, `month` is taken equal to 1.
|
1270
|
+
* - If `year` is set and both `month` and `monthDay` are undefined, the day is taken to be the first
|
1271
|
+
* one in the year.
|
1272
|
+
* - If `isoYear` and `isoWeek` are set, `weekday` is taken equal to 1.
|
1273
|
+
* - If `isoYear` and `weekday` are set, `isoWeek` is taken equal to 1.
|
1274
|
+
* - If `isoYear` is set and both `isoWeek` and `weekday` are undefined, the day is taken to be the
|
1275
|
+
* first one in the iso year.
|
1276
|
+
* - If both `year` and `isoYear` are undefined, an error is raised.
|
1277
|
+
*
|
1278
|
+
* `hour23` must be greater than or equal to 0 and less than or equal to 23. `hour11` must be
|
1279
|
+
* greater than or equal to 0 and less than or equal to 11. `meridiem` must be one of 0 (AM) or 12
|
1280
|
+
* (PM). If there is not sufficient information to determine the hour of the day, i.e. none of the
|
1281
|
+
* two following tuples is fully determined [hour23], [hour11, meridiem], default values are
|
1282
|
+
* determined as follows:
|
1283
|
+
*
|
1284
|
+
* - If `meridiem` is set, `hour11` is taken equal to 0.
|
1285
|
+
* - If `hour11` is set, `meridiem` is taken equal to 0.
|
1286
|
+
* - Otherwise, `meridiem` and `hour11` are taken equal to 0.
|
1287
|
+
*
|
1288
|
+
* `minute` must be greater than or equal to 0 and less than or equal to 59. If omitted, minute is
|
1289
|
+
* assumed to be 0.
|
1290
|
+
*
|
1291
|
+
* `second` must be greater than or equal to 0 and less than or equal to 59. If omitted, second is
|
1292
|
+
* assumed to be 0.
|
1293
|
+
*
|
1294
|
+
* `millisecond` must be greater than or equal to 0 and less than or equal to 999. If omitted,
|
1295
|
+
* millisecond is assumed to be 0.
|
1296
|
+
*
|
1297
|
+
* `zoneOffset` must be strictly greater to -13 and strictly less than 15. `zoneHour` must be
|
1298
|
+
* greater than or equal to -12 and less than or equal to 14. `zoneMinute` must be greater than or
|
1299
|
+
* equal to 0 and less than or equal to 59. `zoneSecond` must be greater than or equal to 0 and less
|
1300
|
+
* than or equal to 59.
|
1301
|
+
*
|
1302
|
+
* If there is not sufficient information to determine the exact time zone offset, i.e. none of the
|
1303
|
+
* two following tuples is fully determined [zoneOffset], [zoneHour, zoneMinute, zoneSecond],
|
1304
|
+
* default values are determined as follows :
|
1305
|
+
*
|
1306
|
+
* - If all parameters are undefined, the local time zone offset of the machine this code is running
|
1307
|
+
* on is used.
|
1308
|
+
* - If any of `zoneHour`, `zoneMinute`, `zoneSecond`, the undefined parameters are taken equal to 0.
|
1309
|
+
*
|
1310
|
+
* Note that zoneHour=-0, zoneMinute=10, zoneSecond=0 is different from zoneHour=0, zoneMinute=10,
|
1311
|
+
* zoneSecond=0. The first corresponds to the string 'GMT-00:10', a negative 10-minute offset, the
|
1312
|
+
* second one to the string 'GMT+00:10', a positive 10-minute offset.
|
1313
|
+
*
|
1314
|
+
* `year`, `ordinalDay`, `month`, `monthDay`, `isoYear`, `isoWeek`, `weekDay`, `hour23`, `hour11`,
|
1315
|
+
* `minute`, `second`, `millisecond`, `zoneHour`, `zoneMinute` and `zoneSecond` should be integers.
|
1316
|
+
* `zoneOffset` does not need to be an integer.
|
1317
|
+
*
|
1318
|
+
* All parameters must be coherent. For instance, `year=1970`, `month=1`, `monthDay=1`, `weekday=0`
|
1319
|
+
* `zoneHour=0`, `zoneMinute=0` and `zoneSecond=0` will trigger an error because 1/1/1970
|
1320
|
+
* 00:00:00:000+0:00 is a thursday. `hour23=13` and `meridiem=0` will also trigger an error.
|
1321
|
+
*
|
1322
|
+
* @category Constructors
|
1323
|
+
*/
|
1324
|
+
export const fromParts = ({
|
1325
|
+
year,
|
1326
|
+
ordinalDay,
|
1327
|
+
month,
|
1328
|
+
monthDay,
|
1329
|
+
isoYear,
|
1330
|
+
isoWeek,
|
1331
|
+
weekday,
|
1332
|
+
hour23,
|
1333
|
+
hour11,
|
1334
|
+
meridiem,
|
1335
|
+
minute,
|
1336
|
+
second,
|
1337
|
+
millisecond,
|
1338
|
+
zoneOffset,
|
1339
|
+
zoneHour,
|
1340
|
+
zoneMinute,
|
1341
|
+
zoneSecond
|
1342
|
+
}) => Either.gen(function* () {
|
1343
|
+
const zonedOrigin = yield* Either.gen(function* () {
|
1344
|
+
if (zoneOffset !== undefined || zoneHour === undefined && zoneMinute === undefined && zoneSecond === undefined) {
|
1345
|
+
const result = yield* pipe(_uncalculatedOrigin, setZoneOffsetKeepParts(zoneOffset));
|
1346
|
+
if (zoneHour !== undefined) yield* pipe(zoneHour, MInputError.assertValue({
|
1347
|
+
expected: getZoneHour(result),
|
1348
|
+
name: "'zoneHour'"
|
1349
|
+
}));
|
1350
|
+
if (zoneMinute !== undefined) yield* pipe(zoneMinute, MInputError.assertValue({
|
1351
|
+
expected: getZoneMinute(result),
|
1352
|
+
name: "'zoneMinute'"
|
1353
|
+
}));
|
1354
|
+
if (zoneSecond !== undefined) yield* pipe(zoneSecond, MInputError.assertValue({
|
1355
|
+
expected: getZoneSecond(result),
|
1356
|
+
name: "'zoneSecond'"
|
1357
|
+
}));
|
1358
|
+
return result;
|
1359
|
+
}
|
1360
|
+
return yield* pipe(_uncalculatedOrigin, setZoneOffsetKeepParts({
|
1361
|
+
zoneHour: zoneHour ?? 0,
|
1362
|
+
zoneMinute: zoneMinute ?? 0,
|
1363
|
+
zoneSecond: zoneSecond ?? 0
|
1364
|
+
}));
|
1365
|
+
});
|
1366
|
+
const withHour = yield* Either.gen(function* () {
|
1367
|
+
if (hour23 !== undefined) {
|
1368
|
+
const result = yield* setHour23(hour23)(zonedOrigin);
|
1369
|
+
if (hour11 !== undefined) yield* pipe(hour11, MInputError.assertValue({
|
1370
|
+
expected: getHour11(result),
|
1371
|
+
name: "'hour11'"
|
1372
|
+
}));
|
1373
|
+
if (meridiem !== undefined) yield* pipe(meridiem, MInputError.assertValue({
|
1374
|
+
expected: getMeridiem(result),
|
1375
|
+
name: "'meridiem'"
|
1376
|
+
}));
|
1377
|
+
return result;
|
1378
|
+
}
|
1379
|
+
const withHour11 = hour11 !== undefined ? yield* setHour11(hour11)(zonedOrigin) : zonedOrigin;
|
1380
|
+
return meridiem === 12 ? setMeridiem(12)(withHour11) : withHour11;
|
1381
|
+
});
|
1382
|
+
const withMinute = minute !== undefined ? yield* setMinute(minute)(withHour) : withHour;
|
1383
|
+
const withSecond = second !== undefined ? yield* setSecond(second)(withMinute) : withMinute;
|
1384
|
+
const withMillisecond = millisecond !== undefined ? yield* setMillisecond(millisecond)(withSecond) : withSecond;
|
1385
|
+
const hasYear = year !== undefined;
|
1386
|
+
const hasIsoYear = isoYear !== undefined;
|
1387
|
+
const hasIsoWeek = isoWeek !== undefined;
|
1388
|
+
const hasWeekday = weekday !== undefined;
|
1389
|
+
if (hasYear && !(hasIsoYear && hasIsoWeek && hasWeekday)) {
|
1390
|
+
const withYear = yield* setYear(year)(withMillisecond);
|
1391
|
+
const withDay = yield* Either.gen(function* () {
|
1392
|
+
if (ordinalDay !== undefined) {
|
1393
|
+
const result = yield* setOrdinalDay(ordinalDay)(withYear);
|
1394
|
+
if (month !== undefined) yield* pipe(month, MInputError.assertValue({
|
1395
|
+
expected: getMonth(result),
|
1396
|
+
name: "'month'"
|
1397
|
+
}));
|
1398
|
+
if (monthDay !== undefined) yield* pipe(monthDay, MInputError.assertValue({
|
1399
|
+
expected: getMonthDay(result),
|
1400
|
+
name: "'monthDay'"
|
1401
|
+
}));
|
1402
|
+
return result;
|
1403
|
+
}
|
1404
|
+
const withMonth = month !== undefined ? yield* setMonth(month)(withYear) : withYear;
|
1405
|
+
return monthDay !== undefined ? yield* setMonthDay(monthDay)(withMonth) : withMonth;
|
1406
|
+
});
|
1407
|
+
if (hasIsoYear) yield* pipe(isoYear, MInputError.assertValue({
|
1408
|
+
expected: getIsoYear(withDay),
|
1409
|
+
name: "'isoYear'"
|
1410
|
+
}));
|
1411
|
+
if (hasIsoWeek) yield* pipe(isoWeek, MInputError.assertValue({
|
1412
|
+
expected: getIsoWeek(withDay),
|
1413
|
+
name: "'isoWeek'"
|
1414
|
+
}));
|
1415
|
+
if (hasWeekday) yield* pipe(weekday, MInputError.assertValue({
|
1416
|
+
expected: getWeekday(withDay),
|
1417
|
+
name: "'weekday'"
|
1418
|
+
}));
|
1419
|
+
return withDay;
|
1420
|
+
}
|
1421
|
+
if (!hasIsoYear) return yield* Either.left(new MInputError.Type({
|
1422
|
+
message: "One of 'year' and 'isoYear' must be be set"
|
1423
|
+
}));
|
1424
|
+
const withIsoYear = yield* setIsoYear(isoYear)(withMillisecond);
|
1425
|
+
const withIsoWeek = yield* setIsoWeek(isoWeek ?? 1)(withIsoYear);
|
1426
|
+
const withWeekday = yield* setWeekday(weekday ?? 1)(withIsoWeek);
|
1427
|
+
if (hasYear) yield* pipe(year, MInputError.assertValue({
|
1428
|
+
expected: getYear(withWeekday),
|
1429
|
+
name: "'year'"
|
1430
|
+
}));
|
1431
|
+
if (month !== undefined) yield* pipe(month, MInputError.assertValue({
|
1432
|
+
expected: getMonth(withWeekday),
|
1433
|
+
name: "'month'"
|
1434
|
+
}));
|
1435
|
+
if (monthDay !== undefined) yield* pipe(monthDay, MInputError.assertValue({
|
1436
|
+
expected: getMonthDay(withWeekday),
|
1437
|
+
name: "'monthDay'"
|
1438
|
+
}));
|
1439
|
+
if (ordinalDay !== undefined) yield* pipe(ordinalDay, MInputError.assertValue({
|
1440
|
+
expected: getOrdinalDay(withWeekday),
|
1441
|
+
name: "'ordinalDay'"
|
1442
|
+
}));
|
1443
|
+
return withWeekday;
|
1444
|
+
});
|
1445
|
+
/**
|
1446
|
+
* Same as fromParts but returns directly the DateTime or throws if it cannot be built
|
1447
|
+
*
|
1448
|
+
* @category Constructors
|
1449
|
+
*/
|
1450
|
+
export const fromPartsOrThrow = /*#__PURE__*/flow(fromParts, /*#__PURE__*/Either.getOrThrowWith(Function.identity));
|
1451
|
+
/**
|
1452
|
+
* Builds a CVDateTime from a Javascript Date
|
1453
|
+
*
|
1454
|
+
* @category Constructors
|
1455
|
+
*/
|
1456
|
+
export const fromDate = date => fromTimestampOrThrow(date.getTime());
|
1457
|
+
/**
|
1458
|
+
* Builds a CVDateTime from an Effect DateTime
|
1459
|
+
*
|
1460
|
+
* @category Constructors
|
1461
|
+
*/
|
1462
|
+
export const fromEffectDateTime = date => fromTimestampOrThrow(DateTime.toEpochMillis(date), DateTime.zonedOffset(date));
|
1463
|
+
/**
|
1464
|
+
* Builds a Javascript Date from a CVDateTime
|
1465
|
+
*
|
1466
|
+
* @category Conversions
|
1467
|
+
*/
|
1468
|
+
export const toDate = self => new Date(timestamp(self));
|
1469
|
+
/**
|
1470
|
+
* Builds an Effect DateTime from a CVDateTime
|
1471
|
+
*
|
1472
|
+
* @category Conversions
|
1473
|
+
*/
|
1474
|
+
export const toEffectDateTime = self => DateTime.unsafeMakeZoned(timestamp(self), {
|
1475
|
+
timeZone: self.zoneOffset
|
1476
|
+
});
|
1477
|
+
/**
|
1478
|
+
* Returns the timestamp of `self` as a number
|
1479
|
+
*
|
1480
|
+
* @category Getters
|
1481
|
+
*/
|
1482
|
+
export const timestamp = /*#__PURE__*/Struct.get('timestamp');
|
1483
|
+
/** Returns the gregorianDate of `self` for the given time zone */
|
1484
|
+
const _gregorianDate = self => pipe(self.gregorianDate, Option.getOrElse(() => {
|
1485
|
+
const result = GregorianDate.fromTimestamp(self._zonedTimestamp);
|
1486
|
+
/* eslint-disable-next-line functional/immutable-data, functional/no-expression-statements */
|
1487
|
+
self.gregorianDate = Option.some(result);
|
1488
|
+
return result;
|
1489
|
+
}));
|
1490
|
+
/**
|
1491
|
+
* Returns the (Gregorian) year of `self` for the given time zone
|
1492
|
+
*
|
1493
|
+
* @category Getters
|
1494
|
+
*/
|
1495
|
+
export const getYear = /*#__PURE__*/flow(_gregorianDate, GregorianDate.year);
|
1496
|
+
/**
|
1497
|
+
* Returns true if the (Gregorian) year of `self` for the given time zone is a leap year. Returns
|
1498
|
+
* false otherwise
|
1499
|
+
*
|
1500
|
+
* @category Getters
|
1501
|
+
*/
|
1502
|
+
export const yearIsLeap = /*#__PURE__*/flow(_gregorianDate, GregorianDate.yearIsLeap);
|
1503
|
+
/**
|
1504
|
+
* Returns the ordinalDay of `self` for the given time zone
|
1505
|
+
*
|
1506
|
+
* @category Getters
|
1507
|
+
*/
|
1508
|
+
export const getOrdinalDay = /*#__PURE__*/flow(_gregorianDate, GregorianDate.ordinalDay);
|
1509
|
+
/**
|
1510
|
+
* Returns the month of `self` for the given time zone
|
1511
|
+
*
|
1512
|
+
* @category Getters
|
1513
|
+
*/
|
1514
|
+
export const getMonth = /*#__PURE__*/flow(_gregorianDate, GregorianDate.getMonth);
|
1515
|
+
/**
|
1516
|
+
* Returns the monthDay of `self` for the given time zone
|
1517
|
+
*
|
1518
|
+
* @category Getters
|
1519
|
+
*/
|
1520
|
+
export const getMonthDay = /*#__PURE__*/flow(_gregorianDate, GregorianDate.getMonthDay);
|
1521
|
+
/** Returns the isoDate of `self` for the given time zone */
|
1522
|
+
const _isoDate = self => pipe(self.isoDate, Option.getOrElse(() => {
|
1523
|
+
const result = pipe(self.gregorianDate, Option.map(IsoDate.fromGregorianDate), Option.getOrElse(() => IsoDate.fromTimestamp(self._zonedTimestamp)));
|
1524
|
+
/* eslint-disable-next-line functional/immutable-data, functional/no-expression-statements */
|
1525
|
+
self.isoDate = Option.some(result);
|
1526
|
+
return result;
|
1527
|
+
}));
|
1528
|
+
/**
|
1529
|
+
* Returns the isoYear of `self` for the given time zone
|
1530
|
+
*
|
1531
|
+
* @category Getters
|
1532
|
+
*/
|
1533
|
+
export const getIsoYear = /*#__PURE__*/flow(_isoDate, IsoDate.year);
|
1534
|
+
/**
|
1535
|
+
* Returns true if the isoYear of `self` for the given time zone is a long year. Returns false
|
1536
|
+
* otherwise
|
1537
|
+
*
|
1538
|
+
* @category Getters
|
1539
|
+
*/
|
1540
|
+
export const isoYearIsLong = /*#__PURE__*/flow(_isoDate, IsoDate.yearIsLong);
|
1541
|
+
/**
|
1542
|
+
* Returns the isoWeek of `self` for the given time zone
|
1543
|
+
*
|
1544
|
+
* @category Getters
|
1545
|
+
*/
|
1546
|
+
export const getIsoWeek = /*#__PURE__*/flow(_isoDate, IsoDate.getIsoWeek);
|
1547
|
+
/**
|
1548
|
+
* Returns the weekday of `self` for the given time zone
|
1549
|
+
*
|
1550
|
+
* @category Getters
|
1551
|
+
*/
|
1552
|
+
export const getWeekday = /*#__PURE__*/flow(_isoDate, IsoDate.getWeekday);
|
1553
|
+
/** Returns the time of `self` for the given time zone */
|
1554
|
+
const _time = self => pipe(self.time, Option.getOrElse(() => {
|
1555
|
+
const result = pipe(self._zonedTimestamp, MNumber.intModulo(DAY_MS), Time.fromTimestamp);
|
1556
|
+
/* eslint-disable-next-line functional/immutable-data, functional/no-expression-statements */
|
1557
|
+
self.time = Option.some(result);
|
1558
|
+
return result;
|
1559
|
+
}));
|
1560
|
+
/**
|
1561
|
+
* Returns the hour23 of `self` for the given time zone
|
1562
|
+
*
|
1563
|
+
* @category Getters
|
1564
|
+
*/
|
1565
|
+
export const getHour23 = /*#__PURE__*/flow(_time, Time.hour23);
|
1566
|
+
/**
|
1567
|
+
* Returns the hour11 of `self` for the given time zone
|
1568
|
+
*
|
1569
|
+
* @category Getters
|
1570
|
+
*/
|
1571
|
+
export const getHour11 = /*#__PURE__*/flow(_time, Time.hour11);
|
1572
|
+
/**
|
1573
|
+
* Returns the meridiem of `self` for the given time zone
|
1574
|
+
*
|
1575
|
+
* @category Getters
|
1576
|
+
*/
|
1577
|
+
export const getMeridiem = /*#__PURE__*/flow(_time, Time.meridiem);
|
1578
|
+
/**
|
1579
|
+
* Returns the minute of `self` for the given time zone
|
1580
|
+
*
|
1581
|
+
* @category Getters
|
1582
|
+
*/
|
1583
|
+
export const getMinute = /*#__PURE__*/flow(_time, Time.minute);
|
1584
|
+
/**
|
1585
|
+
* Returns the second of `self` for the given time zone
|
1586
|
+
*
|
1587
|
+
* @category Getters
|
1588
|
+
*/
|
1589
|
+
export const getSecond = /*#__PURE__*/flow(_time, Time.second);
|
1590
|
+
/**
|
1591
|
+
* Returns the millisecond of `self` for the given time zone
|
1592
|
+
*
|
1593
|
+
* @category Getters
|
1594
|
+
*/
|
1595
|
+
export const getMillisecond = /*#__PURE__*/flow(_time, Time.millisecond);
|
1596
|
+
/** Returns the zoneOffsetParts of `self` */
|
1597
|
+
const _zoneOffsetParts = self => pipe(self.zoneOffsetParts, Option.getOrElse(() => {
|
1598
|
+
const result = ZoneOffsetParts.fromZoneOffset(self.zoneOffset);
|
1599
|
+
/* eslint-disable-next-line functional/immutable-data, functional/no-expression-statements */
|
1600
|
+
self.zoneOffsetParts = Option.some(result);
|
1601
|
+
return result;
|
1602
|
+
}));
|
1603
|
+
/**
|
1604
|
+
* Returns the hour part of the zoneOffset of `self`
|
1605
|
+
*
|
1606
|
+
* @category Getters
|
1607
|
+
*/
|
1608
|
+
export const getZoneHour = /*#__PURE__*/flow(_zoneOffsetParts, ZoneOffsetParts.zoneHour);
|
1609
|
+
/**
|
1610
|
+
* Returns the minute part of the zoneOffset of `self`
|
1611
|
+
*
|
1612
|
+
* @category Getters
|
1613
|
+
*/
|
1614
|
+
export const getZoneMinute = /*#__PURE__*/flow(_zoneOffsetParts, ZoneOffsetParts.zoneMinute);
|
1615
|
+
/**
|
1616
|
+
* Returns the minute part of the zoneOffset of `self`
|
1617
|
+
*
|
1618
|
+
* @category Getters
|
1619
|
+
*/
|
1620
|
+
export const getZoneSecond = /*#__PURE__*/flow(_zoneOffsetParts, ZoneOffsetParts.zoneSecond);
|
1621
|
+
const _gregorianDateSetter = self => {
|
1622
|
+
const selfTimestamp = _gregorianDate(self).timestamp;
|
1623
|
+
return gregorianDate => {
|
1624
|
+
const offset = gregorianDate.timestamp - selfTimestamp;
|
1625
|
+
return pipe(self, MStruct.evolve({
|
1626
|
+
timestamp: Number.sum(offset),
|
1627
|
+
gregorianDate: pipe(gregorianDate, Option.some, Function.constant),
|
1628
|
+
isoDate: Function.constant(Option.none()),
|
1629
|
+
_zonedTimestamp: Number.sum(offset)
|
1630
|
+
}), _make);
|
1631
|
+
};
|
1632
|
+
};
|
1633
|
+
const _isoDateSetter = self => {
|
1634
|
+
const selfTimestamp = _isoDate(self).timestamp;
|
1635
|
+
return isoDate => {
|
1636
|
+
const offset = isoDate.timestamp - selfTimestamp;
|
1637
|
+
return pipe(self, MStruct.evolve({
|
1638
|
+
timestamp: Number.sum(offset),
|
1639
|
+
gregorianDate: Function.constant(Option.none()),
|
1640
|
+
isoDate: pipe(isoDate, Option.some, Function.constant),
|
1641
|
+
_zonedTimestamp: Number.sum(offset)
|
1642
|
+
}), _make);
|
1643
|
+
};
|
1644
|
+
};
|
1645
|
+
const _timeSetter = self => {
|
1646
|
+
const selfTimestampOffset = _time(self).timestampOffset;
|
1647
|
+
return time => {
|
1648
|
+
const offset = time.timestampOffset - selfTimestampOffset;
|
1649
|
+
return pipe(self, MStruct.evolve({
|
1650
|
+
timestamp: Number.sum(offset),
|
1651
|
+
time: pipe(time, Option.some, Function.constant),
|
1652
|
+
_zonedTimestamp: Number.sum(offset)
|
1653
|
+
}), _make);
|
1654
|
+
};
|
1655
|
+
};
|
1656
|
+
/**
|
1657
|
+
* If possible, returns a right of a DateTime having year `year` and the same `month`, `monthDay`,
|
1658
|
+
* `hour23`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a `left` of an
|
1659
|
+
* error otherwise. `year` must be an integer comprised in the range [MIN_FULL_YEAR,
|
1660
|
+
* MAX_FULL_YEAR].
|
1661
|
+
*
|
1662
|
+
* @category Setters
|
1663
|
+
*/
|
1664
|
+
export const setYear = year => self => pipe(self, _gregorianDate, GregorianDate.setYear(year), Either.map(_gregorianDateSetter(self)));
|
1665
|
+
/**
|
1666
|
+
* Same as setYear but returns directly a DateTime or throws in case of an error
|
1667
|
+
*
|
1668
|
+
* @category Setters
|
1669
|
+
*/
|
1670
|
+
export const setYearOrThrow = /*#__PURE__*/flow(setYear, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1671
|
+
/**
|
1672
|
+
* If possible, returns a right of a DateTime having ordinalDay `ordinalDay` and the same `year`,
|
1673
|
+
* `hour23`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a `left` of an
|
1674
|
+
* error otherwise. `ordinalDay` must be an integer greater than or equal to 1 and less than or
|
1675
|
+
* equal to the number of days in the current year
|
1676
|
+
*
|
1677
|
+
* @category Setters
|
1678
|
+
*/
|
1679
|
+
export const setOrdinalDay = ordinalDay => self => pipe(self, _gregorianDate, GregorianDate.setOrdinalDay(ordinalDay), Either.map(_gregorianDateSetter(self)));
|
1680
|
+
/**
|
1681
|
+
* Same as setOrdinalDay but returns directly a DateTime or throws in case of an error
|
1682
|
+
*
|
1683
|
+
* @category Setters
|
1684
|
+
*/
|
1685
|
+
export const setOrdinalDayOrThrow = /*#__PURE__*/flow(setOrdinalDay, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1686
|
+
/**
|
1687
|
+
* If possible, returns a right of a DateTime having month `month` and the same `year`, `monthDay`,
|
1688
|
+
* `hour23`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a `left` of an
|
1689
|
+
* error otherwise. `month` must be an integer greater than or equal to 1 (January) and less than or
|
1690
|
+
* equal to 12 (December)
|
1691
|
+
*
|
1692
|
+
* @category Setters
|
1693
|
+
*/
|
1694
|
+
export const setMonth = month => self => pipe(self, _gregorianDate, GregorianDate.setMonth(month), Either.map(_gregorianDateSetter(self)));
|
1695
|
+
/**
|
1696
|
+
* Same as setMonth but returns directly a DateTime or throws in case of an error
|
1697
|
+
*
|
1698
|
+
* @category Setters
|
1699
|
+
*/
|
1700
|
+
export const setMonthOrThrow = /*#__PURE__*/flow(setMonth, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1701
|
+
/**
|
1702
|
+
* If possible, returns a right of a DateTime having monthDay `monthDay` and the same `year`,
|
1703
|
+
* `month`, `hour23`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a `left`
|
1704
|
+
* of an error otherwise. `monthDay` must be an integer greater than or equal to 1 and less than or
|
1705
|
+
* equal to the number of days in the current month.
|
1706
|
+
*
|
1707
|
+
* @category Setters
|
1708
|
+
*/
|
1709
|
+
export const setMonthDay = monthDay => self => pipe(self, _gregorianDate, GregorianDate.setMonthDay(monthDay), Either.map(_gregorianDateSetter(self)));
|
1710
|
+
/**
|
1711
|
+
* Same as setMonthDay but returns directly a DateTime or throws in case of an error
|
1712
|
+
*
|
1713
|
+
* @category Setters
|
1714
|
+
*/
|
1715
|
+
export const setMonthDayOrThrow = /*#__PURE__*/flow(setMonthDay, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1716
|
+
/**
|
1717
|
+
* If possible, returns a right of a DateTime having isoYear `isoYear` and the same `isoWeek`,
|
1718
|
+
* `weekday`, `hour23`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a
|
1719
|
+
* `left` of an error otherwise. `isoYear` must be an integer comprised in the range [MIN_FULL_YEAR,
|
1720
|
+
* MAX_FULL_YEAR].
|
1721
|
+
*
|
1722
|
+
* @category Setters
|
1723
|
+
*/
|
1724
|
+
export const setIsoYear = isoYear => self => pipe(self, _isoDate, IsoDate.setYear(isoYear), Either.map(_isoDateSetter(self)));
|
1725
|
+
/**
|
1726
|
+
* Same as setIsoYear but returns directly a DateTime or throws in case of an error
|
1727
|
+
*
|
1728
|
+
* @category Setters
|
1729
|
+
*/
|
1730
|
+
export const setIsoYearOrThrow = /*#__PURE__*/flow(setIsoYear, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1731
|
+
/**
|
1732
|
+
* If possible, returns a right of a DateTime having isoWeek `isoWeek` and the same `isoYear`,
|
1733
|
+
* `weekday`, `hour23`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a
|
1734
|
+
* `left` of an error otherwise. `isoWeek` must be an integer greater than or equal to 1 and less
|
1735
|
+
* than or equal to the number of iso weeks in the current year.
|
1736
|
+
*
|
1737
|
+
* @category Setters
|
1738
|
+
*/
|
1739
|
+
export const setIsoWeek = isoWeek => self => pipe(self, _isoDate, IsoDate.setIsoWeek(isoWeek), Either.map(_isoDateSetter(self)));
|
1740
|
+
/**
|
1741
|
+
* Same as setIsoWeek but returns directly a DateTime or throws in case of an error
|
1742
|
+
*
|
1743
|
+
* @category Setters
|
1744
|
+
*/
|
1745
|
+
export const setIsoWeekOrThrow = /*#__PURE__*/flow(setIsoWeek, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1746
|
+
/**
|
1747
|
+
* If possible, returns a right of a DateTime having weekday `weekday` and the same `isoYear`,
|
1748
|
+
* `isoWeek`, `hour23`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a
|
1749
|
+
* `left` of an error otherwise. `weekday` must be an integer greater than or equal to 1 (monday)
|
1750
|
+
* and less than or equal to 7 (sunday).
|
1751
|
+
*
|
1752
|
+
* @category Setters
|
1753
|
+
*/
|
1754
|
+
export const setWeekday = weekday => self => pipe(self, _isoDate, IsoDate.setWeekday(weekday), Either.map(_isoDateSetter(self)));
|
1755
|
+
/**
|
1756
|
+
* Same as setWeekday but returns directly a DateTime or throws in case of an error
|
1757
|
+
*
|
1758
|
+
* @category Setters
|
1759
|
+
*/
|
1760
|
+
export const setWeekdayOrThrow = /*#__PURE__*/flow(setWeekday, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1761
|
+
/**
|
1762
|
+
* If possible, returns a right of a DateTime having hour23 `hour23` and the same `year`,
|
1763
|
+
* `ordinalDay`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a `left` of
|
1764
|
+
* an error otherwise. `hour23` must be an integer greater than or equal to 0 and less than or equal
|
1765
|
+
* to 23
|
1766
|
+
*
|
1767
|
+
* @category Setters
|
1768
|
+
*/
|
1769
|
+
export const setHour23 = hour23 => self => pipe(self, _time, Time.setHour23(hour23), Either.map(_timeSetter(self)));
|
1770
|
+
/**
|
1771
|
+
* Same as setHour23 but returns directly a DateTime or throws in case of an error
|
1772
|
+
*
|
1773
|
+
* @category Setters
|
1774
|
+
*/
|
1775
|
+
export const setHour23OrThrow = /*#__PURE__*/flow(setHour23, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1776
|
+
/**
|
1777
|
+
* If possible, returns a right of a DateTime having hour11 `hour11` and the same `year`,
|
1778
|
+
* `ordinalDay`, `meridiem`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a
|
1779
|
+
* `left` of an error otherwise. `hour11` must be an integer greater than or equal to 0 and less
|
1780
|
+
* than or equal to 11.
|
1781
|
+
*
|
1782
|
+
* @category Setters
|
1783
|
+
*/
|
1784
|
+
export const setHour11 = hour11 => self => pipe(self, _time, Time.setHour11(hour11), Either.map(_timeSetter(self)));
|
1785
|
+
/**
|
1786
|
+
* Same as setHour11 but returns directly a DateTime or throws in case of an error
|
1787
|
+
*
|
1788
|
+
* @category Setters
|
1789
|
+
*/
|
1790
|
+
export const setHour11OrThrow = /*#__PURE__*/flow(setHour11, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1791
|
+
/**
|
1792
|
+
* Returns a DateTime having meridiem `meridiem` and the same `year`, `ordinalDay`, `hour11`,
|
1793
|
+
* `minute`, `second`, `millisecond` and `zoneOffset` as `self`
|
1794
|
+
*
|
1795
|
+
* @category Setters
|
1796
|
+
*/
|
1797
|
+
export const setMeridiem = meridiem => self => pipe(self, _time, Time.setMeridiem(meridiem), _timeSetter(self));
|
1798
|
+
/**
|
1799
|
+
* If possible, returns a right of a DateTime having minute `minute` and the same `year`,
|
1800
|
+
* `ordinalDay`, `hour23`, `second`, `millisecond` and `zoneOffset` as `self`. Returns a `left` of
|
1801
|
+
* an error otherwise. `minute` must be an integer greater than or equal to 0 and less than or equal
|
1802
|
+
* to 59
|
1803
|
+
*
|
1804
|
+
* @category Setters
|
1805
|
+
*/
|
1806
|
+
export const setMinute = minute => self => pipe(self, _time, Time.setMinute(minute), Either.map(_timeSetter(self)));
|
1807
|
+
/**
|
1808
|
+
* Same as setMinute but returns directly a DateTime or throws in case of an error
|
1809
|
+
*
|
1810
|
+
* @category Setters
|
1811
|
+
*/
|
1812
|
+
export const setMinuteOrThrow = /*#__PURE__*/flow(setMinute, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1813
|
+
/**
|
1814
|
+
* If possible, returns a right of a DateTime having second `second` and the same `year`,
|
1815
|
+
* `ordinalDay`, `hour23`, `minute`, `millisecond` and `zoneOffset` as `self`. Returns a `left` of
|
1816
|
+
* an error otherwise. `second` must be an integer greater than or equal to 0 and less than or equal
|
1817
|
+
* to 59
|
1818
|
+
*
|
1819
|
+
* @category Setters
|
1820
|
+
*/
|
1821
|
+
export const setSecond = second => self => pipe(self, _time, Time.setSecond(second), Either.map(_timeSetter(self)));
|
1822
|
+
/**
|
1823
|
+
* Same as setSecond but returns directly a DateTime or throws in case of an error
|
1824
|
+
*
|
1825
|
+
* @category Setters
|
1826
|
+
*/
|
1827
|
+
export const setSecondOrThrow = /*#__PURE__*/flow(setSecond, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1828
|
+
/**
|
1829
|
+
* If possible, returns a right of a DateTime having millisecond `millisecond` and the same `year`,
|
1830
|
+
* `ordinalDay`, `hour23`, `minute`, `second` and `zoneOffset` as `self`. Returns a `left` of an
|
1831
|
+
* error otherwise. `millisecond` must be an integer greater than or equal to 0 and less than or
|
1832
|
+
* equal to 999.
|
1833
|
+
*
|
1834
|
+
* @category Setters
|
1835
|
+
*/
|
1836
|
+
export const setMillisecond = millisecond => self => pipe(self, _time, Time.setMillisecond(millisecond), Either.map(_timeSetter(self)));
|
1837
|
+
/**
|
1838
|
+
* Same as setMillisecond but returns directly a DateTime or throws in case of an error
|
1839
|
+
*
|
1840
|
+
* @category Setters
|
1841
|
+
*/
|
1842
|
+
export const setMillisecondOrThrow = /*#__PURE__*/flow(setMillisecond, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1843
|
+
/**
|
1844
|
+
* If possible, returns a right of a copy of `self` with timestamp set to `timestamp`. Returns a
|
1845
|
+
* `left` of an error otherwise. `timestamp` must be an integer comprised in the range
|
1846
|
+
* [MIN_TIMESTAMP, MAX_TIMESTAMP] representing the number of milliseconds since 1/1/1970
|
1847
|
+
* 00:00:00:000+0:00.
|
1848
|
+
*/
|
1849
|
+
const _setTimestamp = timestamp => self => Either.gen(function* () {
|
1850
|
+
const validatedTimestamp = yield* pipe(timestamp, MInputError.assertInRange({
|
1851
|
+
min: MIN_TIMESTAMP,
|
1852
|
+
max: MAX_TIMESTAMP,
|
1853
|
+
minIncluded: true,
|
1854
|
+
maxIncluded: true,
|
1855
|
+
offset: 0,
|
1856
|
+
name: 'timestamp'
|
1857
|
+
}));
|
1858
|
+
return _uncalculatedFromTimestamp(validatedTimestamp, self.zoneOffset);
|
1859
|
+
});
|
1860
|
+
const _setZoneOffset = (keepTimestamp, zoneOffset = LOCAL_TIME_ZONE_OFFSET) => self => Either.gen(function* () {
|
1861
|
+
const buildFromZoneOffset = validatedZoneOffset => keepTimestamp ? _uncalculatedFromTimestamp(self.timestamp, validatedZoneOffset) : _uncalculatedFromZonedTimestamp(self._zonedTimestamp, validatedZoneOffset);
|
1862
|
+
if (MTypes.isPrimitive(zoneOffset)) {
|
1863
|
+
const validatedZoneOffset = yield* pipe(zoneOffset, MInputError.assertInRange({
|
1864
|
+
min: -13,
|
1865
|
+
max: 15,
|
1866
|
+
minIncluded: false,
|
1867
|
+
maxIncluded: false,
|
1868
|
+
offset: 0,
|
1869
|
+
name: "'zoneOffset'"
|
1870
|
+
}));
|
1871
|
+
return buildFromZoneOffset(validatedZoneOffset);
|
1872
|
+
}
|
1873
|
+
const zoneOffsetParts = yield* ZoneOffsetParts.fromParts(zoneOffset);
|
1874
|
+
const result = pipe(zoneOffsetParts, ZoneOffsetParts.toHour, buildFromZoneOffset);
|
1875
|
+
/* eslint-disable-next-line functional/immutable-data, functional/no-expression-statements */
|
1876
|
+
result.zoneOffsetParts = Option.some(zoneOffsetParts);
|
1877
|
+
return result;
|
1878
|
+
});
|
1879
|
+
/**
|
1880
|
+
* If possible, returns a right of a copy of `self` with the same timestamp and zoneOffset set to
|
1881
|
+
* `zoneOffset`.
|
1882
|
+
*
|
1883
|
+
* If `zoneOffset` is omitted, the local time zone offset of the machine this code is running on is
|
1884
|
+
* used.
|
1885
|
+
*
|
1886
|
+
* `zoneOffset` can be expressed as as a number of hours. In this case, it must be strictly greater
|
1887
|
+
* to -13 and strictly less than 15.
|
1888
|
+
*
|
1889
|
+
* It can also be expressed as an object containing three components:
|
1890
|
+
*
|
1891
|
+
* - `zoneHour` which must be greater than or equal to -12 and less than or equal to 14.
|
1892
|
+
* - `zoneMinute` which must be greater than or equal to 0 and less than or equal to 59.
|
1893
|
+
* - `zoneSecond` which must be greater than or equal to 0 and less than or equal to 59.
|
1894
|
+
*
|
1895
|
+
* `zoneHour`, `zoneMinute` and `zoneSecond` should be integers. `zoneOffset`, when expressed as a
|
1896
|
+
* number of hours, does not need to be an integer.
|
1897
|
+
*
|
1898
|
+
* @category Setters
|
1899
|
+
*/
|
1900
|
+
export const setZoneOffsetKeepTimestamp = zoneOffset => _setZoneOffset(true, zoneOffset);
|
1901
|
+
/**
|
1902
|
+
* Same as setZoneOffsetKeepTimestamp but returns directly a DateTime or throws in case of an error
|
1903
|
+
*
|
1904
|
+
* @category Setters
|
1905
|
+
*/
|
1906
|
+
export const setZoneOffsetKeepTimestampOrThrow = /*#__PURE__*/flow(setZoneOffsetKeepTimestamp, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1907
|
+
/**
|
1908
|
+
* If possible, returns a right of a copy of `self` with the same CVDateTime.Parts (except
|
1909
|
+
* zoneOffset) and zoneOffset set to `zoneOffset`.
|
1910
|
+
*
|
1911
|
+
* See setZoneOffsetKeepTimestamp for more details
|
1912
|
+
*
|
1913
|
+
* @category Setters
|
1914
|
+
*/
|
1915
|
+
export const setZoneOffsetKeepParts = zoneOffset => _setZoneOffset(false, zoneOffset);
|
1916
|
+
/**
|
1917
|
+
* Same as setZoneOffsetKeepTimestamp but returns directly a DateTime or throws in case of an error
|
1918
|
+
*
|
1919
|
+
* @category Setters
|
1920
|
+
*/
|
1921
|
+
export const setZoneOffsetKeepPartsOrThrow = /*#__PURE__*/flow(setZoneOffsetKeepParts, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
1922
|
+
/**
|
1923
|
+
* Returns true if self is the first day of a month in the given timezone
|
1924
|
+
*
|
1925
|
+
* @category Predicates
|
1926
|
+
*/
|
1927
|
+
export const isFirstMonthDay = self => getMonthDay(self) === 1;
|
1928
|
+
/**
|
1929
|
+
* Returns true if self is the last day of a month in the given timezone
|
1930
|
+
*
|
1931
|
+
* @category Predicates
|
1932
|
+
*/
|
1933
|
+
export const isLastMonthDay = self => getMonthDay(self) === pipe(self, _gregorianDate, GregorianDate.getNumberOfDaysInMonth(getMonth(self)));
|
1934
|
+
/**
|
1935
|
+
* Returns true if self is the first day of a year in the given timezone
|
1936
|
+
*
|
1937
|
+
* @category Predicates
|
1938
|
+
*/
|
1939
|
+
export const isFirstYearDay = self => getOrdinalDay(self) === 1;
|
1940
|
+
/**
|
1941
|
+
* Returns true if self is the last day of a year in the given timezone
|
1942
|
+
*
|
1943
|
+
* @category Predicates
|
1944
|
+
*/
|
1945
|
+
export const isLastYearDay = self => getOrdinalDay(self) === pipe(self, _gregorianDate, GregorianDate.getYearDurationInDays);
|
1946
|
+
/**
|
1947
|
+
* Returns true if self is the first day of an iso year in the given timezone
|
1948
|
+
*
|
1949
|
+
* @category Predicates
|
1950
|
+
*/
|
1951
|
+
export const isFirstIsoYearDay = self => getIsoWeek(self) === 1 && getWeekday(self) === 1;
|
1952
|
+
/**
|
1953
|
+
* Returns true if self is the last day of an iso year in the given timezone
|
1954
|
+
*
|
1955
|
+
* @category Predicates
|
1956
|
+
*/
|
1957
|
+
export const isLastIsoYearDay = self => getIsoWeek(self) === pipe(self, _isoDate, IsoDate.getLastIsoWeek) && getWeekday(self) === 7;
|
1958
|
+
/**
|
1959
|
+
* Returns a copy of `self` where `monthDay` is set to the first day of the current month. All time
|
1960
|
+
* parts (`hour23`, `hour11`, `meridiem`, `minute`, `second`, `millisecond`) are left unchanged
|
1961
|
+
*
|
1962
|
+
* @category Offsetters
|
1963
|
+
*/
|
1964
|
+
export const toFirstMonthDay = /*#__PURE__*/setMonthDayOrThrow(1);
|
1965
|
+
/**
|
1966
|
+
* Returns a copy of `self` where `monthDay` is set to the last day of the current month. All time
|
1967
|
+
* parts (`hour23`, `hour11`, `meridiem`, `minute`, `second`, `millisecond`) are left unchanged
|
1968
|
+
*
|
1969
|
+
* @category Offsetters
|
1970
|
+
*/
|
1971
|
+
export const toLastMonthDay = self => setMonthDayOrThrow(pipe(self, _gregorianDate, GregorianDate.getNumberOfDaysInMonth(getMonth(self))))(self);
|
1972
|
+
/**
|
1973
|
+
* Returns a copy of `self` where `ordinalDay` is set to the first day of the current year. All time
|
1974
|
+
* parts (`hour23`, `hour11`, `meridiem`, `minute`, `second`, `millisecond`) are left unchanged
|
1975
|
+
*
|
1976
|
+
* @category Offsetters
|
1977
|
+
*/
|
1978
|
+
export const toFirstYearDay = self => setOrdinalDayOrThrow(1)(self);
|
1979
|
+
/**
|
1980
|
+
* Returns a copy of `self` where `ordinalDay` is set to the last day of the current year. All time
|
1981
|
+
* parts (`hour23`, `hour11`, `meridiem`, `minute`, `second`, `millisecond`) are left unchanged
|
1982
|
+
*
|
1983
|
+
* @category Offsetters
|
1984
|
+
*/
|
1985
|
+
export const toLastYearDay = self => setOrdinalDayOrThrow(pipe(self, _gregorianDate, GregorianDate.getYearDurationInDays))(self);
|
1986
|
+
/**
|
1987
|
+
* Returns a copy of `self` where `isoWeek` and `weekday` are set to 1. All time parts (`hour23`,
|
1988
|
+
* `hour11`, `meridiem`, `minute`, `second`, `millisecond`) are left unchanged
|
1989
|
+
*
|
1990
|
+
* @category Offsetters
|
1991
|
+
*/
|
1992
|
+
export const toFirstIsoYearDay = /*#__PURE__*/flow(/*#__PURE__*/setIsoWeekOrThrow(1), /*#__PURE__*/setWeekdayOrThrow(1));
|
1993
|
+
/**
|
1994
|
+
* Returns a copy of `self` where `isoWeek` is set to the last week of the current iso year.
|
1995
|
+
* `weekday` and all time parts (`hour23`, `hour11`, `meridiem`, `minute`, `second`, `millisecond`)
|
1996
|
+
* are left unchanged
|
1997
|
+
*
|
1998
|
+
* @category Offsetters
|
1999
|
+
*/
|
2000
|
+
export const toLastIsoYearWeek = self => pipe(self, setIsoWeekOrThrow(pipe(self, _isoDate, IsoDate.getLastIsoWeek)));
|
2001
|
+
/**
|
2002
|
+
* Returns a copy of `self` where `isoWeek` is set to the last week of the current iso year and
|
2003
|
+
* `weekday` is set to 7. All time parts (`hour23`, `hour11`, `meridiem`, `minute`, `second`,
|
2004
|
+
* `millisecond`) are left unchanged
|
2005
|
+
*
|
2006
|
+
* @category Offsetters
|
2007
|
+
*/
|
2008
|
+
export const toLastIsoYearDay = self => pipe(self, toLastIsoYearWeek, setWeekdayOrThrow(7));
|
2009
|
+
/**
|
2010
|
+
* If possible, returns a copy of `self` offset by `offset` years and having the same `month`,
|
2011
|
+
* `hour23`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. If `respectMonthEnd` is
|
2012
|
+
* true and `self` is on the last day of a month, the new DateTime object's monthDay will be the
|
2013
|
+
* last of the target month. Otherwise, it will be the same as `self`'s. Returns a `left` of an
|
2014
|
+
* error otherwise.
|
2015
|
+
*
|
2016
|
+
* @category Offsetters
|
2017
|
+
*/
|
2018
|
+
export const offsetYears = (offset, respectMonthEnd) => offsetMonths(offset * 12, respectMonthEnd);
|
2019
|
+
/**
|
2020
|
+
* Same as offsetYears but returns directly a DateTime or throws in case of an error
|
2021
|
+
*
|
2022
|
+
* @category Setters
|
2023
|
+
*/
|
2024
|
+
export const offsetYearsOrThrow = /*#__PURE__*/flow(offsetYears, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
2025
|
+
/**
|
2026
|
+
* If possible, returns a copy of `self` offset by `offset` months and having the same `hour23`,
|
2027
|
+
* `minute`, `second`, `millisecond` and `zoneOffset` as `self`. If `respectMonthEnd` is true and
|
2028
|
+
* `self` is on the last day of a month, the new DateTime object's monthDay will be the last of the
|
2029
|
+
* target month. Otherwise, it will be the same as `self`'s. Returns a `left` of an error if the
|
2030
|
+
* DateTime object can
|
2031
|
+
*
|
2032
|
+
* @category Offsetters
|
2033
|
+
*/
|
2034
|
+
export const offsetMonths = (offset, respectMonthEnd) => self => {
|
2035
|
+
const [yearOffset, targetMonthIndex] = pipe(self, getMonth, Number.sum(offset - 1), MNumber.quotientAndRemainder(12));
|
2036
|
+
const offsetToLastMonthDay = respectMonthEnd && isLastMonthDay(self);
|
2037
|
+
return pipe(self, offsetToLastMonthDay ? setMonthDayOrThrow(1) : Function.identity, setMonthOrThrow(targetMonthIndex + 1), setYear(getYear(self) + yearOffset), Either.map(MFunction.fIfTrue({
|
2038
|
+
condition: offsetToLastMonthDay,
|
2039
|
+
f: toLastMonthDay
|
2040
|
+
})));
|
2041
|
+
};
|
2042
|
+
/**
|
2043
|
+
* Same as offsetMonths but returns directly a DateTime or throws in case of an error
|
2044
|
+
*
|
2045
|
+
* @category Setters
|
2046
|
+
*/
|
2047
|
+
export const offsetMonthsOrThrow = /*#__PURE__*/flow(offsetMonths, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
2048
|
+
/**
|
2049
|
+
* Returns a copy of `self` offset by `offset` days
|
2050
|
+
*
|
2051
|
+
* @category Offsetters
|
2052
|
+
*/
|
2053
|
+
export const offsetDays = offset => offsetMilliseconds(offset * DAY_MS);
|
2054
|
+
/**
|
2055
|
+
* Same as offsetDays but returns directly a DateTime or throws in case of an error
|
2056
|
+
*
|
2057
|
+
* @category Setters
|
2058
|
+
*/
|
2059
|
+
export const offsetDaysOrThrow = /*#__PURE__*/flow(offsetDays, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
2060
|
+
/**
|
2061
|
+
* If possible, returns a copy of `self` offset by `offset` iso years and having the same `weekday`,
|
2062
|
+
* `hour23`, `minute`, `second`, `millisecond` and `zoneOffset` as `self`. If `respectYearEnd` is
|
2063
|
+
* true and `self` is on the last day of an iso year, the new DateTime object's isoWeek will be the
|
2064
|
+
* last of the target iso year. Otherwise, it will be the same as `self`'s. Returns a `left` of an
|
2065
|
+
* error otherwise.
|
2066
|
+
*
|
2067
|
+
* @category Offsetters
|
2068
|
+
*/
|
2069
|
+
export const offsetIsoYears = (offset, respectYearEnd) => self => {
|
2070
|
+
const offsetToLastIsoYearDay = respectYearEnd && isLastIsoYearDay(self);
|
2071
|
+
return pipe(self, offsetToLastIsoYearDay ? setIsoWeekOrThrow(1) : Function.identity, setIsoYear(getIsoYear(self) + offset), Either.map(MFunction.fIfTrue({
|
2072
|
+
condition: offsetToLastIsoYearDay,
|
2073
|
+
f: toLastIsoYearWeek
|
2074
|
+
})));
|
2075
|
+
};
|
2076
|
+
/**
|
2077
|
+
* Same as offsetIsoYears but returns directly a DateTime or throws in case of an error
|
2078
|
+
*
|
2079
|
+
* @category Setters
|
2080
|
+
*/
|
2081
|
+
export const offsetIsoYearsOrThrow = /*#__PURE__*/flow(offsetIsoYears, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
2082
|
+
/**
|
2083
|
+
* Returns a copy of `self` offset by `offset` hours
|
2084
|
+
*
|
2085
|
+
* @category Offsetters
|
2086
|
+
*/
|
2087
|
+
export const offsetHours = offset => offsetMilliseconds(offset * HOUR_MS);
|
2088
|
+
/**
|
2089
|
+
* Same as offsetHours but returns directly a DateTime or throws in case of an error
|
2090
|
+
*
|
2091
|
+
* @category Setters
|
2092
|
+
*/
|
2093
|
+
export const offsetHoursOrThrow = /*#__PURE__*/flow(offsetHours, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
2094
|
+
/**
|
2095
|
+
* Returns a copy of `self` offset by `offset` minutes
|
2096
|
+
*
|
2097
|
+
* @category Offsetters
|
2098
|
+
*/
|
2099
|
+
export const offsetMinutes = offset => offsetMilliseconds(offset * MINUTE_MS);
|
2100
|
+
/**
|
2101
|
+
* Same as offsetMinutes but returns directly a DateTime or throws in case of an error
|
2102
|
+
*
|
2103
|
+
* @category Setters
|
2104
|
+
*/
|
2105
|
+
export const offsetMinutesOrThrow = /*#__PURE__*/flow(offsetMinutes, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
2106
|
+
/**
|
2107
|
+
* Returns a copy of `self` offset by `offset` seconds
|
2108
|
+
*
|
2109
|
+
* @category Offsetters
|
2110
|
+
*/
|
2111
|
+
export const offsetSeconds = offset => offsetMilliseconds(offset * SECOND_MS);
|
2112
|
+
/**
|
2113
|
+
* Same as offsetSeconds but returns directly a DateTime or throws in case of an error
|
2114
|
+
*
|
2115
|
+
* @category Setters
|
2116
|
+
*/
|
2117
|
+
export const offsetSecondsOrThrow = /*#__PURE__*/flow(offsetSeconds, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
2118
|
+
/**
|
2119
|
+
* Returns a copy of `self` offset by `offset` milliseconds
|
2120
|
+
*
|
2121
|
+
* @category Offsetters
|
2122
|
+
*/
|
2123
|
+
export const offsetMilliseconds = offset => self => _setTimestamp(timestamp(self) + offset)(self);
|
2124
|
+
/**
|
2125
|
+
* Same as offsetMilliseconds but returns directly a DateTime or throws in case of an error
|
2126
|
+
*
|
2127
|
+
* @category Setters
|
2128
|
+
*/
|
2129
|
+
export const offsetMillisecondsOrThrow = /*#__PURE__*/flow(offsetMilliseconds, /*#__PURE__*/Function.compose(/*#__PURE__*/Either.getOrThrowWith(Function.identity)));
|
2130
|
+
//# sourceMappingURL=DateTime.js.map
|