Skip to content

Commit 1b448f5

Browse files
authored
Replace unclear fromStandardDate() (#1290)
fromStandardDate() has been deprecated and split into clarified functions fromStandardDateLocal() and fromStandardDateUTC()
1 parent 40e166b commit 1b448f5

File tree

3 files changed

+122
-22
lines changed

3 files changed

+122
-22
lines changed

packages/core/src/temporal-types.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,35 @@ export class Date<T extends NumberOrInteger = Integer> {
352352

353353
/**
354354
* Create a {@link Date} object from the given standard JavaScript `Date`.
355-
* Hour, minute, second, millisecond and time zone offset components of the given date are ignored.
355+
* Hour, minute, second and millisecond components of the given date are ignored.
356356
*
357-
* NOTE: the function {@link toStandardDate} and {@link fromStandardDate} are not inverses of one another. {@link fromStandardDate} takes the Day, Month and Year in local time from the supplies JavaScript Date object, while {@link toStandardDate} creates a new JavaScript Date object at midnight UTC. This incongruity will be rectified in 6.0
357+
* NOTE: the function {@link toStandardDate} and {@link fromStandardDate} are not inverses of one another. {@link fromStandardDate} takes the Day, Month and Year in local time from the supplied JavaScript Date object, while {@link toStandardDate} creates a new JavaScript Date object at midnight UTC. This incongruity will be rectified in 6.0
358358
* If your timezone has a negative offset from UTC, creating a JavaScript Date at midnight UTC and converting it with {@link fromStandardDate} will result in a Date for the day before.
359359
*
360360
* @param {global.Date} standardDate - The standard JavaScript date to convert.
361361
* @return {Date} New Date.
362+
* @deprecated use {@link fromStandardDateLocal} which is a drop in replacement, or {@link fromStandardDateUTC} which takes the Year, Month and Date from UTC rather than Local time
362363
*/
363364
static fromStandardDate (standardDate: StandardDate): Date<number> {
365+
return this.fromStandardDateLocal(standardDate)
366+
}
367+
368+
/**
369+
* Create a {@link Date} object from the given standard JavaScript `Date` using the Year, Month and Date in Local Time.
370+
* Hour, minute, second and millisecond components of the given date are ignored.
371+
*
372+
* NOTE: this function and {@link toStandardDate} are not inverses of one another.
373+
* This takes the Day, Month and Year in local time from the supplied JavaScript Date object, while {@link toStandardDate} creates a new JavaScript Date object at midnight UTC.
374+
* For a more global approach, use {@link fromStandardDateUTC}, which reads the date in UTC time.
375+
*
376+
* @example
377+
* fromStandardDateLocal(new Date("2010-10-10T00:00:00")) // Will create a date at 2010-10-10 as JS Dates are created at local time by default
378+
* fromStandardDateLocal(new Date("2010-10-10T00:00:00Z")) // This may cause issues as this date is created at UTC with the trailing "Z"
379+
*
380+
* @param {global.Date} standardDate - The standard JavaScript date to convert.
381+
* @return {Date} New Date.
382+
*/
383+
static fromStandardDateLocal (standardDate: StandardDate): Date<number> {
364384
verifyStandardDateAndNanos(standardDate)
365385

366386
return new Date(
@@ -370,14 +390,33 @@ export class Date<T extends NumberOrInteger = Integer> {
370390
)
371391
}
372392

393+
/**
394+
* Create a {@link Date} object from the given standard JavaScript `Date` using the Year, Month and Date in UTC time.
395+
* Hour, minute, second and millisecond components of the given date are ignored.
396+
*
397+
* @example
398+
* fromStandardDateUTC(new Date("2010-10-10T00:00:00")) // This may cause issues as JS Dates are created at local time by default
399+
* fromStandardDateUTC(new Date("2010-10-10T00:00:00Z")) // Will create a date at 2010-10-10 as this date is created at UTC with the trailing "Z"
400+
*
401+
* @param {global.Date} standardDate - The standard JavaScript date to convert.
402+
* @return {Date} New Date.
403+
*/
404+
static fromStandardDateUTC (standardDate: StandardDate): Date<number> {
405+
verifyStandardDateAndNanos(standardDate)
406+
407+
return new Date(
408+
standardDate.getUTCFullYear(),
409+
standardDate.getUTCMonth() + 1,
410+
standardDate.getUTCDate()
411+
)
412+
}
413+
373414
/**
374415
* Convert date to standard JavaScript `Date`.
375416
*
376417
* The time component of the returned `Date` is set to midnight
377418
* and the time zone is set to UTC.
378419
*
379-
* NOTE: the function {@link toStandardDate} and {@link fromStandardDate} are not inverses of one another. {@link fromStandardDate} takes the Day, Month and Year in local time from the supplies JavaScript Date object, while {@link toStandardDate} creates a new JavaScript Date object at midnight UTC. This incongruity will be rectified in 6.0
380-
*
381420
* @returns {StandardDate} Standard JavaScript `Date` at `00:00:00.000` UTC.
382421
*/
383422
toStandardDate (): StandardDate {

packages/core/test/temporal-types.test.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,19 @@ const MAX_UTC_IN_MS = 8_640_000_000_000_000
2424
const ONE_DAY_IN_MS = 86_400_000
2525

2626
describe('Date', () => {
27-
describe('.toStandardDate()', () => {
28-
it('should convert to a standard date', () => {
29-
const localDatetime = new Date(2020, 3, 2)
30-
31-
const standardDate = localDatetime.toStandardDate()
32-
33-
expect(standardDate.getUTCFullYear()).toEqual(localDatetime.year)
34-
expect(standardDate.getUTCMonth()).toEqual(localDatetime.month - 1)
35-
expect(standardDate.getUTCDate()).toEqual(localDatetime.day)
36-
})
37-
38-
it('should be the reverse operation of fromStandardDate but losing time information', () => {
27+
describe('.fromStandardDateLocal()', () => {
28+
it('should create a date from the zoned date on a JS Date.', () => {
3929
fc.assert(
4030
fc.property(
4131
fc.date({
4232
max: temporalUtil.newDate(MAX_UTC_IN_MS - ONE_DAY_IN_MS),
4333
min: temporalUtil.newDate(MIN_UTC_IN_MS + ONE_DAY_IN_MS)
4434
}),
4535
standardDate => {
46-
const date = Date.fromStandardDate(standardDate)
36+
const date = Date.fromStandardDateLocal(standardDate)
4737
const receivedDate = date.toStandardDate()
4838

49-
expect(receivedDate.getUTCFullYear()).toEqual(standardDate.getFullYear()) // Date converts from local time but to UTC
39+
expect(receivedDate.getUTCFullYear()).toEqual(standardDate.getFullYear())
5040
expect(receivedDate.getUTCMonth()).toEqual(standardDate.getMonth())
5141
expect(receivedDate.getUTCDate()).toEqual(standardDate.getDate())
5242
expect(receivedDate.getUTCHours()).toEqual(0)
@@ -55,6 +45,38 @@ describe('Date', () => {
5545
)
5646
})
5747
})
48+
describe('.fromStandardDateUTC()', () => {
49+
it('should be the reverse operation of toStandardDateUTC but losing time information', () => {
50+
fc.assert(
51+
fc.property(
52+
fc.date({
53+
max: temporalUtil.newDate(MAX_UTC_IN_MS - ONE_DAY_IN_MS),
54+
min: temporalUtil.newDate(MIN_UTC_IN_MS + ONE_DAY_IN_MS)
55+
}),
56+
standardDate => {
57+
const date = Date.fromStandardDateUTC(standardDate)
58+
const receivedDate = date.toStandardDate()
59+
60+
expect(receivedDate.getUTCFullYear()).toEqual(standardDate.getUTCFullYear())
61+
expect(receivedDate.getUTCMonth()).toEqual(standardDate.getUTCMonth())
62+
expect(receivedDate.getUTCDate()).toEqual(standardDate.getUTCDate())
63+
expect(receivedDate.getUTCHours()).toEqual(0)
64+
expect(receivedDate.getUTCMinutes()).toEqual(0)
65+
})
66+
)
67+
})
68+
})
69+
describe('.toStandardDate()', () => {
70+
it('should convert to a standard date', () => {
71+
const localDatetime = new Date(2020, 3, 2)
72+
73+
const standardDate = localDatetime.toStandardDate()
74+
75+
expect(standardDate.getUTCFullYear()).toEqual(localDatetime.year)
76+
expect(standardDate.getUTCMonth()).toEqual(localDatetime.month - 1)
77+
expect(standardDate.getUTCDate()).toEqual(localDatetime.day)
78+
})
79+
})
5880
})
5981

6082
describe('LocalDateTime', () => {

packages/neo4j-driver-deno/lib/core/temporal-types.ts

Lines changed: 43 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)