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

Use UTC for HDate, fix HTime and HDateTime.toAxon #32

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion spec/core/HDateTime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,27 @@ describe('HDateTime', function (): void {
describe('#toAxon()', function (): void {
it('returns an Axon string', function (): void {
expect(HDateTime.make('2009-11-09T15:39:00Z').toAxon()).toBe(
'dateTime(2009-11-09,15:39:00)'
'dateTime(2009-11-09,15:39:00,"UTC")'
)
})

it('returns an Axon string with the timezone set to UTC', function (): void {
expect(
HDateTime.make({
_kind: Kind.DateTime,
val: '2009-11-09T15:39:00Z',
tz: 'London',
}).toAxon()
).toBe('dateTime(2009-11-09,15:39:00,"UTC")')
})

it('returns an Axon string with an offset and the timezone set to UTC', function () {
expect(
HDateTime.make(
'2021-04-14T07:42:46.275-05:00 New_York'
).toAxon()
).toBe('dateTime(2021-04-14,12:42:46.275,"UTC")')
})
}) // #toAxon()

describe('#isKind()', function (): void {
Expand Down
15 changes: 7 additions & 8 deletions src/core/HDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,14 @@ export class HDate implements HVal {
* @throws An error if the date can't be found.
*/
private static getDateFromDateObj(date: Date): string {
// Parse date string from ISO format.
const iso = date.toISOString() || ''
const res = /^([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])./.exec(iso)

if (!res || !res[1]) {
throw new Error('Invalid date')
}
return `${date.getUTCFullYear()}-${HDate.encodeDigits(
date.getUTCMonth() + 1
)}-${this.encodeDigits(date.getUTCDate())}`
}

return res[1]
private static encodeDigits(value: number): string {
const val = String(value)
return val.length === 1 ? `0${val}` : val
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/core/HDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,10 @@ export class HDateTime implements HVal {
*/
public toAxon(): string {
const date = this.date

return `dateTime(${HDate.make(date).toAxon()},${HTime.make(
date
).toAxon()})`
).toAxon()},"UTC")`
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/HTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class HTime implements HVal {
const hours = date.getUTCHours()
const minutes = date.getUTCMinutes()
const seconds = date.getUTCSeconds()
const ms = date.getMilliseconds()
const ms = date.getUTCMilliseconds()

return HTime.getTime(hours, minutes, seconds, ms)
}
Expand Down
Loading