Skip to content

Commit

Permalink
Merge branch 'release/v0.27.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Jan 8, 2025
2 parents 6150c7b + 6e617a8 commit 80bcf2c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.27.0",
"version": "0.27.1",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down Expand Up @@ -69,9 +69,9 @@
"watch": "nr build -- --watch src"
},
"devDependencies": {
"@antfu/eslint-config": "^3.12.1",
"@antfu/eslint-config": "^3.12.2",
"@antfu/ni": "^0.23.2",
"@types/node": "^22.10.2",
"@types/node": "^22.10.5",
"@vitejs/plugin-vue": "^5.2.1",
"@vitest/browser": "^2.1.8",
"@vitest/coverage-v8": "^2.1.8",
Expand All @@ -80,7 +80,7 @@
"playwright": "^1.49.1",
"tsup": "^8.3.5",
"typescript": "^5.7.2",
"vite": "^6.0.6",
"vite": "^6.0.7",
"vitest": "^2.1.8"
},
"pnpm": {
Expand Down
51 changes: 50 additions & 1 deletion src/common/data/day.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dayDiff, dayFromDate, dayFromString, dayIterator, dayMonthStart, dayOffset, dayRange, dayToParts, dayToString, dayYearStart } from './day'
import { dayDiff, dayFromAny, dayFromDate, dayFromString, dayFromTimestamp, dayFromTimestampSeconds, dayFromToday, dayIterator, dayMonthStart, dayOffset, dayRange, dayToDate, dayToParts, dayToString, dayToTimestamp, dayToTimestampSeconds, dayYearStart } from './day'
import { Day, dateStringToDays, forEachDay } from './day-legacy'

describe('days', () => {
Expand Down Expand Up @@ -201,4 +201,53 @@ describe('days', () => {

expect(dayIterator(-6, 20130104).next().value).toEqual(20121230)
})

it('dayFromAny with string input', () => {
const _res = dayFromAny('2022-10-05')
expect(_res).toBe(20221005)
})

it('dayFromAny with array input', () => {
const _res = dayFromAny([2023])
expect(_res).toBe(20230101)
})

it('dayFromAny with date input', () => {
const date = new Date('1995-12-17T03:24:00')
const _res = dayFromAny(date)
expect(_res).toBe(19951217)
})

it('dayFromAny ignores short numbers', () => {
const _res = dayFromAny(42)
expect(_res).toBeUndefined()
})

it('dayFromTimestamp', () => {
const _day = 20220815
const _stamp = dayToTimestamp(_day)
const _res = dayFromTimestamp(_stamp)
expect(_res).toBe(_day)
})

it('dayToTimestampSeconds', () => {
const _day = 20220815
const _secs = dayToTimestampSeconds(_day)
expect(_secs).toBeCloseTo(Math.floor(dayToDate(_day, true).getTime() / 1000))
})

it('dayFromTimestampSeconds', () => {
const _day = 20220815
const _secs = dayToTimestampSeconds(_day)
expect(_secs).toBeCloseTo(Math.floor(dayToDate(_day, true).getTime() / 1000))
const _res = dayFromTimestampSeconds(_secs)
// Multiplying by 1000 to match the ms input for dayFromTimestampSeconds
expect(_res).toBe(_day)
})

it('dayFromToday', () => {
const _today = dayFromToday()
// Should be current day in YYYYMMDD format
expect(typeof _today).toBe('number')
})
})
18 changes: 15 additions & 3 deletions src/common/data/day.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Functional Variant

import { timestampMillisecondsToSeconds, timestampSecondsToMilliseconds } from "../time"

export const DAY_MS = 86400000 // 1000 * 60 * 60 * 24

export type DayValue = number
Expand Down Expand Up @@ -53,9 +55,8 @@ export function dayFromAny(
else if (typeof value === 'string') {
return dayFromString(value)
}
else if (Array.isArray(value) && value.length === 3) {
const [year, month, day] = value
return dayFromParts(year, month, day)
else if (Array.isArray(value) && value.length >= 1) {
return dayFromParts(...value)
}
else if (value instanceof Date) {
return dayFromDate(value, utc)
Expand Down Expand Up @@ -92,14 +93,25 @@ export function dayFromDateGMT(date: Date): DayValue {
return dayFromDate(date, true)
}


export function dayToTimestampSeconds(day: DayValue, utc = true): number {
return Math.floor(dayToDate(day, utc).getTime() / 1000)
}

/// Timestamp in miliseconds
export function dayToTimestamp(day: DayValue, utc = true): number {
return dayToDate(day, utc).getTime()
}

/// Timestamp in miliseconds
export function dayFromTimestamp(ms: number, utc = true): DayValue {
return dayFromDate(new Date(ms), utc)
}

export function dayFromTimestampSeconds(ms: number, utc = true): DayValue {
return dayFromDate(new Date(Math.floor(ms * 1000)), utc)
}

export function dayToString(day: DayValue, sep = '-') {
const baseString = String(day)
return (
Expand Down
16 changes: 10 additions & 6 deletions src/common/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,26 @@ export function datetimeToUTC(fromDate: Date): Date {
))
}

/** ms -> s with simple check */
export function timestampMillisecondsToSeconds(ts: number) {
// const tsMs2000 = 946684800000 // same as Jan 11 1970 in ms
// const tsMs2500 = 16725225600000 // same as Jul 13 1970 in ms
// 1000000000000

/** ms -> s with simple check. Assume low values are already ms */
export function timestampMillisecondsToSeconds(ts: number, smart = true) {
if (ts <= 0)
return 0
if (ts < 1000000000000) {
if (smart && ts < 1000000000000) { // TODO find a better threshold and add tests
return ts
// log.warn('Timestamp might already be in seconds?', ts)
}
return Math.floor(ts / 1000)
}

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

0 comments on commit 80bcf2c

Please sign in to comment.