time-convertor-ts

Convert time across timezones (typescript)
git clone http://git.hanabi.in/repos/time-convertor-ts.git
Log | Files | Refs | README | LICENSE

index.ts (3649B)


      1 import moment from "moment-timezone";
      2 
      3 import { IDisplayTime, IGetAbbr } from "../utils/interfaces";
      4 
      5 /**
      6  * @description clears the zones in localStorage
      7  */
      8 function clearTimezones<never>() {
      9   localStorage.setItem("zones", "{}");
     10 }
     11 
     12 /**
     13  *
     14  * @param fmtStr {string} Formatting string (ex. 'HH:mm:ss MMMM DD, YYYY')
     15  * @param time {moment.Moment} Time to be formatted
     16  * @param timezone {string} Timezone to set
     17  * @returns {string} formatted time
     18  */
     19 function displayTime({ fmtStr, time, timezone }: IDisplayTime) {
     20   return time.tz(timezone).format(fmtStr);
     21 }
     22 
     23 /**
     24  * @description Convert a string like `foo_bar_baz` to `foo bar baz`
     25  * @param {string} str
     26  */
     27 function friendlyStr(str: string): string {
     28   return str.replace(/_/g, " ");
     29 }
     30 
     31 /**
     32  *
     33  * @description Obtain the abbreviation of a timezone
     34  * @param timezone {string} Time zone string
     35  * @param time {moment.Moment} moment object
     36  * @returns {string}
     37  */
     38 function getAbbr({ timezone, time }: IGetAbbr): string {
     39   // @ts-ignore -- they most certainly won't be undefined
     40   return moment.tz.zone(timezone).abbr(time);
     41 }
     42 
     43 /**
     44  * @description Fetch list of saved time zones from the localStorage
     45  * @returns {string[] | []} Array of saved time zones
     46  */
     47 function getSavedZones<never>(): string[] | [] {
     48   // query localStorage to get JSON as string
     49   const savedData: string = localStorage.getItem("zones") || "{}";
     50   // extract array
     51   const savedZones: string[] = JSON.parse(savedData).list || [];
     52   return savedZones;
     53 }
     54 
     55 /**
     56  * @description get yesterday's date as a moment object
     57  * @ref { https://flaviocopes.com/how-to-get-yesterday-date-javascript/ }
     58  * @returns {moment.Moment}
     59  */
     60 function getYesterday<never>(): moment.Moment {
     61   const today = new Date();
     62   const yesterday = new Date();
     63 
     64   yesterday.setDate(today.getDate() - 1);
     65   return moment(yesterday);
     66 }
     67 
     68 /**
     69  * @description Removes one zone from localStorage
     70  * @param {string} zone zone to be removed from the localStorage
     71  */
     72 function removeTimeZone(zone: string = ""): void {
     73   // get saved zones
     74   const savedZones = getSavedZones();
     75   // filter out
     76   const updatedZones = savedZones.filter((savedZone) => savedZone !== zone);
     77   // stringify JSON
     78   const newZonesStr = JSON.stringify({ list: updatedZones });
     79   // save to localStorage
     80   localStorage.setItem("zones", newZonesStr);
     81 }
     82 
     83 /**
     84  *
     85  * @description takes in an array of timezones and updates the localStorage
     86  * @param timezone {string[]} An array of timezones
     87  */
     88 function saveTimezones(timezone: string[] | [] = []) {
     89   // get saved zones
     90   const savedZones = getSavedZones();
     91   // new array of sorted time zones
     92   const newZones = Array.from(new Set([...savedZones, ...timezone])).sort();
     93   // stringify JSON
     94   const newZonesStr = JSON.stringify({ list: newZones });
     95   // save to localStorage
     96   localStorage.setItem("zones", newZonesStr);
     97 }
     98 
     99 /**
    100  * @description Convert a string like `foo bar baz` to `foo_bar_baz`
    101  * @param {string} str
    102  */
    103 function unfriendlyStr(str: string): string {
    104   return str.replace(/ /g, "_");
    105 }
    106 
    107 export {
    108   clearTimezones,
    109   displayTime,
    110   friendlyStr,
    111   getAbbr,
    112   getSavedZones,
    113   getYesterday,
    114   removeTimeZone,
    115   saveTimezones,
    116   unfriendlyStr,
    117 };
    118 
    119 const basePath = process.env.PUBLIC_URL;
    120 const DEFAULT_TZ = "UTC";
    121 const HM = "HH:mm";
    122 const HMMDY = "HH:mm MMMM DD, YYYY";
    123 const HMSDMY = "HH:mm:ss MMMM DD, YYYY";
    124 const localTimezone = moment.tz.guess();
    125 const MAX_DATE = "2038-01-18";
    126 const timezoneList = moment.tz.names().sort();
    127 const YEAR = new Date().getFullYear();
    128 const YMD = "YYYY-MM-DD";
    129 
    130 export {
    131   basePath,
    132   DEFAULT_TZ,
    133   HM,
    134   HMMDY,
    135   HMSDMY,
    136   localTimezone,
    137   MAX_DATE,
    138   timezoneList,
    139   YEAR,
    140   YMD,
    141 };