This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
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.
Merge pull request #19 from buildo/17-handle_localdatetime
- Loading branch information
Showing
11 changed files
with
193 additions
and
15 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,47 @@ | ||
// @flow | ||
|
||
import { pad2, pad3 } from './util'; | ||
|
||
export default class LocalDateTime extends Date { | ||
|
||
static ISO_DATE_TIME_FORMAT = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(.(\d{3}))?$/ | ||
|
||
static test(isoDateTime: string) { | ||
return LocalDateTime.ISO_DATE_TIME_FORMAT.test(isoDateTime); | ||
} | ||
|
||
constructor(value: LocalDateTime | string | void) { | ||
if (typeof value === 'undefined') { | ||
super(new Date()); | ||
} else if (value instanceof LocalDateTime) { | ||
super( | ||
value.getFullYear(), value.getMonth(), value.getDate(), | ||
value.getHours(), value.getMinutes(), value.getSeconds(), value.getMilliseconds() | ||
); | ||
} else if (typeof value === 'string' && LocalDateTime.ISO_DATE_TIME_FORMAT.test(value)) { | ||
const [ | ||
year, | ||
month, | ||
date, | ||
hours, | ||
minutes, | ||
seconds, | ||
, | ||
milliseconds | ||
] = LocalDateTime.ISO_DATE_TIME_FORMAT.exec(value).slice(1).map(s => parseInt(s, 10)); | ||
super(year, month - 1, date, hours, minutes, seconds, milliseconds || 0); | ||
} else { | ||
throw new Error('Invalid date supplied. Please specify an ISO date time string (YYYY-MM-DDTHH:mm:SS) or a LocalDateTime object.\nhttps://github.com/buildo/local-date#parser'); // eslint-disable-line max-len | ||
} | ||
} | ||
|
||
toISOString(): string { | ||
const date = [this.getFullYear(), pad2(this.getMonth() + 1), pad2(this.getDate())].join('-'); | ||
const time = [ | ||
pad2(this.getHours()), pad2(this.getMinutes()), pad2(this.getSeconds()) | ||
].join(':'); | ||
const milliseconds = pad3(this.getMilliseconds()); | ||
return `${date}T${time}.${milliseconds}`; | ||
} | ||
|
||
} |
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,4 +1,7 @@ | ||
// @flow | ||
|
||
import LocalDate from './LocalDate'; | ||
import LocalDateTime from './LocalDateTime'; | ||
export default LocalDate; | ||
export { LocalDate, LocalDateTime }; | ||
|
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,9 @@ | ||
// @flow | ||
|
||
export function pad2(number: number): number | string { | ||
return number < 10 ? `0${number}` : number; | ||
} | ||
|
||
export function pad3(number: number): number | string { | ||
return number < 10 ? `00${number}` : number < 100 ? `0${number}` : number; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,19 @@ | ||
// @flow | ||
|
||
import LocalDateTime from '../../src/LocalDateTime'; | ||
|
||
describe('Getters', () => { | ||
|
||
const localDateTime = new LocalDateTime(); | ||
|
||
it('toISOString should return an ISO date time', () => { | ||
expect(localDateTime.toISOString()).toMatch(LocalDateTime.ISO_DATE_TIME_FORMAT); | ||
expect(() => new LocalDateTime(localDateTime.toISOString())).not.toThrow(); | ||
}); | ||
|
||
it('toJSON should return an ISO date time', () => { | ||
expect(localDateTime.toJSON()).toMatch(LocalDateTime.ISO_DATE_TIME_FORMAT); | ||
expect(() => new LocalDateTime(localDateTime.toJSON())).not.toThrow(); | ||
}); | ||
|
||
}); |
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,69 @@ | ||
import LocalDateTime from '../../src/LocalDateTime'; | ||
|
||
console.warn = () => {}; // eslint-disable-line no-console | ||
|
||
describe('Parser', () => { | ||
|
||
it('Should be an instance of Date', () => { | ||
expect(new LocalDateTime() instanceof Date).toBe(true); | ||
}); | ||
|
||
it('Should be an instance of LocalDateTime', () => { | ||
expect(new LocalDateTime() instanceof LocalDateTime).toBe(true); | ||
}); | ||
|
||
it('Should return a LocalDateTime with current date time if no argument is passed and should not consider the timezone', () => { | ||
const today = new Date(); | ||
const todayLocalDateTime = new LocalDateTime(); | ||
|
||
expect(today.getFullYear()).toEqual(todayLocalDateTime.getFullYear()); | ||
expect(today.getMonth()).toEqual(todayLocalDateTime.getMonth()); | ||
expect(today.getDate()).toEqual(todayLocalDateTime.getDate()); | ||
expect(today.getHours()).toEqual(todayLocalDateTime.getHours()); | ||
expect(today.getMinutes()).toEqual(todayLocalDateTime.getMinutes()); | ||
expect(today.getSeconds()).toEqual(todayLocalDateTime.getSeconds()); | ||
expect(today.getMilliseconds()).toEqual(todayLocalDateTime.getMilliseconds()); | ||
}); | ||
|
||
it('Should parse ISO date times without considering the timezone', () => { | ||
const year = 1991; | ||
const month = 6; | ||
const day = 4; | ||
const hours = 10; | ||
const minutes = 10; | ||
const seconds = 42; | ||
const milliseconds = 210; | ||
const isoDate = `${year}-0${month}-0${day}T${hours}:${minutes}:${seconds}.${milliseconds}`; | ||
const localDateTime = new LocalDateTime(isoDate); | ||
|
||
expect(localDateTime.getFullYear()).toEqual(year); | ||
expect(localDateTime.getMonth()).toEqual(month - 1); | ||
expect(localDateTime.getDate()).toEqual(day); | ||
expect(localDateTime.getHours()).toEqual(hours); | ||
expect(localDateTime.getMinutes()).toEqual(minutes); | ||
expect(localDateTime.getSeconds()).toEqual(seconds); | ||
expect(localDateTime.getMilliseconds()).toEqual(milliseconds); | ||
}); | ||
|
||
it('Should clone instances of LocalDateTimes', () => { | ||
const localDateTime = new LocalDateTime('2016-05-20T10:10:42'); | ||
const clonedLocalDateTime = new LocalDateTime(localDateTime); | ||
|
||
expect(localDateTime).toEqual(clonedLocalDateTime); | ||
}); | ||
|
||
it('Should throw an error if argument is invalid', () => { | ||
const newLocalDateTime = argument => () => { | ||
return new LocalDateTime(argument); | ||
}; | ||
|
||
expect(newLocalDateTime(new Date())).toThrow(); | ||
expect(newLocalDateTime(null)).toThrow(); | ||
expect(newLocalDateTime(1483549074687)).toThrow(); // timestamp | ||
expect(newLocalDateTime(2016, 5, 21)).toThrow(); | ||
expect(newLocalDateTime('2017-01-04T18:04:52Z')).toThrow(); | ||
expect(newLocalDateTime('2017-01-04T18:04:52.438Z')).toThrow(); | ||
expect(newLocalDateTime('2017-01-04T18:04:52+00:00')).toThrow(); | ||
}); | ||
|
||
}); |
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,24 @@ | ||
// @flow | ||
|
||
import LocalDateTime from '../../src/LocalDateTime'; | ||
|
||
describe('Static properties', () => { | ||
|
||
it('ISO_DATE_TIME_FORMAT should be a valid RegExp', () => { | ||
expect(LocalDateTime.ISO_DATE_TIME_FORMAT instanceof RegExp).toBe(true); | ||
}); | ||
|
||
it('test should return "true" if argument is a valid (local) ISO date time', () => { | ||
expect(LocalDateTime.test('2016-05-20T10:10:42')).toBe(true); | ||
expect(LocalDateTime.test('2016-05-20T10:10:42.234')).toBe(true); | ||
}); | ||
|
||
it('test should return "false" if argument is not a valid (local) ISO date time', () => { | ||
expect(LocalDateTime.test('2017/01/04')).toBe(false); | ||
expect(LocalDateTime.test('05-20-2017')).toBe(false); | ||
expect(LocalDateTime.test('2017-01-04T18:04:52Z')).toBe(false); | ||
expect(LocalDateTime.test('2017-01-04T18:04:52.438Z')).toBe(false); | ||
expect(LocalDateTime.test('2017-01-04T18:04:52+00:00')).toBe(false); | ||
}); | ||
|
||
}); |