time-convertor-ts

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

commit bbd15711834e1a6ada16df8084d5d66819afdae9
parent 35c71ba7b15c8c75b2d1779413bec6a080e7d151
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Mon, 25 May 2020 08:40:28 +0530

interfaces + more mothods

Diffstat:
Msrc/utils/index.ts | 34++++++++++++++++++++++++++++++++++
Asrc/utils/interfaces.tsx | 23+++++++++++++++++++++++
2 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/src/utils/index.ts b/src/utils/index.ts @@ -1,6 +1,22 @@ import moment from 'moment-timezone'; /** + * @description clears the localStorage + */ +function clearTimezones() { + localStorage.clear() +} +/** + * + * @param fmtStr {string} Formatting string (ex. 'HH:mm:ss MMMM DD, YYYY') + * @param time {moment.Moment} Time to be formatted + * @param timezone {string} Timezone to set + * @returns {string} formatted time + */ +function displayTime({ fmtStr, time, timezone }: { fmtStr: string; time: moment.Moment; timezone: string }) { + return time.tz(timezone).format(fmtStr); +} +/** * @description Convert a string like `foo_bar_baz` to `foo bar baz` * @param {string} str */ @@ -9,6 +25,19 @@ function friendlyStr(str: string): string { } /** + * + * @param timezone {string[]} An array of timezones + * @description takes in an array of timezones and updates the localStorage + */ +function saveTimezones(timezone: string[] | [] = []) { + const savedData: string = localStorage.getItem('zones') || '{}'; + const savedZones: string[] = JSON.parse(savedData).list || []; + const newZones = Array.from(new Set([...savedZones, ...timezone])).sort(); + const newZonesStr = JSON.stringify({ list: newZones }); + localStorage.setItem('zones', newZonesStr); +} + +/** * @description Convert a string like `foo bar baz` to `foo_bar_baz` * @param {string} str */ @@ -17,17 +46,22 @@ function unfriendlyStr(str: string): string { } export { + clearTimezones, + displayTime, friendlyStr, + saveTimezones, unfriendlyStr }; const DEFAULT_TZ = 'UTC'; +const HMSDMY = 'HH:mm:ss MMMM, DD, YYYY'; const localTimezone = moment.tz.guess(); const timezoneList = moment.tz.names().sort(); const YEAR = new Date().getFullYear(); export { DEFAULT_TZ, + HMSDMY, localTimezone, timezoneList, YEAR diff --git a/src/utils/interfaces.tsx b/src/utils/interfaces.tsx @@ -0,0 +1,23 @@ +import React from 'react'; + +export interface ICurrentConvertor { + time: moment.Moment; + TZ1: string; + TZ2: string; + setTZ1: React.Dispatch<React.SetStateAction<string>>; + setTZ2: React.Dispatch<React.SetStateAction<string>>; +} + +export interface ISimpleConvertor { + time: moment.Moment; + TZ1: string; + setTZ1: React.Dispatch<React.SetStateAction<string>>; +} + +export interface ITimezoneInput { + autofocus: boolean; + changeValue: React.Dispatch<React.SetStateAction<string>>; + id: string; + placeholder: string; + TZ: string; +}