@tim-code/my-util 0.0.17 → 0.0.19
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/package.json +1 -1
- package/src/time.js +44 -11
- package/src/time.test.js +53 -0
package/package.json
CHANGED
package/src/time.js
CHANGED
|
@@ -6,7 +6,7 @@ import { mod } from "./math.js"
|
|
|
6
6
|
* @param {boolean=} $1.floorMinute If true, floors to the nearest minute. If false, floors to the nearest second.
|
|
7
7
|
* @param {number=} $1.timestamp Unix timestamp to use instead of current time.
|
|
8
8
|
* @param {string=} $1.timezone Timezone to use instead of Eastern time.
|
|
9
|
-
* @returns {Object} { timestamp, date, time
|
|
9
|
+
* @returns {Object} { timestamp, date: YYYY-MM-DD, time: HH:mm:ss }
|
|
10
10
|
*/
|
|
11
11
|
export function getEasternTime({
|
|
12
12
|
floorMinute = false,
|
|
@@ -17,7 +17,8 @@ export function getEasternTime({
|
|
|
17
17
|
timestamp = new Date().getTime() / 1000
|
|
18
18
|
}
|
|
19
19
|
timestamp = floorMinute ? Math.floor(timestamp / 60) * 60 : Math.floor(timestamp)
|
|
20
|
-
|
|
20
|
+
// 'en-CA' (English - Canada) formats dates as YYYY-MM-DD and times in 24-hour format by default
|
|
21
|
+
const string = new Date(timestamp * 1000).toLocaleString("en-CA", {
|
|
21
22
|
...(timezone ? { timeZone: timezone } : {}),
|
|
22
23
|
hour12: false,
|
|
23
24
|
year: "numeric",
|
|
@@ -27,9 +28,7 @@ export function getEasternTime({
|
|
|
27
28
|
minute: "2-digit",
|
|
28
29
|
second: "2-digit",
|
|
29
30
|
})
|
|
30
|
-
const [
|
|
31
|
-
const [month, day, year] = americanDate.split("/")
|
|
32
|
-
const date = [year, month, day].join("-")
|
|
31
|
+
const [date, time] = string.split(", ")
|
|
33
32
|
return { timestamp, date, time }
|
|
34
33
|
}
|
|
35
34
|
|
|
@@ -38,13 +37,21 @@ export function getEasternTime({
|
|
|
38
37
|
* @param {Object} $1
|
|
39
38
|
* @param {boolean=} $1.floorMinute If true, floors to the nearest minute. If false, floors to the nearest second.
|
|
40
39
|
* @param {number=} $1.timestamp Unix timestamp to use instead of current time.
|
|
41
|
-
* @param {string=} $1.timezone Timezone to use instead of local time.
|
|
40
|
+
* @param {string=} $1.timezone Timezone to use instead of local time. undefined corresponds to "America/New_York" and "" (falsy) corresponds to local time.
|
|
42
41
|
* @returns {Object} { timestamp, date, time, minute, datetime }
|
|
43
42
|
*/
|
|
44
|
-
export function getTime({ floorMinute, timestamp, timezone =
|
|
43
|
+
export function getTime({ floorMinute, timestamp, timezone = "" } = {}) {
|
|
45
44
|
return getEasternTime({ floorMinute, timestamp, timezone })
|
|
46
45
|
}
|
|
47
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Get today's date in YYYY-MM-DD format.
|
|
49
|
+
* @returns {string}
|
|
50
|
+
*/
|
|
51
|
+
export function today() {
|
|
52
|
+
return getTime().date
|
|
53
|
+
}
|
|
54
|
+
|
|
48
55
|
/**
|
|
49
56
|
* Checks if the string represents a valid YYYY-MM-DD date.
|
|
50
57
|
* This will return false for dates like "2024-02-31".
|
|
@@ -88,7 +95,7 @@ export function isUnixTimestamp(ts, { max = 9999999999 } = {}) {
|
|
|
88
95
|
|
|
89
96
|
/**
|
|
90
97
|
* Add an amount of time to a time string.
|
|
91
|
-
* @param {string} timeString HH:mm:ss or HH:mm
|
|
98
|
+
* @param {string} timeString HH:mm:ss or HH:mm
|
|
92
99
|
* @param {Object} $1
|
|
93
100
|
* @param {number} $1.hours Hours to add to time string
|
|
94
101
|
* @param {number} $1.minutes Minutes to add to time string
|
|
@@ -112,9 +119,35 @@ export function addTime(timeString, { minutes = 0, hours = 0 } = {}) {
|
|
|
112
119
|
return newTime
|
|
113
120
|
}
|
|
114
121
|
|
|
122
|
+
const MINUTES_IN_DAY = 24 * 60
|
|
123
|
+
/**
|
|
124
|
+
* Get all minutes between two times. This does not work across day i.e 23:59:00 to 00:00:00.
|
|
125
|
+
* @param {string} start HH:mm:ss or HH:mm
|
|
126
|
+
* @param {string} end HH:mm:ss or HH:mm
|
|
127
|
+
* @returns {Array} times in HH:mm:ss
|
|
128
|
+
*/
|
|
129
|
+
export function getMinuteRange(start, end) {
|
|
130
|
+
// coerce start and end to seconds
|
|
131
|
+
start = addTime(start)
|
|
132
|
+
end = addTime(end)
|
|
133
|
+
if (!(start <= end)) {
|
|
134
|
+
throw new Error("start time is not less than end time")
|
|
135
|
+
}
|
|
136
|
+
const times = []
|
|
137
|
+
let current = start
|
|
138
|
+
while (current <= end) {
|
|
139
|
+
times.push(current)
|
|
140
|
+
current = addTime(current, { minutes: 1 })
|
|
141
|
+
if (times.length >= MINUTES_IN_DAY) {
|
|
142
|
+
break
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return times
|
|
146
|
+
}
|
|
147
|
+
|
|
115
148
|
/**
|
|
116
149
|
* Adds a number of days to a date string.
|
|
117
|
-
* @param {string} dateString
|
|
150
|
+
* @param {string} dateString YYYY-MM-DD
|
|
118
151
|
* @param {number} days
|
|
119
152
|
* @returns {string}
|
|
120
153
|
*/
|
|
@@ -127,8 +160,8 @@ export function addDays(dateString, days = 0) {
|
|
|
127
160
|
|
|
128
161
|
/**
|
|
129
162
|
* Get all dates between two dates, with limit.
|
|
130
|
-
* @param {string} start
|
|
131
|
-
* @param {string} end
|
|
163
|
+
* @param {string} start YYYY-MM-DD
|
|
164
|
+
* @param {string} end YYYY-MM-DD
|
|
132
165
|
* @returns {Array}
|
|
133
166
|
*/
|
|
134
167
|
export function getDateRange(start, end, { limit = 1000 } = {}) {
|
package/src/time.test.js
CHANGED
|
@@ -4,10 +4,12 @@ import {
|
|
|
4
4
|
addTime,
|
|
5
5
|
getDateRange,
|
|
6
6
|
getEasternTime,
|
|
7
|
+
getMinuteRange,
|
|
7
8
|
getTime,
|
|
8
9
|
isDate,
|
|
9
10
|
isTime,
|
|
10
11
|
isUnixTimestamp,
|
|
12
|
+
today,
|
|
11
13
|
} from "./time.js"
|
|
12
14
|
|
|
13
15
|
// getEasternTime changed: now accepts a "timezone" param and no longer returns "minute" or "datetime"
|
|
@@ -177,6 +179,14 @@ describe("getTime", () => {
|
|
|
177
179
|
})
|
|
178
180
|
})
|
|
179
181
|
|
|
182
|
+
describe("today", () => {
|
|
183
|
+
test("returns today's date in YYYY-MM-DD format", () => {
|
|
184
|
+
const expected = getTime().date
|
|
185
|
+
expect(today()).toBe(expected)
|
|
186
|
+
expect(/^\d{4}-\d{2}-\d{2}$/u.test(today())).toBe(true)
|
|
187
|
+
})
|
|
188
|
+
})
|
|
189
|
+
|
|
180
190
|
describe("isDate", () => {
|
|
181
191
|
test("returns true for valid YYYY-MM-DD dates", () => {
|
|
182
192
|
expect(isDate("2024-06-01")).toBe(true)
|
|
@@ -316,6 +326,49 @@ describe("addTime", () => {
|
|
|
316
326
|
})
|
|
317
327
|
})
|
|
318
328
|
|
|
329
|
+
describe("getMinuteRange", () => {
|
|
330
|
+
test("returns all minutes between start and end inclusive", () => {
|
|
331
|
+
expect(getMinuteRange("12:00:00", "12:03:00")).toEqual([
|
|
332
|
+
"12:00:00",
|
|
333
|
+
"12:01:00",
|
|
334
|
+
"12:02:00",
|
|
335
|
+
"12:03:00",
|
|
336
|
+
])
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
test("works with times that cross the hour", () => {
|
|
340
|
+
expect(getMinuteRange("12:58:00", "13:01:00")).toEqual([
|
|
341
|
+
"12:58:00",
|
|
342
|
+
"12:59:00",
|
|
343
|
+
"13:00:00",
|
|
344
|
+
"13:01:00",
|
|
345
|
+
])
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
test("works with times with nonzero seconds", () => {
|
|
349
|
+
expect(getMinuteRange("12:00:30", "12:02:30")).toEqual([
|
|
350
|
+
"12:00:30",
|
|
351
|
+
"12:01:30",
|
|
352
|
+
"12:02:30",
|
|
353
|
+
])
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
test("returns single element if start equals end", () => {
|
|
357
|
+
expect(getMinuteRange("12:34:56", "12:34:56")).toEqual(["12:34:56"])
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
test("throws if start is not less than end", () => {
|
|
361
|
+
expect(() => getMinuteRange("12:04:00", "12:03:00")).toThrow(
|
|
362
|
+
/start time is not less than end time/u
|
|
363
|
+
)
|
|
364
|
+
expect(() => getMinuteRange("12:00:00", "12:00:00")).not.toThrow()
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
test("handles input with no seconds", () => {
|
|
368
|
+
expect(getMinuteRange("12:00", "12:02")).toEqual(["12:00:00", "12:01:00", "12:02:00"])
|
|
369
|
+
})
|
|
370
|
+
})
|
|
371
|
+
|
|
319
372
|
describe("addDays", () => {
|
|
320
373
|
test("adds days within the same month", () => {
|
|
321
374
|
expect(addDays("2024-06-01", 5)).toBe("2024-06-06")
|