-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fix daily start time timezone issues
- Loading branch information
1 parent
1445b8d
commit cc3a1e9
Showing
3 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import dayjs from 'dayjs'; | ||
|
||
var utc = require('dayjs/plugin/utc'); | ||
var timezone = require('dayjs/plugin/timezone'); | ||
dayjs.extend(utc); | ||
dayjs.extend(timezone); | ||
|
||
export type Time = string; | ||
|
||
const isStringTime = (time: string | Time): time is Time => { | ||
const [hours, minutes] = time.split(':'); | ||
|
||
if (Number(hours) < 0 || Number(hours) > 23) return false; | ||
if (Number(minutes) < 0 || Number(minutes) > 59) return false; | ||
|
||
return true; | ||
}; | ||
|
||
export const getTimeFromUTCTime = (utcTime: Time): Time => { | ||
if (!isStringTime(utcTime)) throw new Error('Time is not a valid string'); | ||
|
||
const day = dayjs() | ||
.utc() | ||
.set('hours', Number(utcTime.split(':')[0])) | ||
.set('minutes', Number(utcTime.split(':')[1])) | ||
// @ts-ignore | ||
.tz(dayjs.tz.guess()); | ||
|
||
return day.format('HH:mm') as Time; | ||
}; | ||
|
||
export const getUTCTimeFromTime = (time: Time): Time => { | ||
if (!isStringTime(time)) throw new Error('Time is not a valid string'); | ||
|
||
const day = dayjs() | ||
.set('hours', Number(time.split(':')[0])) | ||
.set('minutes', Number(time.split(':')[1])) | ||
// @ts-ignore | ||
.tz('UTC'); | ||
|
||
return day.format('HH:mm') as Time; | ||
}; |