-
Notifications
You must be signed in to change notification settings - Fork 151
Allow to create temporal objects from standard JS Date #390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
*/ | ||
|
||
import * as util from './internal/temporal-util'; | ||
import {assertNumberOrInteger, assertString} from './internal/util'; | ||
import {assertNumberOrInteger, assertString, assertValidDate} from './internal/util'; | ||
import {newError} from './error'; | ||
|
||
const IDENTIFIER_PROPERTY_ATTRIBUTES = { | ||
|
@@ -94,6 +94,23 @@ export class LocalTime { | |
Object.freeze(this); | ||
} | ||
|
||
/** | ||
* Create a local time object from the given standard JavaScript <code>Date</code> and optional nanoseconds. | ||
* Year, month, day and time zone offset components of the given date are ignored. | ||
* @param {global.Date} standardDate the standard JavaScript date to convert. | ||
* @param {Integer|number|undefined} nanosecond the optional amount of nanoseconds. | ||
* @return {LocalTime} new local time. | ||
*/ | ||
static fromStandardDate(standardDate, nanosecond) { | ||
verifyStandardDateAndNanos(standardDate, nanosecond); | ||
|
||
return new LocalTime( | ||
standardDate.getHours(), | ||
standardDate.getMinutes(), | ||
standardDate.getSeconds(), | ||
util.totalNanoseconds(standardDate, nanosecond)); | ||
} | ||
|
||
toString() { | ||
return util.timeToIsoString(this.hour, this.minute, this.second, this.nanosecond); | ||
} | ||
|
@@ -133,6 +150,24 @@ export class Time { | |
Object.freeze(this); | ||
} | ||
|
||
/** | ||
* Create a time object from the given standard JavaScript <code>Date</code> and optional nanoseconds. | ||
* Year, month and day components of the given date are ignored. | ||
* @param {global.Date} standardDate the standard JavaScript date to convert. | ||
* @param {Integer|number|undefined} nanosecond the optional amount of nanoseconds. | ||
* @return {Time} new time. | ||
*/ | ||
static fromStandardDate(standardDate, nanosecond) { | ||
verifyStandardDateAndNanos(standardDate, nanosecond); | ||
|
||
return new Time( | ||
standardDate.getHours(), | ||
standardDate.getMinutes(), | ||
standardDate.getSeconds(), | ||
util.totalNanoseconds(standardDate, nanosecond), | ||
util.timeZoneOffsetInSeconds(standardDate)); | ||
} | ||
|
||
toString() { | ||
return util.timeToIsoString(this.hour, this.minute, this.second, this.nanosecond) + util.timeZoneOffsetToIsoString(this.timeZoneOffsetSeconds); | ||
} | ||
|
@@ -168,6 +203,21 @@ export class Date { | |
Object.freeze(this); | ||
} | ||
|
||
/** | ||
* Create a date object from the given standard JavaScript <code>Date</code>. | ||
* Hour, minute, second, millisecond and time zone offset components of the given date are ignored. | ||
* @param {global.Date} standardDate the standard JavaScript date to convert. | ||
* @return {Date} new date. | ||
*/ | ||
static fromStandardDate(standardDate) { | ||
verifyStandardDateAndNanos(standardDate, null); | ||
|
||
return new Date( | ||
standardDate.getFullYear(), | ||
standardDate.getMonth(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's a problem about conversion of Maybe add value range checks on temporal type constructors? |
||
standardDate.getDate()); | ||
} | ||
|
||
toString() { | ||
return util.dateToIsoString(this.year, this.month, this.day); | ||
} | ||
|
@@ -211,6 +261,26 @@ export class LocalDateTime { | |
Object.freeze(this); | ||
} | ||
|
||
/** | ||
* Create a local date-time object from the given standard JavaScript <code>Date</code> and optional nanoseconds. | ||
* Time zone offset component of the given date is ignored. | ||
* @param {global.Date} standardDate the standard JavaScript date to convert. | ||
* @param {Integer|number|undefined} nanosecond the optional amount of nanoseconds. | ||
* @return {LocalDateTime} new local date-time. | ||
*/ | ||
static fromStandardDate(standardDate, nanosecond) { | ||
verifyStandardDateAndNanos(standardDate, nanosecond); | ||
|
||
return new LocalDateTime( | ||
standardDate.getFullYear(), | ||
standardDate.getMonth(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
standardDate.getDate(), | ||
standardDate.getHours(), | ||
standardDate.getMinutes(), | ||
standardDate.getSeconds(), | ||
util.totalNanoseconds(standardDate, nanosecond)); | ||
} | ||
|
||
toString() { | ||
return localDateTimeToString(this.year, this.month, this.day, this.hour, this.minute, this.second, this.nanosecond); | ||
} | ||
|
@@ -261,6 +331,27 @@ export class DateTime { | |
Object.freeze(this); | ||
} | ||
|
||
/** | ||
* Create a date-time object from the given standard JavaScript <code>Date</code> and optional nanoseconds. | ||
* @param {global.Date} standardDate the standard JavaScript date to convert. | ||
* @param {Integer|number|undefined} nanosecond the optional amount of nanoseconds. | ||
* @return {DateTime} new date-time. | ||
*/ | ||
static fromStandardDate(standardDate, nanosecond) { | ||
verifyStandardDateAndNanos(standardDate, nanosecond); | ||
|
||
return new DateTime( | ||
standardDate.getFullYear(), | ||
standardDate.getMonth(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
standardDate.getDate(), | ||
standardDate.getHours(), | ||
standardDate.getMinutes(), | ||
standardDate.getSeconds(), | ||
util.totalNanoseconds(standardDate, nanosecond), | ||
util.timeZoneOffsetInSeconds(standardDate), | ||
null /* no time zone id */); | ||
} | ||
|
||
toString() { | ||
const localDateTimeStr = localDateTimeToString(this.year, this.month, this.day, this.hour, this.minute, this.second, this.nanosecond); | ||
const timeZoneStr = this.timeZoneId ? `[${this.timeZoneId}]` : util.timeZoneOffsetToIsoString(this.timeZoneOffsetSeconds); | ||
|
@@ -303,3 +394,10 @@ function verifyTimeZoneArguments(timeZoneOffsetSeconds, timeZoneId) { | |
throw newError(`Unable to create DateTime without either time zone offset or id. Please specify either of them. Given offset: ${timeZoneOffsetSeconds} and id: ${timeZoneId}`); | ||
} | ||
} | ||
|
||
function verifyStandardDateAndNanos(standardDate, nanosecond) { | ||
assertValidDate(standardDate, 'Standard date'); | ||
if (nanosecond !== null && nanosecond !== undefined) { | ||
assertNumberOrInteger(nanosecond, 'Nanosecond'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the comment is outdated.