Fix timezone adjustment

This commit is contained in:
Nick Mowen 2023-03-17 13:52:45 -06:00
parent f6946d283f
commit 02eda1d7e7

View File

@ -56,7 +56,7 @@ export const formatUnixTimestampToDateTime = (unixTimestamp: number, config: Dat
// use strftime_fmt if defined in config file // use strftime_fmt if defined in config file
if (strftime_fmt) { if (strftime_fmt) {
const strftime_locale = strftime.timezone(getTimezoneOffset(timezone)).localizeByIdentifier(locale); const strftime_locale = strftime.timezone(getTimezoneOffset(date, timezone)).localizeByIdentifier(locale);
return strftime_locale(strftime_fmt, date); return strftime_locale(strftime_fmt, date);
} }
@ -121,10 +121,11 @@ export const getDurationFromTimestamps = (start_time: number, end_time: number |
* @param timezone string representation of the timezone the user is requesting * @param timezone string representation of the timezone the user is requesting
* @returns number of minutes offset from UTC * @returns number of minutes offset from UTC
*/ */
const getTimezoneOffset = (timezone: string): number => { const getTimezoneOffset = (date: Date, timezone: string): number => {
const date = new Date(); const utcDate = new Date(date.getMilliseconds() - (date.getTimezoneOffset() * 60 * 1000));
let iso = date.toLocaleString('en-us', { timeZone: timezone, hour12: false }).replace(', ', 'T'); // locale of en-CA is required for proper locale format
iso += '.' + date.getMilliseconds().toString().padStart(3, '0'); let iso = utcDate.toLocaleString('en-CA', { timeZone: timezone, hour12: false }).replace(', ', 'T');
const lie = new Date(iso + 'Z'); iso += '.' + utcDate.getMilliseconds().toString().padStart(3, '0');
return -(lie.getMilliseconds() - date.getMilliseconds()) / 60 / 1000; const target = new Date(iso + 'Z');
return (target.getTime() - utcDate.getTime()) / 60 / 1000;
} }