-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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(web-analytics): Add timestamp utils and uuidv7 code to plugin-server #27070
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,3 +119,30 @@ export function parseDate(supposedIsoString: string): DateTime { | |
} | ||
return DateTime.fromJSDate(jsDate).toUTC() | ||
} | ||
|
||
export function toYearMonthDayInTimezone( | ||
timestamp: number, | ||
timeZone: string | ||
): { year: number; month: number; day: number } { | ||
const parts = new Intl.DateTimeFormat('en', { | ||
timeZone, | ||
year: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
}).formatToParts(new Date(timestamp)) | ||
const year = parts.find((part) => part.type === 'year')?.value | ||
const month = parts.find((part) => part.type === 'month')?.value | ||
const day = parts.find((part) => part.type === 'day')?.value | ||
if (!year || !month || !day) { | ||
throw new Error('Failed to get year, month, or day') | ||
} | ||
Comment on lines
+136
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe this can ever happen, Are you doing this for TS purposes? If so, because const [month, , day, , year] = parts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this is just to make typescript happy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it was - I looked through the docs and I do think the order is guaranteed https://tc39.es/ecma402/#sec-formatdatetimetoparts but I'm not confident enough to assume this |
||
return { year: Number(year), month: Number(month), day: Number(day) } | ||
} | ||
|
||
export function toStartOfDayInTimezone(timestamp: number, timeZone: string): Date { | ||
const { year, month, day } = toYearMonthDayInTimezone(timestamp, timeZone) | ||
return DateTime.fromObject( | ||
{ year, month, day, hour: 0, minute: 0, second: 0, millisecond: 0 }, | ||
{ zone: timeZone } | ||
).toJSDate() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JS integers are also 48 bits (out of a 64 bit number), so there will be bigger problems