scdate 0.2.4 → 0.2.6
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/dist/internal/SDate.d.ts +3 -0
- package/dist/internal/SDate.js +3 -0
- package/dist/internal/STime.d.ts +3 -0
- package/dist/internal/STime.js +3 -0
- package/dist/internal/STimestamp.d.ts +3 -0
- package/dist/internal/STimestamp.js +3 -0
- package/dist/internal/SWeekdays.d.ts +7 -0
- package/dist/internal/SWeekdays.js +7 -0
- package/dist/internal/date.js +1 -0
- package/dist/internal/time.js +2 -0
- package/dist/internal/utils.d.ts +4 -0
- package/dist/internal/utils.js +4 -0
- package/dist/internal/zoned.d.ts +4 -0
- package/dist/internal/zoned.js +4 -0
- package/dist/sDate.d.ts +280 -0
- package/dist/sDate.js +283 -0
- package/dist/sTime.d.ts +131 -0
- package/dist/sTime.js +131 -0
- package/dist/sTimestamp.d.ts +266 -0
- package/dist/sTimestamp.js +266 -0
- package/dist/sWeekdays.d.ts +116 -0
- package/dist/sWeekdays.js +118 -0
- package/package.json +9 -9
package/dist/sTimestamp.js
CHANGED
|
@@ -4,43 +4,187 @@ import { getISODateFromISOTimestamp, getISOTimeFromISOTimestamp, getISOTimestamp
|
|
|
4
4
|
import { getMillisecondsInUTCFromTimestamp, getTimeZonedDate, } from './internal/zoned.js';
|
|
5
5
|
import { getShortDateString, sDate } from './sDate.js';
|
|
6
6
|
import { get12HourTimeString, sTime } from './sTime.js';
|
|
7
|
+
/**
|
|
8
|
+
* --- Factory ---
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Returns a new STimestamp instance.
|
|
12
|
+
*
|
|
13
|
+
* @param timestamp An instance of STimestamp or a string in the
|
|
14
|
+
* YYYY-MM-DDTHH:MM format.
|
|
15
|
+
*/
|
|
7
16
|
export const sTimestamp = (timestamp) => {
|
|
8
17
|
if (timestamp instanceof STimestamp) {
|
|
9
18
|
return timestamp;
|
|
10
19
|
}
|
|
11
20
|
return new STimestamp(timestamp);
|
|
12
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* --- Factory helpers ---
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Returns a new STimestamp instance set to the date and time that results from
|
|
27
|
+
* converting the given number of milliseconds since the Unix epoch to the given
|
|
28
|
+
* time zone.
|
|
29
|
+
*
|
|
30
|
+
* @param utcDateInMilliseconds The number of milliseconds since the Unix epoch.
|
|
31
|
+
* @param timeZone The time zone to use when creating the timestamp. See
|
|
32
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
33
|
+
*/
|
|
13
34
|
export const getTimestampFromUTCMilliseconds = (utcDateInMilliseconds, timeZone) => {
|
|
14
35
|
const date = getTimeZonedDate(utcDateInMilliseconds, timeZone);
|
|
15
36
|
return sTimestamp(getISOTimestampFromZonedDate(date));
|
|
16
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* Returns a new STimestamp instance set to the current date and time in the
|
|
40
|
+
* given time zone.
|
|
41
|
+
*
|
|
42
|
+
* @param timeZone The time zone to use when creating the timestamp. See
|
|
43
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
44
|
+
*/
|
|
17
45
|
export const getTimestampNow = (timeZone) => {
|
|
18
46
|
return getTimestampFromUTCMilliseconds(Date.now(), timeZone);
|
|
19
47
|
};
|
|
48
|
+
/**
|
|
49
|
+
* Returns a new STimestamp instance set to the date and time that results from
|
|
50
|
+
* combining the given date and time.
|
|
51
|
+
*
|
|
52
|
+
* @param date The date to use when creating the timestamp. It can be an SDate
|
|
53
|
+
* or a string in the YYYY-MM-DD format.
|
|
54
|
+
* @param time The time to use when creating the timestamp. It can be an STime
|
|
55
|
+
* or a string in the HH:MM format.
|
|
56
|
+
*/
|
|
20
57
|
export const getTimestampFromDateAndTime = (date, time) => {
|
|
21
58
|
const sDateValue = sDate(date);
|
|
22
59
|
const sDimeValue = sTime(time);
|
|
23
60
|
return sTimestamp(`${sDateValue.date}T${sDimeValue.time}`);
|
|
24
61
|
};
|
|
62
|
+
/**
|
|
63
|
+
* --- Getters ---
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Returns a native Date adjusted so that the local time of that date matches
|
|
67
|
+
* the local timestamp at the specified time zone.
|
|
68
|
+
*
|
|
69
|
+
* @param date The date to get the time zoned date from. It can be an SDate or a
|
|
70
|
+
* string in the YYYY-MM-DD format.
|
|
71
|
+
* @param timeZone The time zone to adjust the date to. See
|
|
72
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
73
|
+
*/
|
|
25
74
|
export const getTimeZonedDateFromTimestamp = (timestamp, timeZone) => {
|
|
26
75
|
const sTimestampValue = sTimestamp(timestamp);
|
|
27
76
|
const dateInUTCMilliseconds = getMillisecondsInUTCFromTimestamp(sTimestampValue, timeZone);
|
|
28
77
|
return getTimeZonedDate(dateInUTCMilliseconds, timeZone);
|
|
29
78
|
};
|
|
79
|
+
/**
|
|
80
|
+
* Returns the number of seconds from now to the given timestamp. If the
|
|
81
|
+
* timestamp is in the future, the number of seconds will be positive. If the
|
|
82
|
+
* timestamp is in the past, the number of seconds will be negative.
|
|
83
|
+
*
|
|
84
|
+
* Daylight Saving Notes:
|
|
85
|
+
*
|
|
86
|
+
* The following notes use `America/New_York` as the time zone to explain how
|
|
87
|
+
* Daylight Savings is handled, but it works the same across time zones on their
|
|
88
|
+
* respective Daylight Saving schedules as defined by Intl API.
|
|
89
|
+
*
|
|
90
|
+
* Notice that when looking at a watch (that adjusts for Daylight Saving
|
|
91
|
+
* automatically) on 2024-03-10 (the day Daylight Savings goes into effect and
|
|
92
|
+
* the time moves forward one hour) the times between 02:00 and 02:59 never
|
|
93
|
+
* happen as the watch goes from 01:59 to 03:00. In the case of 2024-11-03 (the
|
|
94
|
+
* day the time zone goes back to Standard Time), the times between 01:00 and
|
|
95
|
+
* 01:59 happen twice as the first time the watch hits 02:00 it goes back to
|
|
96
|
+
* 01:00.
|
|
97
|
+
*
|
|
98
|
+
* To account for time zone changes, this method converts the timestamp to UTC
|
|
99
|
+
* milliseconds (for both the current time and the expected timestamp) before
|
|
100
|
+
* calculating the difference in seconds. This works as expected for all times,
|
|
101
|
+
* but the expectation for the transition times (those that don't happen on a
|
|
102
|
+
* watch that automatically adjusts or that happen twice) it can work in
|
|
103
|
+
* unexpected ways.
|
|
104
|
+
*
|
|
105
|
+
* For example, trying to calculate the number of seconds to 2024-03-10T02:00
|
|
106
|
+
* (the start of Daylight Saving Time) at 2024-03-10T01:59 (still in Standard
|
|
107
|
+
* Time) will result in -3540 seconds (59 minutes in the past). A similar
|
|
108
|
+
* situation happens when the time zone transitions from Daylight Saving Time to
|
|
109
|
+
* Standard Time as can be derived from the table below.
|
|
110
|
+
*
|
|
111
|
+
* In 'America/New_York'
|
|
112
|
+
*
|
|
113
|
+
* Transition to Eastern Daylight Time (EDT) in 2024
|
|
114
|
+
* | Time Zone | T1 | T2 | T3 |
|
|
115
|
+
* |------------------|-----------------------|------------------------|-----------------------|
|
|
116
|
+
* | America/New_York | 2024-03-10T01:59(EST) | 2024-03-10T02:00(EDT) | 2024-03-10T03:00(EST) |
|
|
117
|
+
* | UTC | 2024-03-10T06:59 | 2024-03-10T06:00 | 2024-03-10T07:00 |
|
|
118
|
+
*
|
|
119
|
+
* Transition to Eastern Standard Time (EST) in 2024
|
|
120
|
+
* | Time Zone | T1 | T2 | T3 |
|
|
121
|
+
* |------------------|-----------------------|------------------------|-----------------------|
|
|
122
|
+
* | America/New_York | 2024-11-03T01:59(EDT) | 2024-11-03T02:00(EST) | 2024-11-03T03:00(EST) |
|
|
123
|
+
* | UTC | 2024-11-03T05:59 | 2024-11-03T07:00 | 2024-11-03T08:00 |
|
|
124
|
+
*
|
|
125
|
+
* @param timestamp The timestamp to get the seconds to. It can be an STimestamp
|
|
126
|
+
* or a string in the YYYY-MM-DDTHH:MM format.
|
|
127
|
+
* @param timeZone The time zone to use when creating the timestamp. See
|
|
128
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
129
|
+
*/
|
|
30
130
|
export const getSecondsToTimestamp = (timestamp, timeZone) => {
|
|
31
131
|
const sTimestampValue = sTimestamp(timestamp);
|
|
32
132
|
const millisecondsNow = Date.now();
|
|
33
133
|
const millisecondsAtTimestamp = getMillisecondsInUTCFromTimestamp(sTimestampValue, timeZone);
|
|
34
134
|
return Math.floor((millisecondsAtTimestamp - millisecondsNow) / MillisecondsInSecond);
|
|
35
135
|
};
|
|
136
|
+
/**
|
|
137
|
+
* Returns a new SDate instance set to the date part of the given timestamp.
|
|
138
|
+
*
|
|
139
|
+
* @param timestamp The timestamp to get the date from. It can be an STimestamp
|
|
140
|
+
* or a string in the YYYY-MM-DDTHH:MM format.
|
|
141
|
+
*/
|
|
36
142
|
export const getDateFromTimestamp = (timestamp) => {
|
|
37
143
|
const sTimestampValue = sTimestamp(timestamp);
|
|
38
144
|
return sDate(getISODateFromISOTimestamp(sTimestampValue.timestamp));
|
|
39
145
|
};
|
|
146
|
+
/**
|
|
147
|
+
* Returns a new STime instance set to the time part of the given timestamp.
|
|
148
|
+
*
|
|
149
|
+
* @param timestamp The timestamp to get the time from. It can be an STimestamp
|
|
150
|
+
* or a string in the YYYY-MM-DDTHH:MM format.
|
|
151
|
+
*/
|
|
40
152
|
export const getTimeFromTimestamp = (timestamp) => {
|
|
41
153
|
const sTimestampValue = sTimestamp(timestamp);
|
|
42
154
|
return sTime(getISOTimeFromISOTimestamp(sTimestampValue.timestamp));
|
|
43
155
|
};
|
|
156
|
+
/**
|
|
157
|
+
* Returns a string representation that includes a minimum set of components
|
|
158
|
+
* from the given timestamp. This is a combination of the the results of
|
|
159
|
+
* the `getShortDateString` method, and `get12HourTimeString`.
|
|
160
|
+
*
|
|
161
|
+
* @param timestamp The timestamp to get the short string from. It can be an
|
|
162
|
+
* STimestamp or a string in the YYYY-MM-DDTHH:MM format.
|
|
163
|
+
* @param timeZone The time zone to use when creating the short string. See
|
|
164
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
165
|
+
* @param locale The locale to use for the string representation.
|
|
166
|
+
* @param options An object with options for the short string representation.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* // Example when the timestamp is today
|
|
171
|
+
* getShortTimestampString('2021-08-10T08:00', 'America/Puerto_Rico', 'en', {
|
|
172
|
+
* includeWeekday: true,
|
|
173
|
+
* onTodayAtText: () => 'Today at',
|
|
174
|
+
* })
|
|
175
|
+
* //=> 'Today at 8:00 AM'
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* // Example when the timestamp is not today
|
|
181
|
+
* getShortTimestampString('2022-09-11T08:00', 'America/Puerto_Rico', 'es', {
|
|
182
|
+
* includeWeekday: true,
|
|
183
|
+
* onTodayAtText: () => 'Hoy a las',
|
|
184
|
+
* })
|
|
185
|
+
* //=> 'dom, 11 sept 22 8:00 AM'
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
44
188
|
export const getShortTimestampString = (timestamp, timeZone, locale, options) => {
|
|
45
189
|
const sTimestampValue = sTimestamp(timestamp);
|
|
46
190
|
const date = getDateFromTimestamp(sTimestampValue);
|
|
@@ -52,12 +196,86 @@ export const getShortTimestampString = (timestamp, timeZone, locale, options) =>
|
|
|
52
196
|
const timeText = get12HourTimeString(time);
|
|
53
197
|
return `${dateText} ${timeText}`;
|
|
54
198
|
};
|
|
199
|
+
/**
|
|
200
|
+
* --- Operations ---
|
|
201
|
+
*/
|
|
202
|
+
/**
|
|
203
|
+
* Returns a new STimestamp instance resulting from adding the given number of
|
|
204
|
+
* calendar days to the given timestamp. Because it adds calendar days rather
|
|
205
|
+
* than 24-hour days, this operation is not affected by time zones.
|
|
206
|
+
*
|
|
207
|
+
* @param timestamp The timestamp to add days to. It can be an STimestamp or a
|
|
208
|
+
* string in the YYYY-MM-DDTHH:MM format.
|
|
209
|
+
* @param days The number of days to add to the timestamp.
|
|
210
|
+
*/
|
|
55
211
|
export const addDaysToTimestamp = (timestamp, days) => {
|
|
56
212
|
const sTimestampValue = sTimestamp(timestamp);
|
|
57
213
|
const nativeDate = getTimestampAsUTCDateMini(sTimestampValue);
|
|
58
214
|
nativeDate.setDate(nativeDate.getDate() + days);
|
|
59
215
|
return sTimestamp(getISOTimestampFromZonedDate(nativeDate));
|
|
60
216
|
};
|
|
217
|
+
/**
|
|
218
|
+
* Returns a new STimestamp instance resulting from adding the given number of
|
|
219
|
+
* minutes to the given timestamp.
|
|
220
|
+
*
|
|
221
|
+
* Daylight Saving Time Notes:
|
|
222
|
+
*
|
|
223
|
+
* The following notes use `America/New_York` as the time zone to explain how
|
|
224
|
+
* Daylight Savings is handled, but it works the same across time zones on their
|
|
225
|
+
* respective Daylight Saving schedules as defined by Intl API.
|
|
226
|
+
*
|
|
227
|
+
* Notice that when looking at a watch (that adjusts for Daylight Saving
|
|
228
|
+
* automatically) on 2024-03-10 (the day Daylight Savings goes into effect and
|
|
229
|
+
* the time moves forward one hour) the times between 02:00 and 02:59 never
|
|
230
|
+
* happen as the watch goes from 01:59 to 03:00. In the case of 2024-11-03 (the
|
|
231
|
+
* day the time zone goes back to Standard Time), the times between 01:00 and
|
|
232
|
+
* 01:59 happen twice as the first time the watch hits 02:00 it goes back to
|
|
233
|
+
* 01:00.
|
|
234
|
+
*
|
|
235
|
+
* To account for time zone changes, this method converts the timestamp to UTC
|
|
236
|
+
* milliseconds before adding the specified minutes. This works as expected for
|
|
237
|
+
* all times, but the expectation for the transition times (those that don't
|
|
238
|
+
* happen on a watch that automatically adjusts or that happen twice) it can
|
|
239
|
+
* work in unexpected ways.
|
|
240
|
+
*
|
|
241
|
+
* Time is converted from the given time zone to
|
|
242
|
+
* UTC before the minutes are added, and then converted back to the specified
|
|
243
|
+
* time zone. This results in the resulting time being adjusted for daylight saving time
|
|
244
|
+
* changes. (e.g. Adding 60 minutes to 2024-03-10T01:59 in America/New_York will
|
|
245
|
+
* result in 2024-03-10T03:59 as time move forward one hour for daylight saving
|
|
246
|
+
* time at 2024-03-10T02:00.)
|
|
247
|
+
*
|
|
248
|
+
* For example, adding one minute to 2024-03-10T01:59 will result in
|
|
249
|
+
* 2024-03-10T03:00 as expected. However, trying to add one minute to
|
|
250
|
+
* 2024-03-10T02:00 (a time that technically does not exist on a watch that
|
|
251
|
+
* automatically adjusts for Daylight Saving) will result in 2024-03-10T01:01.
|
|
252
|
+
* This is because 2024-03-10T02:00 is converted to 2024-03-10T06:00 UTC (due
|
|
253
|
+
* to timezone offset being -4 starting from 02:00 local time) and one minute
|
|
254
|
+
* later would be 2024-03-10T06:01 UTC which would be 2024-03-10T01:01 in
|
|
255
|
+
* `America/New_York`. A similar situation happens when the time zone
|
|
256
|
+
* transitions from Daylight Saving Time to Standard Time as can be derived from
|
|
257
|
+
* the table below.
|
|
258
|
+
*
|
|
259
|
+
* In 'America/New_York'
|
|
260
|
+
*
|
|
261
|
+
* Transition to Eastern Daylight Time (EDT) in 2024
|
|
262
|
+
* | Time Zone | T1 | T2 | T3 |
|
|
263
|
+
* |------------------|-----------------------|------------------------|-----------------------|
|
|
264
|
+
* | America/New_York | 2024-03-10T01:59(EST) | 2024-03-10T02:00(EDT) | 2024-03-10T03:00(EST) |
|
|
265
|
+
* | UTC | 2024-03-10T06:59 | 2024-03-10T06:00 | 2024-03-10T07:00 |
|
|
266
|
+
*
|
|
267
|
+
* Transition to Eastern Standard Time (EST) in 2024
|
|
268
|
+
* | Time Zone | T1 | T2 | T3 |
|
|
269
|
+
* |------------------|-----------------------|------------------------|-----------------------|
|
|
270
|
+
* | America/New_York | 2024-11-03T01:59(EDT) | 2024-11-03T02:00(EST) | 2024-11-03T03:00(EST) |
|
|
271
|
+
* | UTC | 2024-11-03T05:59 | 2024-11-03T07:00 | 2024-11-03T08:00 |
|
|
272
|
+
*
|
|
273
|
+
* @param timestamp The timestamp to add minutes to. It can be an STimestamp or
|
|
274
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
275
|
+
* @param minutes The number of minutes to add to the timestamp.
|
|
276
|
+
* @param timeZone The time zone to use when creating the timestamp. See
|
|
277
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
278
|
+
*/
|
|
61
279
|
export const addMinutesToTimestamp = (timestamp, minutes, timeZone) => {
|
|
62
280
|
const sTimestampValue = sTimestamp(timestamp);
|
|
63
281
|
const newMillisecondsInUTC = getMillisecondsInUTCFromTimestamp(sTimestampValue, timeZone) +
|
|
@@ -65,26 +283,74 @@ export const addMinutesToTimestamp = (timestamp, minutes, timeZone) => {
|
|
|
65
283
|
const newTimestamp = getTimestampFromUTCMilliseconds(newMillisecondsInUTC, timeZone);
|
|
66
284
|
return newTimestamp;
|
|
67
285
|
};
|
|
286
|
+
/**
|
|
287
|
+
* --- Comparisons ---
|
|
288
|
+
*/
|
|
289
|
+
/**
|
|
290
|
+
* Returns true if the two timestamps represent the same date and time. Returns
|
|
291
|
+
* false otherwise.
|
|
292
|
+
*
|
|
293
|
+
* @param timestamp1 The first timestamp to compare. It can be an STimestamp or
|
|
294
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
295
|
+
* @param timestamp2 The second timestamp to compare. It can be an STimestamp or
|
|
296
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
297
|
+
*/
|
|
68
298
|
export const isSameTimestamp = (timestamp1, timestamp2) => {
|
|
69
299
|
const sTimestamp1 = sTimestamp(timestamp1);
|
|
70
300
|
const sTimestamp2 = sTimestamp(timestamp2);
|
|
71
301
|
return sTimestamp1.timestamp === sTimestamp2.timestamp;
|
|
72
302
|
};
|
|
303
|
+
/**
|
|
304
|
+
* Returns true if the first timestamp represents a date and time that happens
|
|
305
|
+
* before the second timestamp. Returns false otherwise.
|
|
306
|
+
*
|
|
307
|
+
* @param timestamp1 The first timestamp to compare. It can be an STimestamp or
|
|
308
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
309
|
+
* @param timestamp2 The second timestamp to compare. It can be an STimestamp or
|
|
310
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
311
|
+
*/
|
|
73
312
|
export const isBeforeTimestamp = (timestamp1, timestamp2) => {
|
|
74
313
|
const sTimestamp1 = sTimestamp(timestamp1);
|
|
75
314
|
const sTimestamp2 = sTimestamp(timestamp2);
|
|
76
315
|
return sTimestamp1.timestamp < sTimestamp2.timestamp;
|
|
77
316
|
};
|
|
317
|
+
/**
|
|
318
|
+
* Returns true if the first timestamp represents a date and time that happens
|
|
319
|
+
* on or before the second timestamp. Returns false otherwise.
|
|
320
|
+
*
|
|
321
|
+
* @param timestamp1 The first timestamp to compare. It can be an STimestamp or
|
|
322
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
323
|
+
* @param timestamp2 The second timestamp to compare. It can be an STimestamp or
|
|
324
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
325
|
+
*/
|
|
78
326
|
export const isSameTimestampOrBefore = (timestamp1, timestamp2) => {
|
|
79
327
|
const sTimestamp1 = sTimestamp(timestamp1);
|
|
80
328
|
const sTimestamp2 = sTimestamp(timestamp2);
|
|
81
329
|
return sTimestamp1.timestamp <= sTimestamp2.timestamp;
|
|
82
330
|
};
|
|
331
|
+
/**
|
|
332
|
+
* Returns true if the first timestamp represents a date and time that happens
|
|
333
|
+
* after the second timestamp. Returns false otherwise.
|
|
334
|
+
*
|
|
335
|
+
* @param timestamp1 The first timestamp to compare. It can be an STimestamp or
|
|
336
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
337
|
+
* @param timestamp2 The second timestamp to compare. It can be an STimestamp or
|
|
338
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
339
|
+
*/
|
|
83
340
|
export const isAfterTimestamp = (timestamp1, timestamp2) => {
|
|
84
341
|
const sTimestamp1 = sTimestamp(timestamp1);
|
|
85
342
|
const sTimestamp2 = sTimestamp(timestamp2);
|
|
86
343
|
return sTimestamp1.timestamp > sTimestamp2.timestamp;
|
|
87
344
|
};
|
|
345
|
+
/**
|
|
346
|
+
* Returns true if the first timestamp represents a date and time that happens
|
|
347
|
+
* on or after the second timestamp. Returns false otherwise.
|
|
348
|
+
*
|
|
349
|
+
* @param timestamp1 The first timestamp to compare. It can be an STimestamp or
|
|
350
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
351
|
+
* @param timestamp2 The second timestamp to compare. It can be an STimestamp or
|
|
352
|
+
* a string in the YYYY-MM-DDTHH:MM format.
|
|
353
|
+
*/
|
|
88
354
|
export const isSameTimestampOrAfter = (timestamp1, timestamp2) => {
|
|
89
355
|
const sTimestamp1 = sTimestamp(timestamp1);
|
|
90
356
|
const sTimestamp2 = sTimestamp(timestamp2);
|
package/dist/sWeekdays.d.ts
CHANGED
|
@@ -1,12 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* --- Factory ---
|
|
3
|
+
*/
|
|
1
4
|
import { Weekday } from './constants.js';
|
|
2
5
|
import { SDate } from './internal/SDate.js';
|
|
3
6
|
import { SWeekdays } from './internal/SWeekdays.js';
|
|
7
|
+
/**
|
|
8
|
+
* Returns a new SWeekdays instance.
|
|
9
|
+
*
|
|
10
|
+
* @param weekdays An instance of SWeekdays that will be returned or a string in
|
|
11
|
+
* the SMTWTFS format. Each character in the string represents a weekday
|
|
12
|
+
* starting on Sunday and ending on Saturday using the first letter of the
|
|
13
|
+
* English word for the week day. If the weekday is excluded, the position is
|
|
14
|
+
* filled with a '-' character.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* sWeekdays('SM----S')
|
|
19
|
+
* // Returns an instance of SWeekdays with the weekdays Sunday, Monday, and
|
|
20
|
+
* // Saturday included while the rest are excluded.
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* sWeekdays('SMTWTFS')
|
|
26
|
+
* // Returns an instance of SWeekdays with all weekdays included.
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
4
29
|
export declare const sWeekdays: (weekdays: string | SWeekdays) => SWeekdays;
|
|
30
|
+
/**
|
|
31
|
+
* --- Factory helpers ---
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* Returns a new SWeekdays instance with all provided weekdays included. The
|
|
35
|
+
* provided weekdays can be any combination of the Weekday enum values.
|
|
36
|
+
*
|
|
37
|
+
* @param weekdays A combination of the Weekday enum values.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* getWeekdaysFromWeekdayFlags(Weekday.Monday | Weekday.Wednesday | Weekday.Friday)
|
|
42
|
+
* // Returns an instance of SWeekdays with the weekdays Monday, Wednesday, and
|
|
43
|
+
* // Friday included while the rest are excluded.
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* getWeekdaysFromWeekdayFlags(Weekday.Tuesday)
|
|
49
|
+
* // Returns an instance of SWeekdays with the weekday Tuesday included while
|
|
50
|
+
* // the rest are excluded.
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
5
53
|
export declare const getWeekdaysFromWeekdayFlags: (weekdays: Weekday) => SWeekdays;
|
|
54
|
+
/**
|
|
55
|
+
* Returns a new SWeekdays instance with all weekdays included.
|
|
56
|
+
*/
|
|
6
57
|
export declare const getWeekdaysWithAllIncluded: () => SWeekdays;
|
|
58
|
+
/**
|
|
59
|
+
* Returns a new SWeekdays instance with no weekdays included.
|
|
60
|
+
*/
|
|
7
61
|
export declare const getWeekdaysWithNoneIncluded: () => SWeekdays;
|
|
62
|
+
/**
|
|
63
|
+
* --- Operations ---
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Returns a new SWeekdays instance with the weekdays shifted forward by one
|
|
67
|
+
* day.
|
|
68
|
+
*
|
|
69
|
+
* @param weekdays The weekdays to shift forward. It can be an SWeekdays or a
|
|
70
|
+
* string in the SMTWTFS format.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* shiftWeekdaysForward('SM----S')
|
|
75
|
+
* // Returns an instance of SWeekdays with the weekdays shifted forward by one
|
|
76
|
+
* // day. 'SM----S' becomes 'SMT----'.
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
8
79
|
export declare const shiftWeekdaysForward: (weekdays: string | SWeekdays) => SWeekdays;
|
|
80
|
+
/**
|
|
81
|
+
* Returns a new SWeekdays instance where only the weekdays that are within the
|
|
82
|
+
* provided date range are included.
|
|
83
|
+
*
|
|
84
|
+
* @param weekdays The weekdays to filter. It can be an SWeekdays or a string in
|
|
85
|
+
* the SMTWTFS format.
|
|
86
|
+
* @param fromDate The start date of the range. It can be an SDate or a string
|
|
87
|
+
* in the YYYY-MM-DD format.
|
|
88
|
+
* @param toDate The end date of the range. It can be an SDate or a string in
|
|
89
|
+
* the YYYY-MM-DD format.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* filterWeekdaysForDates('SMTWTFS', '2020-03-05', '2020-03-05')
|
|
94
|
+
* // Returns an instance of SWeekdays with only Thursday included.
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
9
97
|
export declare const filterWeekdaysForDates: (weekdays: string | SWeekdays, fromDate: string | SDate, toDate: string | SDate) => SWeekdays;
|
|
98
|
+
/**
|
|
99
|
+
* Returns a new SWeekdays instance with the provided weekday added to the
|
|
100
|
+
* current set of weekdays.
|
|
101
|
+
*
|
|
102
|
+
* @param weekdays The weekdays to add the weekday to. It can be an SWeekdays or
|
|
103
|
+
* a string in the SMTWTFS format.
|
|
104
|
+
* @param weekdayToAdd The weekday to add.
|
|
105
|
+
*/
|
|
10
106
|
export declare const addWeekdayToWeekdays: (weekdays: string | SWeekdays, weekdayToAdd: Weekday) => SWeekdays;
|
|
107
|
+
/**
|
|
108
|
+
* --- Comparisons ---
|
|
109
|
+
*/
|
|
110
|
+
/**
|
|
111
|
+
* Returns true if the provided weekdays include the provided weekday. Returns
|
|
112
|
+
* false otherwise.
|
|
113
|
+
*
|
|
114
|
+
* @param weekdays The weekdays to check. It can be an SWeekdays or a string in
|
|
115
|
+
* the SMTWTFS format.
|
|
116
|
+
* @param weekday The weekday to check.
|
|
117
|
+
*/
|
|
11
118
|
export declare const doesWeekdaysIncludeWeekday: (weekdays: string | SWeekdays, weekday: Weekday) => boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Returns true if any of the included weekdays in weekdays1 is also included in
|
|
121
|
+
* weekdays2. Returns false otherwise.
|
|
122
|
+
*
|
|
123
|
+
* @param weekdays1 The first set of weekdays to compare. It can be an SWeekdays
|
|
124
|
+
* or a string in the SMTWTFS format.
|
|
125
|
+
* @param weekdays2 The second set of weekdays to compare. It can be an
|
|
126
|
+
* SWeekdays or a string in the SMTWTFS format.
|
|
127
|
+
*/
|
|
12
128
|
export declare const doesWeekdaysHaveOverlapWithWeekdays: (weekdays1: string | SWeekdays, weekdays2: string | SWeekdays) => boolean;
|
package/dist/sWeekdays.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* --- Factory ---
|
|
3
|
+
*/
|
|
1
4
|
import { SWeekdays } from './internal/SWeekdays.js';
|
|
2
5
|
import { DayToWeekday, DaysInWeek } from './internal/constants.js';
|
|
3
6
|
import { getAtIndex, hasFlag } from './internal/utils.js';
|
|
@@ -7,12 +10,57 @@ const AllWeekdaysIncludedMask = 'SMTWTFS';
|
|
|
7
10
|
const NotIncludedDay = '-';
|
|
8
11
|
const NoWeekdaysIncluded = NotIncludedDay.repeat(AllWeekdaysIncludedMask.length);
|
|
9
12
|
const WeekdaysCount = AllWeekdaysIncludedMask.length;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a new SWeekdays instance.
|
|
15
|
+
*
|
|
16
|
+
* @param weekdays An instance of SWeekdays that will be returned or a string in
|
|
17
|
+
* the SMTWTFS format. Each character in the string represents a weekday
|
|
18
|
+
* starting on Sunday and ending on Saturday using the first letter of the
|
|
19
|
+
* English word for the week day. If the weekday is excluded, the position is
|
|
20
|
+
* filled with a '-' character.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* sWeekdays('SM----S')
|
|
25
|
+
* // Returns an instance of SWeekdays with the weekdays Sunday, Monday, and
|
|
26
|
+
* // Saturday included while the rest are excluded.
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* sWeekdays('SMTWTFS')
|
|
32
|
+
* // Returns an instance of SWeekdays with all weekdays included.
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
10
35
|
export const sWeekdays = (weekdays) => {
|
|
11
36
|
if (weekdays instanceof SWeekdays) {
|
|
12
37
|
return weekdays;
|
|
13
38
|
}
|
|
14
39
|
return new SWeekdays(weekdays);
|
|
15
40
|
};
|
|
41
|
+
/**
|
|
42
|
+
* --- Factory helpers ---
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Returns a new SWeekdays instance with all provided weekdays included. The
|
|
46
|
+
* provided weekdays can be any combination of the Weekday enum values.
|
|
47
|
+
*
|
|
48
|
+
* @param weekdays A combination of the Weekday enum values.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* getWeekdaysFromWeekdayFlags(Weekday.Monday | Weekday.Wednesday | Weekday.Friday)
|
|
53
|
+
* // Returns an instance of SWeekdays with the weekdays Monday, Wednesday, and
|
|
54
|
+
* // Friday included while the rest are excluded.
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* getWeekdaysFromWeekdayFlags(Weekday.Tuesday)
|
|
60
|
+
* // Returns an instance of SWeekdays with the weekday Tuesday included while
|
|
61
|
+
* // the rest are excluded.
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
16
64
|
export const getWeekdaysFromWeekdayFlags = (weekdays) => {
|
|
17
65
|
const newWeekdays = [...AllWeekdaysIncludedMask];
|
|
18
66
|
for (let i = 0; i < DayToWeekday.length; i++) {
|
|
@@ -23,12 +71,35 @@ export const getWeekdaysFromWeekdayFlags = (weekdays) => {
|
|
|
23
71
|
}
|
|
24
72
|
return sWeekdays(newWeekdays.join(''));
|
|
25
73
|
};
|
|
74
|
+
/**
|
|
75
|
+
* Returns a new SWeekdays instance with all weekdays included.
|
|
76
|
+
*/
|
|
26
77
|
export const getWeekdaysWithAllIncluded = () => {
|
|
27
78
|
return sWeekdays(AllWeekdaysIncludedMask);
|
|
28
79
|
};
|
|
80
|
+
/**
|
|
81
|
+
* Returns a new SWeekdays instance with no weekdays included.
|
|
82
|
+
*/
|
|
29
83
|
export const getWeekdaysWithNoneIncluded = () => {
|
|
30
84
|
return sWeekdays(NoWeekdaysIncluded);
|
|
31
85
|
};
|
|
86
|
+
/**
|
|
87
|
+
* --- Operations ---
|
|
88
|
+
*/
|
|
89
|
+
/**
|
|
90
|
+
* Returns a new SWeekdays instance with the weekdays shifted forward by one
|
|
91
|
+
* day.
|
|
92
|
+
*
|
|
93
|
+
* @param weekdays The weekdays to shift forward. It can be an SWeekdays or a
|
|
94
|
+
* string in the SMTWTFS format.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* shiftWeekdaysForward('SM----S')
|
|
99
|
+
* // Returns an instance of SWeekdays with the weekdays shifted forward by one
|
|
100
|
+
* // day. 'SM----S' becomes 'SMT----'.
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
32
103
|
export const shiftWeekdaysForward = (weekdays) => {
|
|
33
104
|
const sWeekdaysInstance = sWeekdays(weekdays);
|
|
34
105
|
const after = [...NoWeekdaysIncluded];
|
|
@@ -42,6 +113,23 @@ export const shiftWeekdaysForward = (weekdays) => {
|
|
|
42
113
|
}
|
|
43
114
|
return sWeekdays(after.join(''));
|
|
44
115
|
};
|
|
116
|
+
/**
|
|
117
|
+
* Returns a new SWeekdays instance where only the weekdays that are within the
|
|
118
|
+
* provided date range are included.
|
|
119
|
+
*
|
|
120
|
+
* @param weekdays The weekdays to filter. It can be an SWeekdays or a string in
|
|
121
|
+
* the SMTWTFS format.
|
|
122
|
+
* @param fromDate The start date of the range. It can be an SDate or a string
|
|
123
|
+
* in the YYYY-MM-DD format.
|
|
124
|
+
* @param toDate The end date of the range. It can be an SDate or a string in
|
|
125
|
+
* the YYYY-MM-DD format.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* filterWeekdaysForDates('SMTWTFS', '2020-03-05', '2020-03-05')
|
|
130
|
+
* // Returns an instance of SWeekdays with only Thursday included.
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
45
133
|
export const filterWeekdaysForDates = (weekdays, fromDate, toDate) => {
|
|
46
134
|
const sWeekdaysInstance = sWeekdays(weekdays);
|
|
47
135
|
const sFromDate = sDate(fromDate);
|
|
@@ -50,9 +138,11 @@ export const filterWeekdaysForDates = (weekdays, fromDate, toDate) => {
|
|
|
50
138
|
throw new Error('The from date must be before the to date.');
|
|
51
139
|
}
|
|
52
140
|
const diff = getDaysBetweenDates(sFromDate, sToDate);
|
|
141
|
+
// All selected weekdays are already included
|
|
53
142
|
if (diff >= DaysInWeek) {
|
|
54
143
|
return sWeekdaysInstance;
|
|
55
144
|
}
|
|
145
|
+
// Less than a week only include the applicable days
|
|
56
146
|
let result = getWeekdaysWithNoneIncluded();
|
|
57
147
|
const DayAfterDelta = 1;
|
|
58
148
|
for (let date = fromDate; isSameDateOrBefore(date, toDate); date = addDaysToDate(date, DayAfterDelta)) {
|
|
@@ -63,6 +153,14 @@ export const filterWeekdaysForDates = (weekdays, fromDate, toDate) => {
|
|
|
63
153
|
}
|
|
64
154
|
return result;
|
|
65
155
|
};
|
|
156
|
+
/**
|
|
157
|
+
* Returns a new SWeekdays instance with the provided weekday added to the
|
|
158
|
+
* current set of weekdays.
|
|
159
|
+
*
|
|
160
|
+
* @param weekdays The weekdays to add the weekday to. It can be an SWeekdays or
|
|
161
|
+
* a string in the SMTWTFS format.
|
|
162
|
+
* @param weekdayToAdd The weekday to add.
|
|
163
|
+
*/
|
|
66
164
|
export const addWeekdayToWeekdays = (weekdays, weekdayToAdd) => {
|
|
67
165
|
const sWeekdaysInstance = sWeekdays(weekdays);
|
|
68
166
|
const newWeekdays = [...sWeekdaysInstance.weekdays];
|
|
@@ -70,11 +168,31 @@ export const addWeekdayToWeekdays = (weekdays, weekdayToAdd) => {
|
|
|
70
168
|
newWeekdays[weekdayIndex] = getAtIndex(AllWeekdaysIncludedMask, weekdayIndex);
|
|
71
169
|
return sWeekdays(newWeekdays.join(''));
|
|
72
170
|
};
|
|
171
|
+
/**
|
|
172
|
+
* --- Comparisons ---
|
|
173
|
+
*/
|
|
174
|
+
/**
|
|
175
|
+
* Returns true if the provided weekdays include the provided weekday. Returns
|
|
176
|
+
* false otherwise.
|
|
177
|
+
*
|
|
178
|
+
* @param weekdays The weekdays to check. It can be an SWeekdays or a string in
|
|
179
|
+
* the SMTWTFS format.
|
|
180
|
+
* @param weekday The weekday to check.
|
|
181
|
+
*/
|
|
73
182
|
export const doesWeekdaysIncludeWeekday = (weekdays, weekday) => {
|
|
74
183
|
const sWeekdaysInstance = sWeekdays(weekdays);
|
|
75
184
|
const weekdayIndex = getIndexForWeekday(weekday);
|
|
76
185
|
return getAtIndex(sWeekdaysInstance.weekdays, weekdayIndex) !== NotIncludedDay;
|
|
77
186
|
};
|
|
187
|
+
/**
|
|
188
|
+
* Returns true if any of the included weekdays in weekdays1 is also included in
|
|
189
|
+
* weekdays2. Returns false otherwise.
|
|
190
|
+
*
|
|
191
|
+
* @param weekdays1 The first set of weekdays to compare. It can be an SWeekdays
|
|
192
|
+
* or a string in the SMTWTFS format.
|
|
193
|
+
* @param weekdays2 The second set of weekdays to compare. It can be an
|
|
194
|
+
* SWeekdays or a string in the SMTWTFS format.
|
|
195
|
+
*/
|
|
78
196
|
export const doesWeekdaysHaveOverlapWithWeekdays = (weekdays1, weekdays2) => {
|
|
79
197
|
const sWeekdays1 = sWeekdays(weekdays1);
|
|
80
198
|
const sWeekdays2 = sWeekdays(weekdays2);
|