Skip to content

Commit

Permalink
fix: fix timezone error
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-void0 committed Jan 17, 2024
1 parent e4319f9 commit 9d82b1b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
14 changes: 11 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,21 @@ const instance = new FakeFingerPrint({
// width: 9999,
// height: 88,
// },
canvas: {
fillText: 'gaga梦',
// canvas: {
// fillText: 'gaga梦',
// },
// audio: {
// strength: 100,
// },
timezone: {
zone: 'America/NewYork',
locale: 'en-US',
offset: -5,
},
},
report: (arg) => {
console.log('key:', arg)
},
})

instance.open(['canvas'])
instance.open(['timezone'])
31 changes: 16 additions & 15 deletions src/lib/timezone.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { type AbstractBaseFunc, Base } from './base'

export interface TimezoneOpts {
text: string
zone: string
locale: string
/* eg: 'Asia/Shanghai' | 'America/New_York' | 'Europe/London' */
zone: Intl.DateTimeFormatOptions['timeZone']
/* eg: 'zh-CN' 'en-US' 'en-GB'...*/
locale: Intl.LocalesArgument
/* offest of time. unit is hour */
offset: number
}

Expand All @@ -24,18 +26,17 @@ export class TimezoneHandle extends Base<TimezoneOpts, TimezoneReport> implement

proxy() {
const self = this
Reflect.defineProperty(Intl, 'DateTimeFormat', {
value: new Proxy(Intl.DateTimeFormat, {
get: () => {
self.report({ type: 'timezone', key: 'dateTimeFormat' })
return function (this: any, ...args: Parameters<typeof Intl.DateTimeFormat>) {
args[0] = self.config?.locale ?? args[0]
args[1] = { timeZone: self.config?.zone, ...args[1] }
return self.oriDateTimeFormat.apply(this, args)
}
},
}),
})

// @ts-expect-error
Intl.DateTimeFormat = function (
this: Intl.DateTimeFormat,
...args: ConstructorParameters<typeof Intl.DateTimeFormat>
) {
self.report({ type: 'timezone', key: 'dateTimeFormat' })
args[0] = (self.config?.locale as any) ?? args[0]
args[1] = { timeZone: self.config?.zone, ...args[1] }
return self.oriDateTimeFormat.apply(this, args)
}

Date.prototype.getTimezoneOffset = function (this: Date) {
self.report({ type: 'timezone', key: 'getTimezoneOffset' })
Expand Down

0 comments on commit 9d82b1b

Please sign in to comment.