Skip to content

Commit

Permalink
Implement default calendar option 1
Browse files Browse the repository at this point in the history
This implements "option 1" for the default calendar, which is defaulting
to the full ISO 8601 calendar everywhere. We add optional calendar
parameters to Temporal.Absolute.prototype.inTimeZone, Temporal.now.date,
Temporal.now.dateTime, and Temporal.TimeZone.prototype.getDateTimeFor.

See: #292.
  • Loading branch information
ptomato committed Jun 5, 2020
1 parent 5be4794 commit 8947101
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
8 changes: 6 additions & 2 deletions docs/absolute.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,20 @@ Same as `getEpochSeconds()`, but with nanosecond (10<sup>&minus;9</sup> second)

The value returned from this method is suitable to be passed to `new Temporal.Absolute()`.

### absolute.**inTimeZone**(_timeZone_: Temporal.TimeZone | string) : Temporal.DateTime
### absolute.**inTimeZone**(_timeZone_: Temporal.TimeZone | string, _calendar_?: Temporal.Calendar | string) : Temporal.DateTime

**Parameters:**
- `timeZone` (object or string): A `Temporal.TimeZone` object, or a string description of the time zone; either its IANA name or UTC offset.
- `calendar` (optional object or string): A `Temporal.Calendar` object, or a calendar identifier.
The default is to use the ISO 8601 calendar.

**Returns:** a `Temporal.DateTime` object indicating the calendar date and wall-clock time in `timeZone` at the absolute time indicated by `absolute`.
**Returns:** a `Temporal.DateTime` object indicating the calendar date and wall-clock time in `timeZone`, according to the reckoning of `calendar`, at the absolute time indicated by `absolute`.

For a list of IANA time zone names, see the current version of the [IANA time zone database](https://www.iana.org/time-zones).
A convenient list is also available [on Wikipedia](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones), although it might not reflect the latest official status.

For a list of calendar identifiers, see the documentation for [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#Parameters).

This method is one way to convert a `Temporal.Absolute` to a `Temporal.DateTime`.

Example usage:
Expand Down
10 changes: 7 additions & 3 deletions docs/now.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ nextTransition.inTimeZone(tz);
// On 2020-03-08T03:00 the clock will change from UTC -08:00 to -07:00
```

### Temporal.now.**dateTime**(_timeZone_: Temporal.TimeZone | string = Temporal.now.timeZone()) : Temporal.DateTime
### Temporal.now.**dateTime**(_timeZone_: Temporal.TimeZone | string = Temporal.now.timeZone(), _calendar_: Temporal.Calendar | string = 'iso8601') : Temporal.DateTime

**Parameters:**
- `timeZone` (optional `Temporal.TimeZone` or string): The time zone to get the current date and time in.
If not given, the current system time zone will be used.
- `calendar` (optional `Temporal.Calendar` or string): The calendar system to get the current date and time in.
If not given, the ISO 8601 calendar will be used.

**Returns:** a `Temporal.DateTime` object representing the current system date and time.

This method gets the current calendar date and wall-clock time according to the system settings.
Optionally a time zone can be given in which the time is computed.
Optionally a time zone can be given in which the time is computed, and a calendar system in which the date is reckoned.

Example usage:
```js
Expand All @@ -83,11 +85,13 @@ Object.entries(financialCentres).forEach(([name, timeZone]) => {
// Tokyo: 2020-01-25T14:52:14.759534758
```

### Temporal.now.**date**(_timeZone_: Temporal.TimeZone | string = Temporal.now.timeZone()) : Temporal.Date
### Temporal.now.**date**(_timeZone_: Temporal.TimeZone | string = Temporal.now.timeZone(), _calendar_: Temporal.Calendar | string = 'iso8601') : Temporal.Date

**Parameters:**
- `timeZone` (optional `Temporal.TimeZone` or string): The time zone to get the current date and time in.
If not given, the current system time zone will be used.
- `calendar` (optional `Temporal.Calendar` or string): The calendar system to get the current date and time in.
If not given, the ISO 8601 calendar will be used.

**Returns:** a `Temporal.Date` object representing the current system date.

Expand Down
6 changes: 4 additions & 2 deletions docs/timezone.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ tz = new Temporal.TimeZone('-08:00');
tz.getOffsetStringFor(timestamp); // => -08:00
```

### timeZone.**getDateTimeFor**(_absolute_: Temporal.Absolute) : Temporal.DateTime
### timeZone.**getDateTimeFor**(_absolute_: Temporal.Absolute, _calendar_?: Temporal.Calendar | string) : Temporal.DateTime

**Parameters:**
- `absolute` (`Temporal.Absolute`): An absolute time to convert.
- `calendar` (optional object or string): A `Temporal.Calendar` object, or a calendar identifier.
The default is to use the ISO 8601 calendar.

**Returns:** A `Temporal.DateTime` object indicating the calendar date and wall-clock time in `timeZone` at the absolute time indicated by `absolute`.
**Returns:** A `Temporal.DateTime` object indicating the calendar date and wall-clock time in `timeZone`, according to the reckoning of `calendar`, at the absolute time indicated by `absolute`.

This method is one way to convert a `Temporal.Absolute` to a `Temporal.DateTime`.

Expand Down
15 changes: 11 additions & 4 deletions polyfill/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export namespace Temporal {
other: Temporal.Absolute,
options?: DifferenceOptions<'days' | 'hours' | 'minutes' | 'seconds'>
): Temporal.Duration;
inTimeZone(tzLike?: Temporal.TimeZone | string): Temporal.DateTime;
inTimeZone(tzLike?: Temporal.TimeZone | string, calendar?: Temporal.Calendar | string): Temporal.DateTime;
toLocaleString(locales?: string | string[], options?: Intl.DateTimeFormatOptions): string;
toJSON(): string;
toString(tzLike?: Temporal.TimeZone | string): string;
Expand Down Expand Up @@ -541,7 +541,7 @@ export namespace Temporal {
readonly name: string;
getOffsetNanosecondsFor(absolute: Temporal.Absolute): number;
getOffsetStringFor(absolute: Temporal.Absolute): string;
getDateTimeFor(absolute: Temporal.Absolute): Temporal.DateTime;
getDateTimeFor(absolute: Temporal.Absolute, calendar?: Temporal.Calendar | string): Temporal.DateTime;
getAbsoluteFor(dateTime: Temporal.DateTime, options?: ToAbsoluteOptions): Temporal.Absolute;
getTransitions(startingPoint: Temporal.Absolute): IteratorResult<Temporal.Absolute>;
getPossibleAbsolutesFor(dateTime: Temporal.DateTime): Temporal.Absolute[];
Expand Down Expand Up @@ -620,8 +620,13 @@ export namespace Temporal {
* {@link https://en.wikipedia.org/wiki/List_of_tz_database_time_zones|IANA time zone identifier}
* string (e.g. `'Europe/London'`) or a `Temporal.TimeZone` instance. If omitted,
* the environment's current time zone will be used.
* @param {Temporal.Calendar | string} [calendar] - calendar identifier or a
* `Temporal.Calendar` instance. If omitted, the ISO 8601 calendar is used.
*/
export function dateTime(tzLike?: Temporal.TimeZone | string): Temporal.DateTime;
export function dateTime(
tzLike?: Temporal.TimeZone | string,
calendar?: Temporal.Calendar | string
): Temporal.DateTime;

/**
* Get the current calendar date in a specific time zone.
Expand All @@ -630,8 +635,10 @@ export namespace Temporal {
* {@link https://en.wikipedia.org/wiki/List_of_tz_database_time_zones|IANA time zone identifier}
* string (e.g. `'Europe/London'`) or a `Temporal.TimeZone` instance. If omitted,
* the environment's current time zone will be used.
* @param {Temporal.Calendar | string} [calendar] - calendar identifier or a
* `Temporal.Calendar` instance. If omitted, the ISO 8601 calendar is used.
*/
export function date(tzLike?: Temporal.TimeZone | string): Temporal.Date;
export function date(tzLike?: Temporal.TimeZone | string, calendar?: Temporal.Calendar | string): Temporal.Date;

/**
* Get the current clock time in a specific time zone.
Expand Down
4 changes: 2 additions & 2 deletions polyfill/lib/absolute.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ export class Absolute {
valueOf() {
throw new TypeError('use compare() or equals() to compare Temporal.Absolute');
}
inTimeZone(temporalTimeZoneLike = 'UTC') {
inTimeZone(temporalTimeZoneLike = 'UTC', calendar = undefined) {
if (!ES.IsTemporalAbsolute(this)) throw new TypeError('invalid receiver');
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
return timeZone.getDateTimeFor(this);
return timeZone.getDateTimeFor(this, calendar);
}

static fromEpochSeconds(epochSeconds) {
Expand Down
8 changes: 4 additions & 4 deletions polyfill/lib/now.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ function absolute() {
const Absolute = GetIntrinsic('%Temporal.Absolute%');
return new Absolute(ES.SystemUTCEpochNanoSeconds());
}
function dateTime(temporalTimeZoneLike = timeZone()) {
function dateTime(temporalTimeZoneLike = timeZone(), calendar = undefined) {
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
const abs = absolute();
const dateTime = timeZone.getDateTimeFor(abs);
const dateTime = timeZone.getDateTimeFor(abs, calendar);
return dateTime;
}
function date(temporalTimeZoneLike) {
return dateTime(temporalTimeZoneLike).getDate();
function date(temporalTimeZoneLike, calendar = undefined) {
return dateTime(temporalTimeZoneLike, calendar).getDate();
}
function time(temporalTimeZoneLike) {
return dateTime(temporalTimeZoneLike).getTime();
Expand Down
5 changes: 3 additions & 2 deletions polyfill/lib/timezone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class TimeZone {
if (!ES.IsTemporalAbsolute(absolute)) throw new TypeError('invalid Absolute object');
return ES.GetTimeZoneOffsetString(GetSlot(absolute, EPOCHNANOSECONDS), GetSlot(this, TIMEZONE_ID));
}
getDateTimeFor(absolute) {
getDateTimeFor(absolute, calendar = 'iso8601') {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
if (!ES.IsTemporalAbsolute(absolute)) throw new TypeError('invalid Absolute object');
const ns = GetSlot(absolute, EPOCHNANOSECONDS);
Expand All @@ -54,7 +54,8 @@ export class TimeZone {
nanosecond
} = ES.GetTimeZoneDateTimeParts(ns, GetSlot(this, TIMEZONE_ID));
const DateTime = GetIntrinsic('%Temporal.DateTime%');
return new DateTime(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond);
calendar = ES.ToTemporalCalendar(calendar);
return new DateTime(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, calendar);
}
getAbsoluteFor(dateTime, options) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
Expand Down

0 comments on commit 8947101

Please sign in to comment.