Skip to content

Commit

Permalink
Fix timezone issues with strftime (#5762)
Browse files Browse the repository at this point in the history
* Fix timezone issues with strftime

* Fix timezone adjustment

* Fix bug
  • Loading branch information
NickM-27 authored Mar 18, 2023
1 parent 732e527 commit e454daf
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion web/src/utils/dateUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const formatUnixTimestampToDateTime = (unixTimestamp: number, config: Dat

// use strftime_fmt if defined in config file
if (strftime_fmt) {
const strftime_locale = strftime.localizeByIdentifier(locale);
const strftime_locale = strftime.timezone(getUTCOffset(date, timezone)).localizeByIdentifier(locale);
return strftime_locale(strftime_fmt, date);
}

Expand Down Expand Up @@ -114,3 +114,18 @@ export const getDurationFromTimestamps = (start_time: number, end_time: number |
}
return duration;
};

/**
* Adapted from https://stackoverflow.com/a/29268535 this takes a timezone string and
* returns the offset of that timezone from UTC in minutes.
* @param timezone string representation of the timezone the user is requesting
* @returns number of minutes offset from UTC
*/
const getUTCOffset = (date: Date, timezone: string): number => {
const utcDate = new Date(date.getTime() - (date.getTimezoneOffset() * 60 * 1000));
// locale of en-CA is required for proper locale format
let iso = utcDate.toLocaleString('en-CA', { timeZone: timezone, hour12: false }).replace(', ', 'T');
iso += '.' + utcDate.getMilliseconds().toString().padStart(3, '0');
const target = new Date(iso + 'Z');
return (target.getTime() - utcDate.getTime()) / 60 / 1000;
}

0 comments on commit e454daf

Please sign in to comment.