diff --git a/package.json b/package.json index 368efd6..200f893 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.24.21", + "version": "0.24.22", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick", @@ -69,18 +69,18 @@ "watch": "nr build -- --watch src" }, "devDependencies": { - "@antfu/eslint-config": "^3.6", + "@antfu/eslint-config": "^3.7", "@antfu/ni": "^0.23.0", - "@types/node": "^22.5.5", - "@vitejs/plugin-vue": "^5.1.3", + "@types/node": "^22.7.2", + "@vitejs/plugin-vue": "^5.1.4", "@vitest/browser": "^2.1.1", "@vitest/coverage-v8": "^2.1.1", - "esbuild": "^0.23.1", + "esbuild": "^0.24.0", "eslint": "^9", - "playwright": "^1.47.1", - "tsup": "^8.2.4", + "playwright": "^1.47.2", + "tsup": "^8.3.0", "typescript": "^5.6.2", - "vite": "^5.4.5", + "vite": "^5.4.8", "vitest": "^2.1.1" }, "pnpm": { diff --git a/src/common/time.spec.ts b/src/common/time.spec.ts index 3a00aae..e6ceb1d 100644 --- a/src/common/time.spec.ts +++ b/src/common/time.spec.ts @@ -1,5 +1,5 @@ import { sleep } from './exec' -import { dateFromSeconds, datetimeToLocal, datetimeToUTC, duration, formatMilliseconds, getPerformanceTimestamp, getTimestamp, getTimestampInSeconds, parseDate } from './time' +import { dateFromSeconds, datetimeToLocal, datetimeToUTC, duration, formatMilliseconds, getPerformanceTimestamp, getTimestamp, getTimestampInSeconds, parseDate, timestampMillisecondsToSeconds, timestampSecondsToMilliseconds } from './time' describe('time.spec', () => { it('should measure', async () => { @@ -85,4 +85,28 @@ describe('time.spec', () => { const utcDate = datetimeToUTC(date) expect(utcDate).toEqual(new Date('2022-01-01T12:00:00Z')) }) + + it('should convert milliseconds to seconds', () => { + const ts = 1609459200000 // 2021-01-01T00:00:00.000Z + const result = timestampMillisecondsToSeconds(ts) + expect(result).toBe(1609459200) + }) + + it('should log a warning if the timestamp is already in seconds', () => { + const ts = 1609459200 + const result = timestampMillisecondsToSeconds(ts) + expect(result).toBe(1609459200) + }) + + it('should convert seconds to milliseconds', () => { + const ts = 1609459200 // 2021-01-01T00:00:00Z + const result = timestampSecondsToMilliseconds(ts) + expect(result).toBe(1609459200000) + }) + + it('should log a warning if the timestamp is already in milliseconds', () => { + const ts = 1609459200000 + const result = timestampMillisecondsToSeconds(ts) + expect(result).toBe(1609459200) + }) }) diff --git a/src/common/time.ts b/src/common/time.ts index 8aa40f4..cbcdc0f 100644 --- a/src/common/time.ts +++ b/src/common/time.ts @@ -110,3 +110,38 @@ export function datetimeToUTC(fromDate: Date): Date { fromDate.getMilliseconds(), )) } + +/** ms -> s with simple check */ +export function timestampMillisecondsToSeconds(ts: number) { + if (ts <= 0) + return 0 + if (ts < 1000000000000) { + 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) { + if (ts <= 0) + return 0 + if (ts > 1000000000000) { + return ts + // log.warn('Timestamp might already be in milliseconds?', ts) + } + return Math.floor(ts * 1000) +} + +export const TIME_YEAR_MS = 31536000000 // 365 * 24 * 60 * 60 * 1000 +export const TIME_YEAR_S = 31536000 // 365 * 24 * 60 * 60 +export const TIME_MONTH_MS = 2592000000 // 30 * 24 * 60 * 60 * 1000 +export const TIME_MONTH_S = 2592000 // 30 * 24 * 60 * 60 +export const TIME_WEEK_MS = 604800000 // 7 * 24 * 60 * 60 * 1000 +export const TIME_WEEK_S = 604800 // 7 * 24 * 60 * 60 +export const TIME_DAY_MS = 86400000 // 24 * 60 * 60 * 1000 +export const TIME_DAY_S = 86400 // 24 * 60 * 60 +export const TIME_HOUR_MS = 3600000 // 60 * 60 * 1000 +export const TIME_HOUR_S = 3600 // 60 * 60 +export const TIME_MINUTE_MS = 60000 // 60 * 1000 +export const TIME_MINUTE_S = 60 // 60 diff --git a/vitest.config.ts b/vitest.config.ts index a4c1066..fadc9ea 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,6 +1,6 @@ -import type { UserConfig } from 'vitest' /// -/* eslint-disable no-console */ + +import type { UserConfig } from 'vitest' import { resolve } from 'node:path' import process from 'node:process' import { defineConfig } from 'vite'