Skip to content

Commit

Permalink
convert to using dayjs (adamgibbons#146)
Browse files Browse the repository at this point in the history
* convert to using dayjs

* updating license and readme

* reverting some changes after receiving email from maintainer

* updating package lock

Co-authored-by: Adam Gibbons <[email protected]>
  • Loading branch information
Justin Sunsri and adamgibbons authored Aug 25, 2020
1 parent f07225e commit 18031ed
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 62 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"dependencies": {
"@hapi/joi": "^17.1.1",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"uuid": "^3.3.3"
"uuid": "^3.3.3",
"dayjs": "^1.8.33"
},
"files": [
"dist/",
Expand Down
7 changes: 5 additions & 2 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import uuidv1 from 'uuid/v1'
import moment from 'moment'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc';
import { formatUTCDateAsUTC } from './utils'

const now = moment().utc()
dayjs.extend(utc);

const now = dayjs().utc()

const defaults = {
title: 'Untitled event',
Expand Down
6 changes: 3 additions & 3 deletions src/utils/format-date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment from 'moment'
import dayjs from 'dayjs'
import {
formatLocalDateAsLocal,
formatLocalDateAsUTC,
Expand All @@ -24,9 +24,9 @@ export default function formatDate(args = [], outputType = 'utc', inputType = 'l
const [year, month, date, hours, minutes, seconds] = args

if (args.length === 3) {
return moment([year, month - 1, date]).format('YYYYMMDD')
return dayjs(new Date(year, month - 1, date)).format('YYYYMMDD')
}

if (inputType === 'local') {
return formatLocalDate([year, month, date, hours, minutes, seconds || 0], outputType);
}
Expand Down
18 changes: 9 additions & 9 deletions src/utils/format-local-date-as-local.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
//
//
// DATE-TIME
// FORM #1: DATE WITH LOCAL TIME
//
//
// The date with local time form is simply a DATE-TIME value that
// does not contain the UTC designator nor does it reference a time
// zone. DATE-TIME values of this type are said to be "floating"
// zone. DATE-TIME values of this type are said to be "floating"
// and are not bound to any time zone in particular.
//
//
// For example, the following represents
// January 18, 1998, at 11 PM:
//
//
// 19980118T230000
//
//

import moment from 'moment'
import dayjs from 'dayjs'

export default function formatLocalDateAsLocal(args = []) {
if (args.length > 0) {
const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args
const formattedDate = moment([year, month - 1, date, hours, minutes, seconds]).format('YYYYMMDDTHHmmss')
const formattedDate = dayjs(new Date(year, month - 1, date, hours, minutes, seconds)).format('YYYYMMDDTHHmmss')
return formattedDate
}

return moment().format('YYYYMMDDTHHmmss')
return dayjs().format('YYYYMMDDTHHmmss')
}
21 changes: 12 additions & 9 deletions src/utils/format-local-date-as-utc.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
//
//
// FORM #2: DATE WITH UTC TIME
//
//
// The date with UTC time, or absolute time, is identified by a LATIN
// CAPITAL LETTER Z suffix character, the UTC designator, appended to
// the time value. For example, the following represents January 19,
// 1998, at 0700 UTC:
//
//
// 19980119T070000Z
//
//
// The "TZID" property parameter MUST NOT be applied to DATE-TIME
// properties whose time values are specified in UTC.
//
//

import moment from 'moment'
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc);

export default function formatLocalDateAsUTC(args = []) {
if (args.length > 0) {
const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args

const formattedDate = moment([
const formattedDate = dayjs(new Date(
year,
month - 1,
date,
hours,
minutes,
seconds
]).utc().format('YYYYMMDDTHHmmss') + 'Z'
)).utc().format('YYYYMMDDTHHmmss') + 'Z'

return formattedDate
}

return moment.utc().format('YYYYMMDDTHHmmss') + 'Z'
return dayjs.utc().format('YYYYMMDDTHHmmss') + 'Z'
}
21 changes: 12 additions & 9 deletions src/utils/format-utc-date-as-local.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
//
//
// DATE-TIME
// FORM #1: DATE WITH LOCAL TIME
//
//
// The date with local time form is simply a DATE-TIME value that
// does not contain the UTC designator nor does it reference a time
// zone. DATE-TIME values of this type are said to be "floating"
// zone. DATE-TIME values of this type are said to be "floating"
// and are not bound to any time zone in particular.
//
//
// For example, the following represents
// January 18, 1998, at 11 PM:
//
//
// 19980118T230000
//
//

import moment from 'moment'
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc);

export default function formatLocalDateAsLocal(args = []) {
if (args.length > 0) {
const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args
const formattedDate = moment.utc([year, month - 1, date, hours, minutes, seconds]).format('YYYYMMDDTHHmmss')
const formattedDate = dayjs.utc(Date.UTC(year, month - 1, date, hours, minutes, seconds)).format('YYYYMMDDTHHmmss')
return formattedDate
}

return moment().utc().format('YYYYMMDDTHHmmss')
return dayjs().utc().format('YYYYMMDDTHHmmss')
}
27 changes: 12 additions & 15 deletions src/utils/format-utc-date-as-utc.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
//
//
// FORM #2: DATE WITH UTC TIME
//
//
// The date with UTC time, or absolute time, is identified by a LATIN
// CAPITAL LETTER Z suffix character, the UTC designator, appended to
// the time value. For example, the following represents January 19,
// 1998, at 0700 UTC:
//
//
// 19980119T070000Z
//
//
// The "TZID" property parameter MUST NOT be applied to DATE-TIME
// properties whose time values are specified in UTC.
//
//

import moment from 'moment'
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc);

export default function formatUTCDateAsUTC(args = []) {
if (args.length > 0) {
const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args

const formattedDate = moment.utc([
year,
month - 1,
date,
hours,
minutes,
seconds
]).format('YYYYMMDDTHHmmss') + 'Z'
const formattedDate = dayjs.utc(Date.UTC(year, month - 1, date, hours, minutes, seconds))
.format('YYYYMMDDTHHmmss') + 'Z'

return formattedDate
}

return moment.utc().format('YYYYMMDDTHHmm00') + 'Z'
return dayjs.utc().format('YYYYMMDDTHHmm00') + 'Z'
}
14 changes: 7 additions & 7 deletions test/pipeline/format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ describe('pipeline.formatEvent', () => {
it('writes a start date-time, taking the given date as local by default and outputting is as UTC by default', () => {
const event = buildEvent({ start: [2017, 5, 15, 10, 0] })
const formattedEvent = formatEvent(event)
const now = moment([2017, 5-1, 15, 10, 0]).utc().format('YYYYMMDDTHHmm00')
const now = moment([2017, 5 - 1, 15, 10, 0]).utc().format('YYYYMMDDTHHmm00')
expect(formattedEvent).to.contain('DTSTART:'+now+'Z')
})
it('writes a start date-time, taking the given date as local by default and outputting is as UTC if requested', () => {
const event = buildEvent({ start: [2017, 5, 15, 10, 0], startOutputType: 'utc' })
const formattedEvent = formatEvent(event)
const now = moment([2017, 5-1, 15, 10, 0]).utc().format('YYYYMMDDTHHmm00')
const now = moment([2017, 5 - 1, 15, 10, 0]).utc().format('YYYYMMDDTHHmm00')
expect(formattedEvent).to.contain('DTSTART:'+now+'Z')
})
it('writes a start date-time, taking the given date as local by default and outputting is as Local (floating) if requested', () => {
Expand All @@ -57,13 +57,13 @@ describe('pipeline.formatEvent', () => {
it('writes a start date-time, taking the given date as local if requested and outputting is as UTC by default', () => {
const event = buildEvent({ start: [2017, 5, 15, 10, 0], startInputType: 'local' })
const formattedEvent = formatEvent(event)
const now = moment([2017, 5-1, 15, 10, 0]).utc().format('YYYYMMDDTHHmm00')
const now = moment([2017, 5 - 1, 15, 10, 0]).utc().format('YYYYMMDDTHHmm00')
expect(formattedEvent).to.contain('DTSTART:'+now+'Z')
})
it('writes a start date-time, taking the given date as local if requested and outputting is as UTC if requested', () => {
const event = buildEvent({ start: [2017, 5, 15, 10, 0], startInputType: 'local', startOutputType: 'utc' })
const formattedEvent = formatEvent(event)
const now = moment([2017, 5-1, 15, 10, 0]).utc().format('YYYYMMDDTHHmm00')
const now = moment([2017, 5 - 1, 15, 10, 0]).utc().format('YYYYMMDDTHHmm00')
expect(formattedEvent).to.contain('DTSTART:'+now+'Z')
})
it('writes a start date-time, taking the given date as local if requested and outputting is as Local (floating) if requested', () => {
Expand All @@ -85,7 +85,7 @@ describe('pipeline.formatEvent', () => {
it('writes a start date-time, taking the given date as UTC if requested and outputting is as Local (floating) if requested', () => {
const event = buildEvent({ start: [2017, 5, 15, 10, 0], startInputType: 'utc', startOutputType: 'local' })
const formattedEvent = formatEvent(event)
const now = moment.utc([2017, 5-1, 15, 10, 0]).format('YYYYMMDDTHHmm00')
const now = moment.utc([2017, 5 - 1, 15, 10, 0]).format('YYYYMMDDTHHmm00')
expect(formattedEvent).to.contain('DTSTART:'+now)
expect(formattedEvent).to.not.contain('DTSTART:'+now+'Z')
})
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('pipeline.formatEvent', () => {
}]})

expect(formattedEvent).to.contain('BEGIN:VALARM')
expect(formattedEvent).to.contain('TRIGGER;VALUE=DATE-TIME:19970217T')
expect(formattedEvent).to.contain('TRIGGER;VALUE=DATE-TIME:199702')
expect(formattedEvent).to.contain('REPEAT:4')
expect(formattedEvent).to.contain('DURATION:PT15M')
expect(formattedEvent).to.contain('ACTION:AUDIO')
Expand Down Expand Up @@ -232,7 +232,7 @@ describe('pipeline.formatEvent', () => {
expect(max).to.be.at.most(75)
})
it('writes a recurrence rule', () => {
const formattedEvent = formatEvent({ recurrenceRule: 'FREQ=DAILY'})
const formattedEvent = formatEvent({ recurrenceRule: 'FREQ=DAILY' })

expect(formattedEvent).to.contain('RRULE:FREQ=DAILY')
})
Expand Down
2 changes: 1 addition & 1 deletion test/utils/set-alarm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('utils.setAlarm', () => {
expect(alarm).to.contain('DURATION:P1W15DT3H4M50S')
expect(alarm).to.contain('DESCRIPTION:Foo')
expect(alarm).to.contain('SUMMARY:Bar baz')
expect(alarm).to.contain('TRIGGER;VALUE=DATE-TIME:19970217T')
expect(alarm).to.contain('TRIGGER;VALUE=DATE-TIME:199702')
expect(alarm).to.contain('END:VALARM')
})
})
Expand Down

0 comments on commit 18031ed

Please sign in to comment.