Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: time core #12

Open
wants to merge 7 commits into
base: v0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/time/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@
"files": [
"dist",
"src"
]
],
"dependencies": {
"@js-temporal/polyfill": "^0.4.4",
"@tanstack/store": "^0.4.1"
},
"devDependencies": {
"csstype": "^3.1.3"
}
}
60 changes: 60 additions & 0 deletions packages/time/src/core/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Temporal } from '@js-temporal/polyfill'
import { Store } from '@tanstack/store'
import { getDefaultTimeZone } from '../utils/dateDefaults'

export interface TimeCoreOptions {
/**
* The time zone to use for the current time.
* @default Intl.DateTimeFormat().resolvedOptions().timeZone
*/
timeZone?: Temporal.TimeZoneLike
}

interface TimeState {
/**
* The current time.
* @default Temporal.Now.zonedDateTimeISO()
* @readonly
* @type Temporal.ZonedDateTime
*/
currentTime: Temporal.ZonedDateTime
}

export abstract class TimeCore {
protected store: Store<TimeState>
protected interval: NodeJS.Timeout | null = null
protected timeZone: Temporal.TimeZoneLike

constructor(options: TimeCoreOptions = {}) {
const defaultTimeZone = getDefaultTimeZone()
this.timeZone = options.timeZone || defaultTimeZone
this.store = new Store<TimeState>({
currentTime: Temporal.Now.zonedDateTimeISO(this.timeZone),
})
this.updateCurrentTime()
}

protected updateCurrentTime() {
this.store.setState((prev) => ({
...prev,
currentTime: Temporal.Now.zonedDateTimeISO(this.timeZone),
}))
}

startUpdatingTime(intervalMs: number = 1000) {
if (!this.interval) {
this.interval = setInterval(() => this.updateCurrentTime(), intervalMs)
}
}

stopUpdatingTime() {
if (this.interval) {
clearInterval(this.interval)
this.interval = null
}
}

getCurrentTime(): Temporal.ZonedDateTime {
return this.store.state.currentTime
}
}
4 changes: 2 additions & 2 deletions packages/time/src/tests/isValidDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {isValidDate} from '../utils/isValidDate';
describe('isValidDate', () => {
test('should return true for a valid date', () => {
expect(isValidDate(new Date())).toBe(true);
});
})

test('should return false for an invalid date', () => {
expect(isValidDate(new Date("invalid"))).toBe(false);
Expand All @@ -13,4 +13,4 @@ describe('isValidDate', () => {
test("should return false for null", () => {
expect(isValidDate(null)).toBe(false);
});
});
});
64 changes: 64 additions & 0 deletions packages/time/src/tests/time.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Temporal } from '@js-temporal/polyfill'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { TimeCore } from '../core/time'


export class TestTimeCore extends TimeCore {
getCurrentTime(): Temporal.ZonedDateTime {
return super.getCurrentTime()
}

startUpdatingTime(intervalMs: number = 1000) {
super.startUpdatingTime(intervalMs)
}

stopUpdatingTime() {
super.stopUpdatingTime()
}
}

describe('TimeCore', () => {
beforeEach(() => {
vi.useFakeTimers()
const mockNow = Temporal.PlainDateTime.from({ year: 2024, month: 1, day: 1, hour: 0, minute: 0, second: 0 })
vi.setSystemTime(mockNow.toZonedDateTime('UTC').epochMilliseconds)
})

afterEach(() => {
vi.useRealTimers()
})

test('should initialize with the current time in the default time zone', () => {
const timeCore = new TestTimeCore()
const currentTime = Temporal.Now.zonedDateTimeISO()
expect(timeCore.getCurrentTime().toString()).toBe(currentTime.toString())
})

test('should initialize with the current time in the specified time zone', () => {
const timeZone = 'America/New_York'
const timeCore = new TestTimeCore({ timeZone })
const currentTime = Temporal.Now.zonedDateTimeISO(timeZone)
expect(timeCore.getCurrentTime().toString()).toBe(currentTime.toString())
})

test('should start updating the current time', () => {
const timeCore = new TestTimeCore()
timeCore.startUpdatingTime()
vi.advanceTimersByTime(1000)
const currentTime = Temporal.Now.zonedDateTimeISO()
expect(timeCore.getCurrentTime().epochMilliseconds).toBe(currentTime.epochMilliseconds)
})

test('should stop updating the current time', () => {
const timeCore = new TestTimeCore()
timeCore.startUpdatingTime()

vi.advanceTimersByTime(1000)
timeCore.stopUpdatingTime()
const stoppedTime = timeCore.getCurrentTime()

vi.advanceTimersByTime(1000)
const timeAfterStop = timeCore.getCurrentTime()
expect(timeAfterStop.epochMilliseconds).toBe(stoppedTime.epochMilliseconds)
})
})
78 changes: 51 additions & 27 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.