Typescript using IRange<Timestamp> and timezone format

As a solution I came up with a function that will be flexible through the front-end (Workshop):

import { DateTime } from 'luxon';
...
public static applyTimezoneToUtcTimestamp(utcTimestamp: Timestamp, timezone: string ="UTC"): Timestamp {
  // Function to get the UTC offset in hours for a specific timezone
  function getUTCOffsetForTimezone(zone: string, date: Date = new Date()): number {
      const dt = DateTime.fromJSDate(date).setZone(zone);
      return dt.offset / 60; // offset in hours
  }
  switch (timezone) {
      case "UTC":
          return utcTimestamp;
      case "Europe/Paris":
          return utcTimestamp.minusHours(getUTCOffsetForTimezone(timezone, new Date(utcTimestamp.toISOString())));
      case "Europe/London":
          return utcTimestamp.minusHours(getUTCOffsetForTimezone(timezone, new Date(utcTimestamp.toISOString())));
      case "Asia/Shanghai":
          return utcTimestamp.minusHours(getUTCOffsetForTimezone(timezone, new Date(utcTimestamp.toISOString())));
      case "America/Chicago":
          return utcTimestamp.minusHours(getUTCOffsetForTimezone(timezone, new Date(utcTimestamp.toISOString())));
      default:
          throw Error(`The provided timezone "${timezone}" is not supported.`);
  }
}    

This way we can change dynamically the timezone offset and being exact with the summer / winter time.of certain impacted countries.

1 Like