File size: 16,093 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {add, addTime, addZoned, constrain, constrainTime, cycleDate, cycleTime, cycleZoned, set, setTime, setZoned, subtract, subtractTime, subtractZoned} from './manipulation';
import {AnyCalendarDate, AnyTime, Calendar, CycleOptions, CycleTimeOptions, DateDuration, DateField, DateFields, DateTimeDuration, Disambiguation, TimeDuration, TimeField, TimeFields} from './types';
import {compareDate, compareTime} from './queries';
import {dateTimeToString, dateToString, timeToString, zonedDateTimeToString} from './string';
import {GregorianCalendar} from './calendars/GregorianCalendar';
import {toCalendarDateTime, toDate, toZoned, zonedToDate} from './conversion';
function shiftArgs(args: any[]) {
let calendar: Calendar = typeof args[0] === 'object'
? args.shift()
: new GregorianCalendar();
let era: string;
if (typeof args[0] === 'string') {
era = args.shift();
} else {
let eras = calendar.getEras();
era = eras[eras.length - 1];
}
let year = args.shift();
let month = args.shift();
let day = args.shift();
return [calendar, era, year, month, day];
}
/** A CalendarDate represents a date without any time components in a specific calendar system. */
export class CalendarDate {
// This prevents TypeScript from allowing other types with the same fields to match.
// i.e. a ZonedDateTime should not be be passable to a parameter that expects CalendarDate.
// If that behavior is desired, use the AnyCalendarDate interface instead.
// @ts-ignore
#type;
/** The calendar system associated with this date, e.g. Gregorian. */
public readonly calendar: Calendar;
/** The calendar era for this date, e.g. "BC" or "AD". */
public readonly era: string;
/** The year of this date within the era. */
public readonly year: number;
/**
* The month number within the year. Note that some calendar systems such as Hebrew
* may have a variable number of months per year. Therefore, month numbers may not
* always correspond to the same month names in different years.
*/
public readonly month: number;
/** The day number within the month. */
public readonly day: number;
constructor(year: number, month: number, day: number);
constructor(era: string, year: number, month: number, day: number);
constructor(calendar: Calendar, year: number, month: number, day: number);
constructor(calendar: Calendar, era: string, year: number, month: number, day: number);
constructor(...args: any[]) {
let [calendar, era, year, month, day] = shiftArgs(args);
this.calendar = calendar;
this.era = era;
this.year = year;
this.month = month;
this.day = day;
constrain(this);
}
/** Returns a copy of this date. */
copy(): CalendarDate {
if (this.era) {
return new CalendarDate(this.calendar, this.era, this.year, this.month, this.day);
} else {
return new CalendarDate(this.calendar, this.year, this.month, this.day);
}
}
/** Returns a new `CalendarDate` with the given duration added to it. */
add(duration: DateDuration): CalendarDate {
return add(this, duration);
}
/** Returns a new `CalendarDate` with the given duration subtracted from it. */
subtract(duration: DateDuration): CalendarDate {
return subtract(this, duration);
}
/** Returns a new `CalendarDate` with the given fields set to the provided values. Other fields will be constrained accordingly. */
set(fields: DateFields): CalendarDate {
return set(this, fields);
}
/**
* Returns a new `CalendarDate` with the given field adjusted by a specified amount.
* When the resulting value reaches the limits of the field, it wraps around.
*/
cycle(field: DateField, amount: number, options?: CycleOptions): CalendarDate {
return cycleDate(this, field, amount, options);
}
/** Converts the date to a native JavaScript Date object, with the time set to midnight in the given time zone. */
toDate(timeZone: string): Date {
return toDate(this, timeZone);
}
/** Converts the date to an ISO 8601 formatted string. */
toString(): string {
return dateToString(this);
}
/** Compares this date with another. A negative result indicates that this date is before the given one, and a positive date indicates that it is after. */
compare(b: AnyCalendarDate): number {
return compareDate(this, b);
}
}
/** A Time represents a clock time without any date components. */
export class Time {
// This prevents TypeScript from allowing other types with the same fields to match.
// @ts-ignore
#type;
/** The hour, numbered from 0 to 23. */
public readonly hour: number;
/** The minute in the hour. */
public readonly minute: number;
/** The second in the minute. */
public readonly second: number;
/** The millisecond in the second. */
public readonly millisecond: number;
constructor(
hour: number = 0,
minute: number = 0,
second: number = 0,
millisecond: number = 0
) {
this.hour = hour;
this.minute = minute;
this.second = second;
this.millisecond = millisecond;
constrainTime(this);
}
/** Returns a copy of this time. */
copy(): Time {
return new Time(this.hour, this.minute, this.second, this.millisecond);
}
/** Returns a new `Time` with the given duration added to it. */
add(duration: TimeDuration) {
return addTime(this, duration);
}
/** Returns a new `Time` with the given duration subtracted from it. */
subtract(duration: TimeDuration) {
return subtractTime(this, duration);
}
/** Returns a new `Time` with the given fields set to the provided values. Other fields will be constrained accordingly. */
set(fields: TimeFields) {
return setTime(this, fields);
}
/**
* Returns a new `Time` with the given field adjusted by a specified amount.
* When the resulting value reaches the limits of the field, it wraps around.
*/
cycle(field: TimeField, amount: number, options?: CycleTimeOptions) {
return cycleTime(this, field, amount, options);
}
/** Converts the time to an ISO 8601 formatted string. */
toString() {
return timeToString(this);
}
/** Compares this time with another. A negative result indicates that this time is before the given one, and a positive time indicates that it is after. */
compare(b: AnyTime) {
return compareTime(this, b);
}
}
/** A CalendarDateTime represents a date and time without a time zone, in a specific calendar system. */
export class CalendarDateTime {
// This prevents TypeScript from allowing other types with the same fields to match.
// @ts-ignore
#type;
/** The calendar system associated with this date, e.g. Gregorian. */
public readonly calendar: Calendar;
/** The calendar era for this date, e.g. "BC" or "AD". */
public readonly era: string;
/** The year of this date within the era. */
public readonly year: number;
/**
* The month number within the year. Note that some calendar systems such as Hebrew
* may have a variable number of months per year. Therefore, month numbers may not
* always correspond to the same month names in different years.
*/
public readonly month: number;
/** The day number within the month. */
public readonly day: number;
/** The hour in the day, numbered from 0 to 23. */
public readonly hour: number;
/** The minute in the hour. */
public readonly minute: number;
/** The second in the minute. */
public readonly second: number;
/** The millisecond in the second. */
public readonly millisecond: number;
constructor(year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number);
constructor(era: string, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number);
constructor(calendar: Calendar, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number);
constructor(calendar: Calendar, era: string, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number);
constructor(...args: any[]) {
let [calendar, era, year, month, day] = shiftArgs(args);
this.calendar = calendar;
this.era = era;
this.year = year;
this.month = month;
this.day = day;
this.hour = args.shift() || 0;
this.minute = args.shift() || 0;
this.second = args.shift() || 0;
this.millisecond = args.shift() || 0;
constrain(this);
}
/** Returns a copy of this date. */
copy(): CalendarDateTime {
if (this.era) {
return new CalendarDateTime(this.calendar, this.era, this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond);
} else {
return new CalendarDateTime(this.calendar, this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond);
}
}
/** Returns a new `CalendarDateTime` with the given duration added to it. */
add(duration: DateTimeDuration): CalendarDateTime {
return add(this, duration);
}
/** Returns a new `CalendarDateTime` with the given duration subtracted from it. */
subtract(duration: DateTimeDuration): CalendarDateTime {
return subtract(this, duration);
}
/** Returns a new `CalendarDateTime` with the given fields set to the provided values. Other fields will be constrained accordingly. */
set(fields: DateFields & TimeFields): CalendarDateTime {
return set(setTime(this, fields), fields);
}
/**
* Returns a new `CalendarDateTime` with the given field adjusted by a specified amount.
* When the resulting value reaches the limits of the field, it wraps around.
*/
cycle(field: DateField | TimeField, amount: number, options?: CycleTimeOptions): CalendarDateTime {
switch (field) {
case 'era':
case 'year':
case 'month':
case 'day':
return cycleDate(this, field, amount, options);
default:
return cycleTime(this, field, amount, options);
}
}
/** Converts the date to a native JavaScript Date object in the given time zone. */
toDate(timeZone: string, disambiguation?: Disambiguation): Date {
return toDate(this, timeZone, disambiguation);
}
/** Converts the date to an ISO 8601 formatted string. */
toString(): string {
return dateTimeToString(this);
}
/** Compares this date with another. A negative result indicates that this date is before the given one, and a positive date indicates that it is after. */
compare(b: CalendarDate | CalendarDateTime | ZonedDateTime): number {
let res = compareDate(this, b);
if (res === 0) {
return compareTime(this, toCalendarDateTime(b));
}
return res;
}
}
/** A ZonedDateTime represents a date and time in a specific time zone and calendar system. */
export class ZonedDateTime {
// This prevents TypeScript from allowing other types with the same fields to match.
// @ts-ignore
#type;
/** The calendar system associated with this date, e.g. Gregorian. */
public readonly calendar: Calendar;
/** The calendar era for this date, e.g. "BC" or "AD". */
public readonly era: string;
/** The year of this date within the era. */
public readonly year: number;
/**
* The month number within the year. Note that some calendar systems such as Hebrew
* may have a variable number of months per year. Therefore, month numbers may not
* always correspond to the same month names in different years.
*/
public readonly month: number;
/** The day number within the month. */
public readonly day: number;
/** The hour in the day, numbered from 0 to 23. */
public readonly hour: number;
/** The minute in the hour. */
public readonly minute: number;
/** The second in the minute. */
public readonly second: number;
/** The millisecond in the second. */
public readonly millisecond: number;
/** The IANA time zone identifier that this date and time is represented in. */
public readonly timeZone: string;
/** The UTC offset for this time, in milliseconds. */
public readonly offset: number;
constructor(year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number);
constructor(era: string, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number);
constructor(calendar: Calendar, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number);
constructor(calendar: Calendar, era: string, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number);
constructor(...args: any[]) {
let [calendar, era, year, month, day] = shiftArgs(args);
let timeZone = args.shift();
let offset = args.shift();
this.calendar = calendar;
this.era = era;
this.year = year;
this.month = month;
this.day = day;
this.timeZone = timeZone;
this.offset = offset;
this.hour = args.shift() || 0;
this.minute = args.shift() || 0;
this.second = args.shift() || 0;
this.millisecond = args.shift() || 0;
constrain(this);
}
/** Returns a copy of this date. */
copy(): ZonedDateTime {
if (this.era) {
return new ZonedDateTime(this.calendar, this.era, this.year, this.month, this.day, this.timeZone, this.offset, this.hour, this.minute, this.second, this.millisecond);
} else {
return new ZonedDateTime(this.calendar, this.year, this.month, this.day, this.timeZone, this.offset, this.hour, this.minute, this.second, this.millisecond);
}
}
/** Returns a new `ZonedDateTime` with the given duration added to it. */
add(duration: DateTimeDuration) {
return addZoned(this, duration);
}
/** Returns a new `ZonedDateTime` with the given duration subtracted from it. */
subtract(duration: DateTimeDuration) {
return subtractZoned(this, duration);
}
/** Returns a new `ZonedDateTime` with the given fields set to the provided values. Other fields will be constrained accordingly. */
set(fields: DateFields & TimeFields, disambiguation?: Disambiguation) {
return setZoned(this, fields, disambiguation);
}
/**
* Returns a new `ZonedDateTime` with the given field adjusted by a specified amount.
* When the resulting value reaches the limits of the field, it wraps around.
*/
cycle(field: DateField | TimeField, amount: number, options?: CycleTimeOptions) {
return cycleZoned(this, field, amount, options);
}
/** Converts the date to a native JavaScript Date object. */
toDate() {
return zonedToDate(this);
}
/** Converts the date to an ISO 8601 formatted string, including the UTC offset and time zone identifier. */
toString() {
return zonedDateTimeToString(this);
}
/** Converts the date to an ISO 8601 formatted string in UTC. */
toAbsoluteString() {
return this.toDate().toISOString();
}
/** Compares this date with another. A negative result indicates that this date is before the given one, and a positive date indicates that it is after. */
compare(b: CalendarDate | CalendarDateTime | ZonedDateTime) {
// TODO: Is this a bad idea??
return this.toDate().getTime() - toZoned(b, this.timeZone).toDate().getTime();
}
}
|