Skip to content

Commit e45a193

Browse files
committed
fix: Input time object for getUnixTime and convertTimeToDate does not need to include hours and minutes - defaults to midnight
1 parent 000e6f9 commit e45a193

4 files changed

Lines changed: 30 additions & 3 deletions

File tree

docs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ const date = getZonedTime(date, berlin)
216216
// Returns the UNIX timestamp (UTC) in milliseconds
217217
```
218218

219-
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.)
219+
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.)
220220

221221
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.
222222

src/convert/utc-date.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
function getUnixTimeFromUTC ({ year, month, day, hours, minutes, seconds = 0, milliseconds = 0 }) {
1+
function getUnixTimeFromUTC ({ year, month, day, hours = 0, minutes = 0, seconds = 0, milliseconds = 0 }) {
22
return Date.UTC(year, month - 1, day, hours, minutes, seconds, milliseconds)
33
}
44

5-
function getDateFromTime ({ year, month, day, hours, minutes, seconds = 0, milliseconds = 0 }) {
5+
function getDateFromTime ({ year, month, day, hours = 0, minutes = 0, seconds = 0, milliseconds = 0 }) {
66
return new Date(year, month - 1, day, hours, minutes, seconds, milliseconds)
77
}
88

test/convertTimeToDate.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,14 @@ it('converts a time without epoch and zone to date', () => {
5757
const expectedDate = new Date(2018, 0, 2, 10, 30)
5858
expect(actualDate).toEqual(expectedDate)
5959
})
60+
61+
it('if time (hours, minutes and seconds) is not provided, defaults to midnight', () => {
62+
const time = {
63+
year: 2018,
64+
month: 1,
65+
day: 2
66+
}
67+
const actualDate = convertTimeToDate(time)
68+
const expectedDate = new Date(2018, 0, 2, 0, 0)
69+
expect(actualDate).toEqual(expectedDate)
70+
})

test/getUnixTime.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,19 @@ it('checks, that other time zone is not requested if epoch is included', () => {
140140
}
141141
expect(() => getUnixTime(berlinTime, berlin)).toThrow()
142142
})
143+
144+
it('if time (hours, minutes and seconds) is not provided, defaults to midnight', () => {
145+
const berlinTime = {
146+
year: 2018,
147+
month: 7,
148+
day: 2,
149+
zone: {
150+
abbreviation: 'CEST',
151+
offset: -120
152+
}
153+
}
154+
const unixTime = getUnixTime(berlinTime)
155+
expect(typeof unixTime === 'number').toBeTruthy()
156+
const epoch = Date.UTC(2018, 6, 1, 22, 0)
157+
expect(unixTime).toEqual(epoch)
158+
})

0 commit comments

Comments
 (0)