Skip to content

Commit 41d71cb

Browse files
committed
feat: add some timestamp seconds logic
1 parent f75eb9c commit 41d71cb

File tree

4 files changed

+78
-13
lines changed

4 files changed

+78
-13
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@
6969
"watch": "nr build -- --watch src"
7070
},
7171
"devDependencies": {
72-
"@antfu/eslint-config": "^3.12.1",
72+
"@antfu/eslint-config": "^3.12.2",
7373
"@antfu/ni": "^0.23.2",
74-
"@types/node": "^22.10.2",
74+
"@types/node": "^22.10.5",
7575
"@vitejs/plugin-vue": "^5.2.1",
7676
"@vitest/browser": "^2.1.8",
7777
"@vitest/coverage-v8": "^2.1.8",
@@ -80,7 +80,7 @@
8080
"playwright": "^1.49.1",
8181
"tsup": "^8.3.5",
8282
"typescript": "^5.7.2",
83-
"vite": "^6.0.6",
83+
"vite": "^6.0.7",
8484
"vitest": "^2.1.8"
8585
},
8686
"pnpm": {

src/common/data/day.spec.ts

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dayDiff, dayFromDate, dayFromString, dayIterator, dayMonthStart, dayOffset, dayRange, dayToParts, dayToString, dayYearStart } from './day'
1+
import { dayDiff, dayFromAny, dayFromDate, dayFromString, dayFromTimestamp, dayFromTimestampSeconds, dayFromToday, dayIterator, dayMonthStart, dayOffset, dayRange, dayToDate, dayToParts, dayToString, dayToTimestamp, dayToTimestampSeconds, dayYearStart } from './day'
22
import { Day, dateStringToDays, forEachDay } from './day-legacy'
33

44
describe('days', () => {
@@ -201,4 +201,53 @@ describe('days', () => {
201201

202202
expect(dayIterator(-6, 20130104).next().value).toEqual(20121230)
203203
})
204+
205+
it('dayFromAny with string input', () => {
206+
const _res = dayFromAny('2022-10-05')
207+
expect(_res).toBe(20221005)
208+
})
209+
210+
it('dayFromAny with array input', () => {
211+
const _res = dayFromAny([2023])
212+
expect(_res).toBe(20230101)
213+
})
214+
215+
it('dayFromAny with date input', () => {
216+
const date = new Date('1995-12-17T03:24:00')
217+
const _res = dayFromAny(date)
218+
expect(_res).toBe(19951217)
219+
})
220+
221+
it('dayFromAny ignores short numbers', () => {
222+
const _res = dayFromAny(42)
223+
expect(_res).toBeUndefined()
224+
})
225+
226+
it('dayFromTimestamp', () => {
227+
const _day = 20220815
228+
const _stamp = dayToTimestamp(_day)
229+
const _res = dayFromTimestamp(_stamp)
230+
expect(_res).toBe(_day)
231+
})
232+
233+
it('dayToTimestampSeconds', () => {
234+
const _day = 20220815
235+
const _secs = dayToTimestampSeconds(_day)
236+
expect(_secs).toBeCloseTo(Math.floor(dayToDate(_day, true).getTime() / 1000))
237+
})
238+
239+
it('dayFromTimestampSeconds', () => {
240+
const _day = 20220815
241+
const _secs = dayToTimestampSeconds(_day)
242+
expect(_secs).toBeCloseTo(Math.floor(dayToDate(_day, true).getTime() / 1000))
243+
const _res = dayFromTimestampSeconds(_secs)
244+
// Multiplying by 1000 to match the ms input for dayFromTimestampSeconds
245+
expect(_res).toBe(_day)
246+
})
247+
248+
it('dayFromToday', () => {
249+
const _today = dayFromToday()
250+
// Should be current day in YYYYMMDD format
251+
expect(typeof _today).toBe('number')
252+
})
204253
})

src/common/data/day.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Functional Variant
22

3+
import { timestampMillisecondsToSeconds, timestampSecondsToMilliseconds } from "../time"
4+
35
export const DAY_MS = 86400000 // 1000 * 60 * 60 * 24
46

57
export type DayValue = number
@@ -53,9 +55,8 @@ export function dayFromAny(
5355
else if (typeof value === 'string') {
5456
return dayFromString(value)
5557
}
56-
else if (Array.isArray(value) && value.length === 3) {
57-
const [year, month, day] = value
58-
return dayFromParts(year, month, day)
58+
else if (Array.isArray(value) && value.length >= 1) {
59+
return dayFromParts(...value)
5960
}
6061
else if (value instanceof Date) {
6162
return dayFromDate(value, utc)
@@ -92,14 +93,25 @@ export function dayFromDateGMT(date: Date): DayValue {
9293
return dayFromDate(date, true)
9394
}
9495

96+
97+
export function dayToTimestampSeconds(day: DayValue, utc = true): number {
98+
return Math.floor(dayToDate(day, utc).getTime() / 1000)
99+
}
100+
101+
/// Timestamp in miliseconds
95102
export function dayToTimestamp(day: DayValue, utc = true): number {
96103
return dayToDate(day, utc).getTime()
97104
}
98105

106+
/// Timestamp in miliseconds
99107
export function dayFromTimestamp(ms: number, utc = true): DayValue {
100108
return dayFromDate(new Date(ms), utc)
101109
}
102110

111+
export function dayFromTimestampSeconds(ms: number, utc = true): DayValue {
112+
return dayFromDate(new Date(Math.floor(ms * 1000)), utc)
113+
}
114+
103115
export function dayToString(day: DayValue, sep = '-') {
104116
const baseString = String(day)
105117
return (

src/common/time.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,26 @@ export function datetimeToUTC(fromDate: Date): Date {
111111
))
112112
}
113113

114-
/** ms -> s with simple check */
115-
export function timestampMillisecondsToSeconds(ts: number) {
114+
// const tsMs2000 = 946684800000 // same as Jan 11 1970 in ms
115+
// const tsMs2500 = 16725225600000 // same as Jul 13 1970 in ms
116+
// 1000000000000
117+
118+
/** ms -> s with simple check. Assume low values are already ms */
119+
export function timestampMillisecondsToSeconds(ts: number, smart = true) {
116120
if (ts <= 0)
117121
return 0
118-
if (ts < 1000000000000) {
122+
if (smart && ts < 1000000000000) { // TODO find a better threshold and add tests
119123
return ts
120124
// log.warn('Timestamp might already be in seconds?', ts)
121125
}
122126
return Math.floor(ts / 1000)
123127
}
124128

125-
/** s -> ms with simple check */
126-
export function timestampSecondsToMilliseconds(ts: number) {
129+
/** s -> ms with simple check. Assume high values are already ms */
130+
export function timestampSecondsToMilliseconds(ts: number, smart = true) {
127131
if (ts <= 0)
128132
return 0
129-
if (ts > 1000000000000) {
133+
if (smart && ts > 1000000000000) { // TODO find a better threshold and add tests
130134
return ts
131135
// log.warn('Timestamp might already be in milliseconds?', ts)
132136
}

0 commit comments

Comments
 (0)