Skip to content

Commit

Permalink
Test internalized server date time parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Etheryte committed May 29, 2024
1 parent 749af37 commit fba322b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
4 changes: 1 addition & 3 deletions web/html/src/manager/recurring/recurring-actions-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ class RecurringActionsDetails extends React.Component<RecurringActionsDetailsPro

// Creation time is always retrieved in server's timezone
// Parse the date and show it in user's timezone
const createdAt = localizedMoment(
Date.parse(`${details.createdAt} ${localizedMoment.serverTimeZoneAbbr}`)
).toUserDateTimeString();
const createdAt = localizedMoment.fromServerDateTimeString(details.createdAt).toUserDateTimeString();

return (
<BootstrapPanel title={t("Schedule Details")}>
Expand Down
23 changes: 19 additions & 4 deletions web/html/src/utils/datetime/localizedMoment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ declare global {
}
}

if (process.env.NODE_ENV !== "production") {
(window as any).localizedMoment = localizedMomentConstructor;
}
// This is for debugging purposes only, if you need to use this module, import it properly
(window as any).localizedMoment = localizedMomentConstructor;

function validateOrGuessTimeZone(input: string | undefined, errorLabel: "Server" | "User") {
try {
Expand Down Expand Up @@ -139,6 +138,13 @@ declare module "moment" {
const userTimeZone: string;
/** Abbreviated user time zone string, e.g. `"JST"` */
const userTimeZoneAbbr: string;

/**
* Parse a date time string provided in the server time zone, e.g. `localizedMoment.fromServerString("May 29, 2024, 11:26:06 AM")`.
*
* @deprecated This function exists for backwards compatibility, for new backend code use ISO 8601 and call `localizedMoment(input)` directly.
*/
const fromServerDateTimeString: (input: string) => moment.Moment;
}

moment.fn.toServerString = function (this: moment.Moment): string {
Expand Down Expand Up @@ -197,6 +203,13 @@ Object.defineProperties(moment, {
return (userTimeZoneAbbr ??= moment().tz(config.userTimeZone).format("z"));
},
},
fromServerDateTimeString: {
get() {
// TODO: Is this format string correct? Maybe there's a library constant we can use here?
return (input: string): moment.Moment =>
moment.tz(input, "MMM DD, YYYY, HH:mm:ss A", true, config.serverTimeZone);
},
},
});

const parseTimeString = function (input: string): { hours: number; minutes: number } | null {
Expand All @@ -218,7 +231,9 @@ function localizedMomentConstructor(input?: moment.MomentInput) {
: moment(input, true).utc().tz("UTC");

if (!utcMoment.isValid()) {
throw new RangeError("Invalid localized moment on input " + JSON.stringify(input));
throw new RangeError(
`Invalid input for localized moment, got input with type "${typeof input}" and value ${input}`
);
}

return utcMoment;
Expand Down

0 comments on commit fba322b

Please sign in to comment.