time-convertor

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

commit 5da3f4896e0cd082ce8393abf979dc37a9c7c4c2
parent 1d61ea8663658c5bbbea211aac922a2cbbda234f
Author: Agastya Chandrakant <me@hanabi.in>
Date:   Mon, 18 May 2020 12:00:28 +0530

Refactored

Diffstat:
Msrc/comp/currentTime.js | 32+++++++++-----------------------
1 file changed, 9 insertions(+), 23 deletions(-)

diff --git a/src/comp/currentTime.js b/src/comp/currentTime.js @@ -1,34 +1,21 @@ import React from 'react'; -import moment from 'moment-timezone'; import AutoCompleteText from './autoCompleteText'; -function useTime() { - const [time, setTime] = React.useState(new Date()); - function updateTime() { - setTime(new Date()); - } - React.useEffect(() => { - let interval = setInterval(() => updateTime(), 1000); - return () => clearInterval(interval); - }); - return time; -} +import useTime from './helpers/useTime'; +import prepData from './helpers/prepData'; function CurrentTime() { - const now = useTime(); const [TZ, setTZ] = React.useState('UTC'); - const ISOtime = now.toISOString(); - const tz = moment.tz.guess(); - const localTZ = tz.replace(/_/g, ' '); - const nowMoment = moment(ISOtime); - const localAbbr = moment.tz.zone(tz).abbr(nowMoment); - const otherAbbr = moment.tz.zone(TZ).abbr(nowMoment); + const { firstAbbr, fmtTime1, fmtTime2, firstTZ, secondAbbr } = prepData( + useTime(), + TZ + ); return ( <div className="time-conversion"> <div className="row mb-3" id="labels"> <div className="col text-right" id="local-time-lbl-box"> <h4 aria-level="4" id="local-time-lbl"> - Local time ({localTZ}) + Local time ({firstTZ}) </h4> </div> <div className="col" id="converted-time-lbl-box"> @@ -38,13 +25,12 @@ function CurrentTime() { <div className="row" id="times"> <div className="col text-right" id="local-time-box"> <h4 aria-level="4" id="local-time"> - {nowMoment.tz(localTZ).format('HH:mm:ss MMM DD, YYYY')} ({localAbbr} - ) + {fmtTime1} ({firstAbbr}) </h4> </div> <div className="col" id="converted-time-box"> <h4 aria-level="4" id="converted-time"> - {nowMoment.tz(TZ).format('HH:mm:ss MMM DD, YYYY')} ({otherAbbr}) + {fmtTime2} ({secondAbbr}) </h4> </div> </div>