forked from adamgibbons/ics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f031719
commit 36a4984
Showing
13 changed files
with
64 additions
and
217 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,26 +1,13 @@ | ||
import uuidv1 from 'uuid/v1' | ||
import dayjs from 'dayjs' | ||
import utc from 'dayjs/plugin/utc'; | ||
import { formatUTCDateAsUTC } from './utils' | ||
|
||
dayjs.extend(utc); | ||
|
||
const now = dayjs().utc() | ||
import { formatDate } from './utils' | ||
|
||
const defaults = { | ||
title: 'Untitled event', | ||
productId: 'adamgibbons/ics', | ||
method: 'PUBLISH', | ||
uid: uuidv1(), | ||
timestamp: formatUTCDateAsUTC([ | ||
now.get('year'), | ||
now.get('month') + 1, | ||
now.get('date'), | ||
now.get('hours'), | ||
now.get('minutes'), | ||
now.get('seconds') | ||
]), | ||
start: formatUTCDateAsUTC() | ||
timestamp: formatDate(null, 'utc'), | ||
start: formatDate(null, 'utc') | ||
} | ||
|
||
export default defaults |
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,36 +1,41 @@ | ||
import dayjs from 'dayjs' | ||
import { | ||
formatLocalDateAsLocal, | ||
formatLocalDateAsUTC, | ||
formatUTCDateAsLocal, | ||
formatUTCDateAsUTC | ||
} from './index' | ||
|
||
function formatLocalDate(args = [], outputType) { | ||
if (outputType == 'utc') { | ||
return formatLocalDateAsUTC(args, outputType) | ||
} | ||
return formatLocalDateAsLocal(args, outputType) | ||
} | ||
|
||
function formatUTCDate(args = [], outputType) { | ||
if (outputType == 'utc') { | ||
return formatUTCDateAsUTC(args, outputType) | ||
} | ||
return formatUTCDateAsLocal(args, outputType) | ||
} | ||
const pad = n => n < 10 ? `0${n}` : `${n}` | ||
|
||
export default function formatDate(args = [], outputType = 'utc', inputType = 'local') { | ||
const [year, month, date, hours, minutes, seconds] = args | ||
if (Array.isArray(args) && args.length === 3) { | ||
const [year, month, date] = args | ||
return `${year}${pad(month)}${pad(date)}` | ||
} | ||
|
||
if (args.length === 3) { | ||
return dayjs(new Date(year, month - 1, date)).format('YYYYMMDD') | ||
let outDate = new Date(new Date().setUTCSeconds(0, 0)) | ||
if (Array.isArray(args) && args.length > 0 && args[0]) { | ||
const [year, month, date, hours = 0, minutes = 0, seconds = 0] = args | ||
if (inputType === 'local') { | ||
outDate = new Date(year, month - 1, date, hours, minutes, seconds) | ||
} else { | ||
outDate = new Date(Date.UTC(year, month - 1, date, hours, minutes, seconds)) | ||
} | ||
} | ||
|
||
if (inputType === 'local') { | ||
return formatLocalDate([year, month, date, hours, minutes, seconds || 0], outputType); | ||
if (outputType === 'local') { | ||
return [ | ||
outDate.getFullYear(), | ||
pad(outDate.getMonth() + 1), | ||
pad(outDate.getDate()), | ||
'T', | ||
pad(outDate.getHours()), | ||
pad(outDate.getMinutes()), | ||
pad(outDate.getSeconds()) | ||
].join('') | ||
} | ||
|
||
// type === 'utc' | ||
return formatUTCDate([year, month, date, hours, minutes, seconds || 0], outputType); | ||
return [ | ||
outDate.getUTCFullYear(), | ||
pad(outDate.getUTCMonth() + 1), | ||
pad(outDate.getUTCDate()), | ||
'T', | ||
pad(outDate.getUTCHours()), | ||
pad(outDate.getUTCMinutes()), | ||
pad(outDate.getUTCSeconds()), | ||
'Z' | ||
].join('') | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,17 +1,36 @@ | ||
import dayjs from 'dayjs' | ||
import utc from 'dayjs/plugin/utc' | ||
|
||
dayjs.extend(utc) | ||
|
||
import { formatDate } from '../../src/utils' | ||
import { expect } from 'chai' | ||
|
||
describe('utils.formatDate', () => { | ||
describe('utils.formatDate', () => { | ||
it('defaults to local time input and UTC time output when no type passed', () => { | ||
const now = dayjs(new Date(2017, 7-1, 16, 22, 30)).utc().format('YYYYMMDDTHHmm00') | ||
expect(formatDate([2017, 7, 16, 22, 30])).to.equal(now+'Z') | ||
}) | ||
it('sets a local (i.e. floating) time when specified', () => { | ||
expect(formatDate([1998, 1, 18, 23, 0], 'local')).to.equal('19980118T230000') | ||
expect(formatDate([1998, 6, 18, 23, 0], 'local', 'local')).to.equal('19980618T230000') | ||
}) | ||
it('sets a date value when passed only three args', () => { | ||
expect(formatDate([2018, 2, 11])).to.equal('20180211') | ||
}) | ||
it('defaults to NOW in UTC date-time when no args passed', () => { | ||
const now = dayjs().utc().format('YYYYMMDDTHHmm00') + 'Z' | ||
expect(formatDate(undefined, 'utc')).to.equal(now) | ||
}) | ||
it('sets a UTC date-time when passed well-formed args', () => { | ||
expect(formatDate([2017, 9, 25, 0, 30], 'utc', 'utc')).to.equal('20170925T003000Z') | ||
expect(formatDate([2017, 1, 31], 'utc','utc')).to.equal('20170131') | ||
}) | ||
it('sets a local DATE-TIME value to NOW when passed nothing', () => { | ||
const now = dayjs().format('YYYYMMDDTHHmm00') | ||
expect(formatDate(undefined, 'local', 'local')).to.equal(now) | ||
}) | ||
it('sets a local DATE-TIME value when passed args', () => { | ||
expect(formatDate([1998, 1, 18, 23, 9, 59], 'local', 'local')) | ||
.to.equal('19980118T230959') | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.