-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Krick <matt.krick@gmail.com>
- Loading branch information
Showing
25 changed files
with
469 additions
and
447 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
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
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
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,32 @@ | ||
import dayjs from 'dayjs' | ||
import {fromDateTime, toDateTime} from '../rruleUtil' | ||
|
||
test('toDateTime: Should handle TZID', () => { | ||
// at noon UTC what time is it in Phoenix? | ||
const now = dayjs('2022-01-01T12:00:00Z') | ||
const tzid = 'America/Phoenix' | ||
const str = toDateTime(now, tzid) | ||
expect(str).toBe('20220101T050000') | ||
}) | ||
|
||
test('toDateTime: Should handle UTC TZID', () => { | ||
// at noon UTC what time is it in UTC? | ||
const now = dayjs('2022-01-01T12:00:00Z') | ||
const tzid = 'UTC' | ||
const str = toDateTime(now, tzid) | ||
expect(str).toBe('20220101T120000') | ||
}) | ||
|
||
test('fromDateTime: Should handle TZID', () => { | ||
const dateTimeStr = '20220101T050000' | ||
const tzid = 'America/Phoenix' | ||
const day = fromDateTime(dateTimeStr, tzid) | ||
expect(day.toISOString()).toBe('2022-01-01T12:00:00.000Z') | ||
}) | ||
|
||
test('fromDateTime: Should handle UTC TZID', () => { | ||
const dateTimeStr = '20220101T050000' | ||
const tzid = 'UTC' | ||
const day = fromDateTime(dateTimeStr, tzid) | ||
expect(day.toISOString()).toBe('2022-01-01T05:00:00.000Z') | ||
}) |
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 |
---|---|---|
@@ -1,21 +1,33 @@ | ||
import {datetime} from 'rrule' | ||
import dayjs, {Dayjs} from 'dayjs' | ||
import customParsePlugin from 'dayjs/plugin/customParseFormat' | ||
import timezonePlugin from 'dayjs/plugin/timezone' | ||
import utcPlugin from 'dayjs/plugin/utc' | ||
import {RRule} from 'rrule' | ||
|
||
export const getRRuleDateFromJSDate = (date: Date) => { | ||
return datetime( | ||
date.getFullYear(), | ||
date.getMonth() + 1, | ||
date.getDate(), | ||
date.getHours(), | ||
date.getMinutes() | ||
) | ||
dayjs.extend(customParsePlugin) | ||
dayjs.extend(utcPlugin) | ||
dayjs.extend(timezonePlugin) | ||
|
||
// the RRule package requires dstart to be a date object set to a negative UTC offset. It's ugly! | ||
export const toRRuleDateTime = (date: Dayjs) => { | ||
return date.tz('UTC', true).toDate() | ||
} | ||
|
||
export const fromRRuleDateTime = (rrule: RRule) => { | ||
const {options} = rrule | ||
const {dtstart, tzid} = options | ||
const tzidTimeStr = `${dtstart.getUTCFullYear()}-${dtstart.getUTCMonth() + 1}-${dtstart.getUTCDate()} ${dtstart.getUTCHours()}:${dtstart.getUTCMinutes()}` | ||
return tzid ? dayjs.tz(tzidTimeStr, tzid) : dayjs(tzidTimeStr) | ||
} | ||
|
||
// These are used by rrule-rust on the server, which has a special DateTime object | ||
export const toDateTime = (date: Dayjs, tzid: string) => { | ||
return tzid | ||
? date.tz(tzid).format('YYYYMMDD[T]HHmmss') | ||
: date.utc().format('YYYYMMDD[T]HHmmss[Z]') | ||
} | ||
|
||
export const getJSDateFromRRuleDate = (rruleDate: Date) => { | ||
return new Date( | ||
rruleDate.getUTCFullYear(), | ||
rruleDate.getUTCMonth(), | ||
rruleDate.getUTCDate(), | ||
rruleDate.getUTCHours(), | ||
rruleDate.getUTCMinutes() | ||
) | ||
export const fromDateTime = (rfc5545String: string, tzid: string) => { | ||
const rawDate = dayjs.utc(rfc5545String, 'YYYYMMDD[T]HHmmss') | ||
return tzid ? dayjs.tz(rawDate.format('YYYY-MM-DD HH:mm'), tzid) : rawDate | ||
} |
Oops, something went wrong.