Skip to content

Commit

Permalink
Merge branch 'release/v0.24.22'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Sep 26, 2024
2 parents 0022602 + 1226db6 commit b9d8485
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.24.21",
"version": "0.24.22",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down Expand Up @@ -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": {
Expand Down
26 changes: 25 additions & 1 deletion src/common/time.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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)
})
})
35 changes: 35 additions & 0 deletions src/common/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UserConfig } from 'vitest'
/// <reference types="vitest" />
/* eslint-disable no-console */

import type { UserConfig } from 'vitest'
import { resolve } from 'node:path'
import process from 'node:process'
import { defineConfig } from 'vite'
Expand Down

0 comments on commit b9d8485

Please sign in to comment.