Skip to content

Commit

Permalink
fix: Input time object for getUnixTime and convertTimeToDate does not…
Browse files Browse the repository at this point in the history
… need to include hours and minutes - defaults to midnight
  • Loading branch information
prantlf committed Jun 10, 2019
1 parent 000e6f9 commit e45a193
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions src/convert/utc-date.js
Original file line number Diff line number Diff line change
@@ -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)
}

Expand Down
11 changes: 11 additions & 0 deletions test/convertTimeToDate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
16 changes: 16 additions & 0 deletions test/getUnixTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit e45a193

Please sign in to comment.