@tim-code/my-util 0.0.8 → 0.0.10
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 +21 -6
- package/src/time.test.js +92 -5
package/package.json
CHANGED
package/src/time.js
CHANGED
|
@@ -5,15 +5,20 @@ import { mod } from "./math.js"
|
|
|
5
5
|
* @param {Object} $1
|
|
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, datetime }
|
|
9
10
|
*/
|
|
10
|
-
export function getEasternTime({
|
|
11
|
+
export function getEasternTime({
|
|
12
|
+
floorMinute = false,
|
|
13
|
+
timestamp = undefined,
|
|
14
|
+
timezone = "America/New_York",
|
|
15
|
+
} = {}) {
|
|
11
16
|
if (!timestamp) {
|
|
12
17
|
timestamp = new Date().getTime() / 1000
|
|
13
18
|
}
|
|
14
19
|
timestamp = floorMinute ? Math.floor(timestamp / 60) * 60 : Math.floor(timestamp)
|
|
15
20
|
const string = new Date(timestamp * 1000).toLocaleString("en-US", {
|
|
16
|
-
timeZone:
|
|
21
|
+
...(timezone ? { timeZone: timezone } : {}),
|
|
17
22
|
hour12: false,
|
|
18
23
|
year: "numeric",
|
|
19
24
|
month: "2-digit",
|
|
@@ -25,9 +30,19 @@ export function getEasternTime({ floorMinute = false, timestamp = undefined } =
|
|
|
25
30
|
const [americanDate, time] = string.split(", ")
|
|
26
31
|
const [month, day, year] = americanDate.split("/")
|
|
27
32
|
const date = [year, month, day].join("-")
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
return { timestamp, date, time }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Gets various ways of representing the current time in local timezone. Floors to nearest second by default.
|
|
38
|
+
* @param {Object} $1
|
|
39
|
+
* @param {boolean=} $1.floorMinute If true, floors to the nearest minute. If false, floors to the nearest second.
|
|
40
|
+
* @param {number=} $1.timestamp Unix timestamp to use instead of current time.
|
|
41
|
+
* @param {string=} $1.timezone Timezone to use instead of local time.
|
|
42
|
+
* @returns {Object} { timestamp, date, time, minute, datetime }
|
|
43
|
+
*/
|
|
44
|
+
export function getTime({ floorMinute, timestamp, timezone = false } = {}) {
|
|
45
|
+
return getEasternTime({ floorMinute, timestamp, timezone })
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
/**
|
package/src/time.test.js
CHANGED
|
@@ -4,20 +4,22 @@ import {
|
|
|
4
4
|
addTime,
|
|
5
5
|
getDateRange,
|
|
6
6
|
getEasternTime,
|
|
7
|
+
getTime,
|
|
7
8
|
isDate,
|
|
8
9
|
isTime,
|
|
9
10
|
isUnixTimestamp,
|
|
10
11
|
} from "./time.js"
|
|
11
12
|
|
|
12
|
-
// getEasternTime changed:
|
|
13
|
+
// getEasternTime changed: now accepts a "timezone" param and no longer returns "minute" or "datetime"
|
|
13
14
|
describe("getEasternTime", () => {
|
|
14
15
|
test("returns correct structure and types", () => {
|
|
15
16
|
const result = getEasternTime()
|
|
16
17
|
expect(typeof result.timestamp).toBe("number")
|
|
17
18
|
expect(typeof result.date).toBe("string")
|
|
18
19
|
expect(typeof result.time).toBe("string")
|
|
19
|
-
|
|
20
|
-
expect(
|
|
20
|
+
// minute and datetime are no longer returned
|
|
21
|
+
expect(result).not.toHaveProperty("minute")
|
|
22
|
+
expect(result).not.toHaveProperty("datetime")
|
|
21
23
|
})
|
|
22
24
|
|
|
23
25
|
test("floors to minute if floorMinute is true", () => {
|
|
@@ -46,7 +48,46 @@ describe("getEasternTime", () => {
|
|
|
46
48
|
expect(def.date).toEqual(explicit.date)
|
|
47
49
|
expect(def.time).toEqual(explicit.time)
|
|
48
50
|
expect(def.timestamp).toEqual(explicit.timestamp)
|
|
49
|
-
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
// New: test timezone override
|
|
54
|
+
test("respects timezone parameter", () => {
|
|
55
|
+
// 2024-06-01T12:34:56Z (UTC)
|
|
56
|
+
const ts = 1717245296
|
|
57
|
+
// New York (EDT, UTC-4)
|
|
58
|
+
const eastern = getEasternTime({ timestamp: ts, timezone: "America/New_York" })
|
|
59
|
+
// Los Angeles (PDT, UTC-7)
|
|
60
|
+
const pacific = getEasternTime({ timestamp: ts, timezone: "America/Los_Angeles" })
|
|
61
|
+
// UTC
|
|
62
|
+
const utc = getEasternTime({ timestamp: ts, timezone: "UTC" })
|
|
63
|
+
expect(eastern.time).not.toBe(pacific.time)
|
|
64
|
+
expect(eastern.time).not.toBe(utc.time)
|
|
65
|
+
expect(pacific.time).not.toBe(utc.time)
|
|
66
|
+
// Should match expected hour offset
|
|
67
|
+
expect(eastern.time.startsWith("08:34")).toBe(true) // EDT
|
|
68
|
+
expect(pacific.time.startsWith("05:34")).toBe(true) // PDT
|
|
69
|
+
expect(utc.time.startsWith("12:34")).toBe(true) // UTC
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test("returns local time if timezone is empty string", () => {
|
|
73
|
+
// If timezone is falsy (""), should use local time zone
|
|
74
|
+
// We'll compare to Date.toLocaleString with no timeZone option
|
|
75
|
+
const ts = 1717245296
|
|
76
|
+
const local = getEasternTime({ timestamp: ts, timezone: "" })
|
|
77
|
+
const expected = new Date(ts * 1000).toLocaleString("en-US", {
|
|
78
|
+
hour12: false,
|
|
79
|
+
year: "numeric",
|
|
80
|
+
month: "2-digit",
|
|
81
|
+
day: "2-digit",
|
|
82
|
+
hour: "2-digit",
|
|
83
|
+
minute: "2-digit",
|
|
84
|
+
second: "2-digit",
|
|
85
|
+
})
|
|
86
|
+
const [, time] = expected.split(", ")
|
|
87
|
+
const [month, day, year] = expected.split(", ")[0].split("/")
|
|
88
|
+
const date = [year, month, day].join("-")
|
|
89
|
+
expect(local.date).toBe(date)
|
|
90
|
+
expect(local.time).toBe(time)
|
|
50
91
|
})
|
|
51
92
|
|
|
52
93
|
// DST boundary tests
|
|
@@ -89,6 +130,53 @@ describe("getEasternTime", () => {
|
|
|
89
130
|
})
|
|
90
131
|
})
|
|
91
132
|
|
|
133
|
+
// New tests for getTime (newly exported function)
|
|
134
|
+
describe("getTime", () => {
|
|
135
|
+
test("returns same structure as getEasternTime", () => {
|
|
136
|
+
const result = getTime({})
|
|
137
|
+
expect(typeof result.timestamp).toBe("number")
|
|
138
|
+
expect(typeof result.date).toBe("string")
|
|
139
|
+
expect(typeof result.time).toBe("string")
|
|
140
|
+
expect(result).not.toHaveProperty("minute")
|
|
141
|
+
expect(result).not.toHaveProperty("datetime")
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
test("defaults to local time if timezone is not provided", () => {
|
|
145
|
+
// getTime({}) should use timezone = false, which disables the timeZone option and uses local
|
|
146
|
+
const ts = 1717245296
|
|
147
|
+
const local = getTime({ timestamp: ts })
|
|
148
|
+
const expected = new Date(ts * 1000).toLocaleString("en-US", {
|
|
149
|
+
hour12: false,
|
|
150
|
+
year: "numeric",
|
|
151
|
+
month: "2-digit",
|
|
152
|
+
day: "2-digit",
|
|
153
|
+
hour: "2-digit",
|
|
154
|
+
minute: "2-digit",
|
|
155
|
+
second: "2-digit",
|
|
156
|
+
})
|
|
157
|
+
const [, time] = expected.split(", ")
|
|
158
|
+
const [month, day, year] = expected.split(", ")[0].split("/")
|
|
159
|
+
const date = [year, month, day].join("-")
|
|
160
|
+
expect(local.date).toBe(date)
|
|
161
|
+
expect(local.time).toBe(time)
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test("passes timezone through to getEasternTime", () => {
|
|
165
|
+
// Should match getEasternTime with same timezone
|
|
166
|
+
const ts = 1717245296
|
|
167
|
+
const pacific = getTime({ timestamp: ts, timezone: "America/Los_Angeles" })
|
|
168
|
+
const ref = getEasternTime({ timestamp: ts, timezone: "America/Los_Angeles" })
|
|
169
|
+
expect(pacific).toEqual(ref)
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
test("floors to minute if floorMinute is true", () => {
|
|
173
|
+
const ts = 1717245296
|
|
174
|
+
const floored = getTime({ timestamp: ts, floorMinute: true })
|
|
175
|
+
expect(floored.timestamp % 60).toBe(0)
|
|
176
|
+
expect(floored.time.endsWith(":00")).toBe(true)
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
|
|
92
180
|
describe("isDate", () => {
|
|
93
181
|
test("returns true for valid YYYY-MM-DD dates", () => {
|
|
94
182
|
expect(isDate("2024-06-01")).toBe(true)
|
|
@@ -279,7 +367,6 @@ describe("addDays", () => {
|
|
|
279
367
|
})
|
|
280
368
|
})
|
|
281
369
|
|
|
282
|
-
// New tests for getDateRange (newly exported function)
|
|
283
370
|
describe("getDateRange", () => {
|
|
284
371
|
test("returns all dates between start and end inclusive", () => {
|
|
285
372
|
expect(getDateRange("2024-06-01", "2024-06-03")).toEqual([
|