diff --git a/docs/API.md b/docs/API.md index 3050f8d..d4411de 100644 --- a/docs/API.md +++ b/docs/API.md @@ -216,7 +216,7 @@ const date = getZonedTime(date, berlin) // Returns the UNIX timestamp (UTC) in milliseconds ``` -This method is usually used with incomplete time objects, which are entered by the user, or parsed from date strings without time zone information. (A complete [time object] contains properties `epoch` with the UTC time and `zone` with the time zone information.) +This method is usually used with incomplete time objects, which are entered by the user, or parsed from date strings without time zone information. (A complete [time object] contains properties `epoch` with the UTC time and `zone` with the time zone information. An incomplete one has to contain the date at least - year, month and day.) The returned object is supposed to be passed to other functions, which use it to convert dates. It is not supposed to be inspected outside of this library. diff --git a/src/convert/utc-date.js b/src/convert/utc-date.js index 98cfc08..1fa65b1 100644 --- a/src/convert/utc-date.js +++ b/src/convert/utc-date.js @@ -1,8 +1,8 @@ -function getUnixTimeFromUTC ({ year, month, day, hours, minutes, seconds = 0, milliseconds = 0 }) { +function getUnixTimeFromUTC ({ year, month, day, hours = 0, minutes = 0, seconds = 0, milliseconds = 0 }) { return Date.UTC(year, month - 1, day, hours, minutes, seconds, milliseconds) } -function getDateFromTime ({ year, month, day, hours, minutes, seconds = 0, milliseconds = 0 }) { +function getDateFromTime ({ year, month, day, hours = 0, minutes = 0, seconds = 0, milliseconds = 0 }) { return new Date(year, month - 1, day, hours, minutes, seconds, milliseconds) } diff --git a/test/convertTimeToDate.test.js b/test/convertTimeToDate.test.js index 80794f7..a074943 100644 --- a/test/convertTimeToDate.test.js +++ b/test/convertTimeToDate.test.js @@ -57,3 +57,14 @@ it('converts a time without epoch and zone to date', () => { const expectedDate = new Date(2018, 0, 2, 10, 30) expect(actualDate).toEqual(expectedDate) }) + +it('if time (hours, minutes and seconds) is not provided, defaults to midnight', () => { + const time = { + year: 2018, + month: 1, + day: 2 + } + const actualDate = convertTimeToDate(time) + const expectedDate = new Date(2018, 0, 2, 0, 0) + expect(actualDate).toEqual(expectedDate) +}) diff --git a/test/getUnixTime.test.js b/test/getUnixTime.test.js index a99f761..9b3b4cf 100644 --- a/test/getUnixTime.test.js +++ b/test/getUnixTime.test.js @@ -140,3 +140,19 @@ it('checks, that other time zone is not requested if epoch is included', () => { } expect(() => getUnixTime(berlinTime, berlin)).toThrow() }) + +it('if time (hours, minutes and seconds) is not provided, defaults to midnight', () => { + const berlinTime = { + year: 2018, + month: 7, + day: 2, + zone: { + abbreviation: 'CEST', + offset: -120 + } + } + const unixTime = getUnixTime(berlinTime) + expect(typeof unixTime === 'number').toBeTruthy() + const epoch = Date.UTC(2018, 6, 1, 22, 0) + expect(unixTime).toEqual(epoch) +})