|
18 | 18 | */
|
19 | 19 |
|
20 | 20 | import * as util from './internal/temporal-util';
|
21 |
| -import {assertNumberOrInteger, assertString} from './internal/util'; |
| 21 | +import {assertNumberOrInteger, assertString, assertValidDate} from './internal/util'; |
22 | 22 | import {newError} from './error';
|
23 | 23 |
|
24 | 24 | const IDENTIFIER_PROPERTY_ATTRIBUTES = {
|
@@ -94,6 +94,23 @@ export class LocalTime {
|
94 | 94 | Object.freeze(this);
|
95 | 95 | }
|
96 | 96 |
|
| 97 | + /** |
| 98 | + * Create a local time object from the given standard JavaScript <code>Date</code> and optional nanoseconds. |
| 99 | + * Year, month, day and time zone offset components of the given date are ignored. |
| 100 | + * @param {global.Date} standardDate the standard JavaScript date to convert. |
| 101 | + * @param {Integer|number|undefined} nanosecond the optional amount of nanoseconds. |
| 102 | + * @return {LocalTime} new local time. |
| 103 | + */ |
| 104 | + static fromStandardDate(standardDate, nanosecond) { |
| 105 | + verifyStandardDateAndNanos(standardDate, nanosecond); |
| 106 | + |
| 107 | + return new LocalTime( |
| 108 | + standardDate.getHours(), |
| 109 | + standardDate.getMinutes(), |
| 110 | + standardDate.getSeconds(), |
| 111 | + util.totalNanoseconds(standardDate, nanosecond)); |
| 112 | + } |
| 113 | + |
97 | 114 | toString() {
|
98 | 115 | return util.timeToIsoString(this.hour, this.minute, this.second, this.nanosecond);
|
99 | 116 | }
|
@@ -133,6 +150,24 @@ export class Time {
|
133 | 150 | Object.freeze(this);
|
134 | 151 | }
|
135 | 152 |
|
| 153 | + /** |
| 154 | + * Create a time object from the given standard JavaScript <code>Date</code> and optional nanoseconds. |
| 155 | + * Year, month and day components of the given date are ignored. |
| 156 | + * @param {global.Date} standardDate the standard JavaScript date to convert. |
| 157 | + * @param {Integer|number|undefined} nanosecond the optional amount of nanoseconds. |
| 158 | + * @return {Time} new time. |
| 159 | + */ |
| 160 | + static fromStandardDate(standardDate, nanosecond) { |
| 161 | + verifyStandardDateAndNanos(standardDate, nanosecond); |
| 162 | + |
| 163 | + return new Time( |
| 164 | + standardDate.getHours(), |
| 165 | + standardDate.getMinutes(), |
| 166 | + standardDate.getSeconds(), |
| 167 | + util.totalNanoseconds(standardDate, nanosecond), |
| 168 | + util.timeZoneOffsetInSeconds(standardDate)); |
| 169 | + } |
| 170 | + |
136 | 171 | toString() {
|
137 | 172 | return util.timeToIsoString(this.hour, this.minute, this.second, this.nanosecond) + util.timeZoneOffsetToIsoString(this.timeZoneOffsetSeconds);
|
138 | 173 | }
|
@@ -168,6 +203,21 @@ export class Date {
|
168 | 203 | Object.freeze(this);
|
169 | 204 | }
|
170 | 205 |
|
| 206 | + /** |
| 207 | + * Create a date object from the given standard JavaScript <code>Date</code>. |
| 208 | + * Hour, minute, second, millisecond and time zone offset components of the given date are ignored. |
| 209 | + * @param {global.Date} standardDate the standard JavaScript date to convert. |
| 210 | + * @return {Date} new date. |
| 211 | + */ |
| 212 | + static fromStandardDate(standardDate) { |
| 213 | + verifyStandardDateAndNanos(standardDate, null); |
| 214 | + |
| 215 | + return new Date( |
| 216 | + standardDate.getFullYear(), |
| 217 | + standardDate.getMonth(), |
| 218 | + standardDate.getDate()); |
| 219 | + } |
| 220 | + |
171 | 221 | toString() {
|
172 | 222 | return util.dateToIsoString(this.year, this.month, this.day);
|
173 | 223 | }
|
@@ -211,6 +261,26 @@ export class LocalDateTime {
|
211 | 261 | Object.freeze(this);
|
212 | 262 | }
|
213 | 263 |
|
| 264 | + /** |
| 265 | + * Create a local date-time object from the given standard JavaScript <code>Date</code> and optional nanoseconds. |
| 266 | + * Time zone offset component of the given date is ignored. |
| 267 | + * @param {global.Date} standardDate the standard JavaScript date to convert. |
| 268 | + * @param {Integer|number|undefined} nanosecond the optional amount of nanoseconds. |
| 269 | + * @return {LocalDateTime} new local date-time. |
| 270 | + */ |
| 271 | + static fromStandardDate(standardDate, nanosecond) { |
| 272 | + verifyStandardDateAndNanos(standardDate, nanosecond); |
| 273 | + |
| 274 | + return new LocalDateTime( |
| 275 | + standardDate.getFullYear(), |
| 276 | + standardDate.getMonth(), |
| 277 | + standardDate.getDate(), |
| 278 | + standardDate.getHours(), |
| 279 | + standardDate.getMinutes(), |
| 280 | + standardDate.getSeconds(), |
| 281 | + util.totalNanoseconds(standardDate, nanosecond)); |
| 282 | + } |
| 283 | + |
214 | 284 | toString() {
|
215 | 285 | return localDateTimeToString(this.year, this.month, this.day, this.hour, this.minute, this.second, this.nanosecond);
|
216 | 286 | }
|
@@ -261,6 +331,27 @@ export class DateTime {
|
261 | 331 | Object.freeze(this);
|
262 | 332 | }
|
263 | 333 |
|
| 334 | + /** |
| 335 | + * Create a date-time object from the given standard JavaScript <code>Date</code> and optional nanoseconds. |
| 336 | + * @param {global.Date} standardDate the standard JavaScript date to convert. |
| 337 | + * @param {Integer|number|undefined} nanosecond the optional amount of nanoseconds. |
| 338 | + * @return {DateTime} new date-time. |
| 339 | + */ |
| 340 | + static fromStandardDate(standardDate, nanosecond) { |
| 341 | + verifyStandardDateAndNanos(standardDate, nanosecond); |
| 342 | + |
| 343 | + return new DateTime( |
| 344 | + standardDate.getFullYear(), |
| 345 | + standardDate.getMonth(), |
| 346 | + standardDate.getDate(), |
| 347 | + standardDate.getHours(), |
| 348 | + standardDate.getMinutes(), |
| 349 | + standardDate.getSeconds(), |
| 350 | + util.totalNanoseconds(standardDate, nanosecond), |
| 351 | + util.timeZoneOffsetInSeconds(standardDate), |
| 352 | + null /* no time zone id */); |
| 353 | + } |
| 354 | + |
264 | 355 | toString() {
|
265 | 356 | const localDateTimeStr = localDateTimeToString(this.year, this.month, this.day, this.hour, this.minute, this.second, this.nanosecond);
|
266 | 357 | const timeZoneStr = this.timeZoneId ? `[${this.timeZoneId}]` : util.timeZoneOffsetToIsoString(this.timeZoneOffsetSeconds);
|
@@ -303,3 +394,10 @@ function verifyTimeZoneArguments(timeZoneOffsetSeconds, timeZoneId) {
|
303 | 394 | throw newError(`Unable to create DateTime without either time zone offset or id. Please specify either of them. Given offset: ${timeZoneOffsetSeconds} and id: ${timeZoneId}`);
|
304 | 395 | }
|
305 | 396 | }
|
| 397 | + |
| 398 | +function verifyStandardDateAndNanos(standardDate, nanosecond) { |
| 399 | + assertValidDate(standardDate, 'Standard date'); |
| 400 | + if (nanosecond !== null && nanosecond !== undefined) { |
| 401 | + assertNumberOrInteger(nanosecond, 'Nanosecond'); |
| 402 | + } |
| 403 | +} |
0 commit comments