Skip to content
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

Update from() methods. #502

Merged
merged 17 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/absolute.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ turnOfTheCentury = new Temporal.Absolute(-2208988800000000000n); // => 1900-01-

## Static methods

### Temporal.Absolute.**from**(_thing_: string | Temporal.Absolute) : Temporal.Absolute
### Temporal.Absolute.**from**(_thing_: any) : Temporal.Absolute

**Parameters:**
- `thing` (string or `Temporal.Absolute`): The value representing the desired point in time.
- `thing`: The value representing the desired point in time.

**Returns:** a new `Temporal.Absolute` object (or the same object if `thing` was a `Temporal.Absolute` object.)
**Returns:** a new `Temporal.Absolute` object.

This static method creates a new `Temporal.Absolute` object from another value.
If the value is a string, it must be in ISO 8601 format, including a date, a time, and a time zone.
If the value is an object, it must be another `Temporal.Absolute` object, in which case the same object is returned.
If the value is another `Temporal.Absolute` object, a new object representing the same point in time is returned.

If `thing` is a string, and the point in time cannot be uniquely determined from the string, then this function throws an exception.
Any other value is converted to a string, which is expected to be in ISO 8601 format, including a date, a time, and a time zone.
If the point in time cannot be uniquely determined from the string, then this function throws an exception.
This includes the case when `thing` is a validly-formatted ISO 8601 string denoting a time that doesn't exist, for example because it was skipped in a daylight saving time transition.

Example usage:
Expand Down
14 changes: 7 additions & 7 deletions docs/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,26 @@ date = new Temporal.Date(2020, 3, 14) // => 2020-03-14

## Static methods

### Temporal.Date.**from**(_thing_: string | object, _options_?: object) : Temporal.Date
### Temporal.Date.**from**(_thing_: any, _options_?: object) : Temporal.Date

**Parameters:**
- `thing` (string or object): The value representing the desired date.
- `thing`: The value representing the desired date.
- `options` (optional object): An object with properties representing options for constructing the date.
The following options are recognized:
- `disambiguation` (string): How to deal with out-of-range values in `thing`.
Allowed values are `constrain`, `balance`, and `reject`.
The default is `constrain`.

**Returns:** a new `Temporal.Date` object (or the same object if `thing` was a `Temporal.Date` object.)
**Returns:** a new `Temporal.Date` object.

This static method creates a new `Temporal.Date` object from another value.
If the value is a string, it must be in ISO 8601 format.
If the value is another `Temporal.Date` object, a new object representing the same date is returned.
If the value is any other object, it must have `year`, `month`, and `day` properties, and a `Temporal.Date` will be constructed from them.

Any non-object value is converted to a string, which is expected to be in ISO 8601 format.
Any time or time zone part is optional and will be ignored.
If the string isn't valid according to ISO 8601, then a `RangeError` will be thrown regardless of the value of `disambiguation`.

If the value is another `Temporal.Date` object, the same object is returned.
If the value is any other object, it must have `year`, `month`, and `day` properties, and a `Temporal.Date` will be constructed from them.

The `disambiguation` option works as follows:
- In `constrain` mode (the default), any out-of-range values are clamped to the nearest in-range value.
- In `balance` mode, any out-of-range values are resolved by balancing them with the next highest unit.
Expand Down
16 changes: 8 additions & 8 deletions docs/datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,28 @@ datetime = new Temporal.DateTime(2020, 3, 14, 13, 37) // => 2020-03-14T13:37

## Static methods

### Temporal.DateTime.**from**(_thing_: string | object, _options_?: object) : Temporal.DateTime
### Temporal.DateTime.**from**(_thing_: any, _options_?: object) : Temporal.DateTime

**Parameters:**
- `thing` (string or object): The value representing the desired date and time.
- `thing`: The value representing the desired date and time.
- `options` (optional object): An object with properties representing options for constructing the date and time.
The following options are recognized:
- `disambiguation` (string): How to deal with out-of-range values in `thing`.
Allowed values are `constrain`, `balance`, and `reject`.
The default is `constrain`.

**Returns:** a new `Temporal.DateTime` object (or the same object if `thing` was a `Temporal.DateTime` object.)
**Returns:** a new `Temporal.DateTime` object.

This static method creates a new `Temporal.DateTime` object from another value.
If the value is a string, it must be in ISO 8601 format.
Any time zone part is optional and will be ignored.
If the string isn't valid according to ISO 8601, then a `RangeError` will be thrown regardless of the value of `disambiguation`.

If the value is another `Temporal.DateTime` object, the same object is returned.
If the value is another `Temporal.DateTime` object, a new object representing the same date and time is returned.
If the value is any other object, a `Temporal.DateTime` will be constructed from the values of any `year`, `month`, `day`, `hour`, `minute`, `second`, `millisecond`, `microsecond`, and `nanosecond` properties that are present.
At least the `year`, `month`, and `day` properties must be present.
Any other missing ones will be assumed to be 0.

Any non-object value is converted to a string, which is expected to be in ISO 8601 format.
Any time zone part is optional and will be ignored.
If the string isn't valid according to ISO 8601, then a `RangeError` will be thrown regardless of the value of `disambiguation`.

The `disambiguation` option works as follows:
- In `constrain` mode (the default), any out-of-range values are clamped to the nearest in-range value.
- In `balance` mode, any out-of-range values are resolved by balancing them with the next highest unit.
Expand Down
13 changes: 7 additions & 6 deletions docs/duration.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ new Temporal.Duration() // => PT0S

## Static methods

### Temporal.Duration.**from**(_thing_: string | object, _options_?: object) : Temporal.Duration
### Temporal.Duration.**from**(_thing_: any, _options_?: object) : Temporal.Duration

**Parameters:**
- `thing` (string or object): A `Duration`-like object or a string from which to create a `Temporal.Duration`.
- `thing`: A `Duration`-like object or a string from which to create a `Temporal.Duration`.
- `options` (optional object): An object with properties representing options for constructing the duration.
The following options are recognized:
- `disambiguation` (optional string): How to disambiguate if any of the other arguments are out of range.
Expand All @@ -70,11 +70,12 @@ new Temporal.Duration() // => PT0S

**Returns:** a new `Temporal.Duration` object.

This static method creates a new duration from another value.
The value can be a string in the ISO 8601 format described above.
Or, if the value is an object, it can either be another `Temporal.Duration` object, or a plain object with properties `years`, `months`, etc.
This static method creates a new `Temporal.Duration` from another value.
If the value is another `Temporal.Duration` object, a new object representing the same duration is returned.
If the value is any other object, a `Temporal.Duration` will be constructed from the values of any `years`, `months`, `days`, `hours`, `minutes`, `seconds`, `milliseconds`, `microseconds`, and `nanoseconds` properties that are present.
Any missing ones will be assumed to be 0.

When creating a duration from an object, this function is often more readable to use than `new Temporal.Duration()` because the names of the properties are visible in the code.
Any non-object value is converted to a string, which is expected to be in ISO 8601 format.

The `disambiguation` option controls how out-of-range values are interpreted:
- `constrain` (the default): Infinite values are clamped to `Number.MAX_VALUE`.
Expand Down
14 changes: 7 additions & 7 deletions docs/monthday.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ md = new Temporal.MonthDay(2, 29) // => 02-29

## Static methods

### Temporal.MonthDay.**from**(_thing_: string | object, _options_?: object) : Temporal.MonthDay
### Temporal.MonthDay.**from**(_thing_: any, _options_?: object) : Temporal.MonthDay

**Parameters:**
- `thing` (string or object): The value representing the desired date.
- `thing`: The value representing the desired date.
- `options` (optional object): An object with properties representing options for constructing the date.
The following options are recognized:
- `disambiguation` (string): How to deal with out-of-range values in `thing`.
Allowed values are `constrain`, `balance`, and `reject`.
The default is `constrain`.

**Returns:** a new `Temporal.MonthDay` object (or the same object if `thing` was a `Temporal.MonthDay` object.)
**Returns:** a new `Temporal.MonthDay` object.

This static method creates a new `Temporal.MonthDay` object from another value.
If the value is a string, it must be in ISO 8601 format.
If the value is another `Temporal.MonthDay` object, a new object representing the same month and day is returned.
If the value is any other object, it must have `month` and `day` properties, and a `Temporal.MonthDay` will be constructed from them.

Any non-object value will be converted to a string, which is expected to be in ISO 8601 format.
Any parts of the string other than the month and the day are optional and will be ignored.
If the string isn't valid according to ISO 8601, then a `RangeError` will be thrown regardless of the value of `disambiguation`.

If the value is another `Temporal.MonthDay` object, the same object is returned.
If the value is any other object, it must have `month` and `day` properties, and a `Temporal.MonthDay` will be constructed from them.

The `disambiguation` option works as follows:
- In `constrain` mode (the default), any out-of-range values are clamped to the nearest in-range value.
- In `balance` mode, any out-of-range values are resolved by balancing them with the next highest unit.
Expand Down
13 changes: 7 additions & 6 deletions docs/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@ time = new Temporal.Time(13, 37) // => 13:37

## Static methods

### Temporal.Time.**from**(_thing_: string | object, _options_?: object) : Temporal.Time
### Temporal.Time.**from**(_thing_: any, _options_?: object) : Temporal.Time

**Parameters:**
- `thing` (string or object): The value representing the desired time.
- `thing`: The value representing the desired time.
- `options` (optional object): An object with properties representing options for constructing the time.
The following options are recognized:
- `disambiguation` (optional string): How to deal with out-of-range values of the other parameters.
Allowed values are `constrain`, `balance`, and `reject`.
The default is `constrain`.

**Returns:** a new `Temporal.Time` object (or the same object if `thing` was a `Temporal.Time` object.)
**Returns:** a new `Temporal.Time` object.

This static method creates a new `Temporal.Time` object from another value.
If the value is a string, it must be in ISO 8601 format.
If the string designates a date or a time zone, they will be ignored.
If the value is another `Temporal.Time` object, the same object is returned.
If the value is another `Temporal.Time` object, a new object representing the same time is returned.
If the value is any other object, a `Temporal.Time` will be constructed from the values of any `hour`, `minute`, `second`, `millisecond`, `microsecond`, and `nanosecond` properties that are present.
Any missing ones will be assumed to be 0.

Any non-object value will be converted to a string, which is expected to be in ISO 8601 format.
If the string designates a date or a time zone, they will be ignored.

The `disambiguation` option works as follows:
- In `constrain` mode (the default), any out-of-range values are clamped to the nearest in-range value.
- In `balance` mode, any out-of-range values are resolved by balancing them with the next highest unit.
Expand Down
11 changes: 6 additions & 5 deletions docs/timezone.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ tz2.getTransitions(now).next().done; // => false

## Static methods

### Temporal.TimeZone.**from**(_thing_: string | object) : Temporal.TimeZone
### Temporal.TimeZone.**from**(_thing_: any) : Temporal.TimeZone

**Parameters:**
- `thing` (string or object): A `Temporal.TimeZone` object or a string from which to create a `Temporal.TimeZone`.
- `thing`: A `Temporal.TimeZone` object or a value from which to create a `Temporal.TimeZone`.

**Returns:** a new `Temporal.TimeZone` object.

This static method creates a new time zone from another value.
If the value is a string, it can be:
- a string that is accepted by `new Temporal.TimeZone()`;
If the value is another `Temporal.TimeZone` object, a new object representing the same time zone is returned.

Any other value is converted to a string, which is expected to be either:
- a string that is accepted by `new Temporal.TimeZone()`; or
- a string in the ISO 8601 format including a time zone offset part.
Or, if the value is an object, it can be another `Temporal.TimeZone` object, which is returned directly.

Note that the ISO 8601 string can optionally be extended with an IANA time zone name in square brackets appended to it.

Expand Down
14 changes: 7 additions & 7 deletions docs/yearmonth.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ ym = new Temporal.YearMonth(2019, 6) // => 2019-06

## Static methods

### Temporal.YearMonth.**from**(_thing_: string | object, _options_?: object) : Temporal.YearMonth
### Temporal.YearMonth.**from**(_thing_: any, _options_?: object) : Temporal.YearMonth

**Parameters:**
- `thing` (string or object): The value representing the desired month.
- `thing`: The value representing the desired month.
- `options` (optional object): An object with properties representing options for constructing the date.
The following options are recognized:
- `disambiguation` (string): How to deal with out-of-range values in `thing`.
Allowed values are `constrain`, `balance`, and `reject`.
The default is `constrain`.

**Returns:** a new `Temporal.YearMonth` object (or the same object if `thing` was a `Temporal.YearMonth` object.)
**Returns:** a new `Temporal.YearMonth` object.

This static method creates a new `Temporal.YearMonth` object from another value.
If the value is a string, it must be in ISO 8601 format.
If the value is another `Temporal.YearMonth` object, a new object representing the same month is returned.
If the value is any other object, it must have `year` and `month` properties, and a `Temporal.YearMonth` will be constructed from them.

Any non-object value is converted to a string, which is expected to be in ISO 8601 format.
Any parts of the string other than the year and the month are optional and will be ignored.
If the string isn't valid according to ISO 8601, then a `RangeError` will be thrown regardless of the value of `disambiguation`.

If the value is another `Temporal.YearMonth` object, the same object is returned.
If the value is any other object, it must have `year` and `month` properties, and a `Temporal.YearMonth` will be constructed from them.

The `disambiguation` option works as follows:
- In `constrain` mode (the default), any out-of-range values are clamped to the nearest in-range value.
- In `balance` mode, any out-of-range values are resolved by balancing them with the next highest unit.
Expand Down
16 changes: 10 additions & 6 deletions polyfill/lib/absolute.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ES } from './ecmascript.mjs';
import { MakeIntrinsicClass } from './intrinsicclass.mjs';
import { EPOCHNANOSECONDS, CreateSlots, GetSlot, SetSlot, YEARS, MONTHS } from './slots.mjs';
import { EPOCHNANOSECONDS, CreateSlots, GetSlot, SetSlot } from './slots.mjs';

import bigInt from 'big-integer';

Expand Down Expand Up @@ -42,7 +42,7 @@ export class Absolute {
milliseconds,
microseconds,
nanoseconds
} = ES.ToLimitedTemporalDuration(temporalDurationLike, [YEARS, MONTHS]);
} = ES.ToLimitedTemporalDuration(temporalDurationLike, ['years', 'months']);

let add = bigInt(0);
add = add.plus(bigInt(nanoseconds));
Expand Down Expand Up @@ -71,7 +71,7 @@ export class Absolute {
milliseconds,
microseconds,
nanoseconds
} = ES.ToLimitedTemporalDuration(temporalDurationLike, [YEARS, MONTHS]);
} = ES.ToLimitedTemporalDuration(temporalDurationLike, ['years', 'months']);

let add = bigInt(0);
add = add.plus(bigInt(nanoseconds));
Expand Down Expand Up @@ -171,9 +171,13 @@ export class Absolute {
return result;
}
static from(item) {
const absolute = ES.ToTemporalAbsolute(item);
if (this === Absolute) return absolute;
const result = new this(bigIntIfAvailable(GetSlot(absolute, EPOCHNANOSECONDS)));
let ns;
if (ES.IsTemporalAbsolute(item)) {
ns = GetSlot(item, EPOCHNANOSECONDS);
} else {
ns = ES.ParseTemporalAbsolute(ES.ToString(item));
}
const result = new this(bigIntIfAvailable(ns));
if (!ES.IsTemporalAbsolute(result)) throw new TypeError('invalid result');
return result;
}
Expand Down
20 changes: 17 additions & 3 deletions polyfill/lib/date.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,23 @@ export class Date {
}
static from(item, options = undefined) {
const disambiguation = ES.ToTemporalDisambiguation(options);
let result = ES.ToTemporalDate(item, disambiguation);
if (this === Date) return result;
return new this(GetSlot(result, YEAR), GetSlot(result, MONTH), GetSlot(result, DAY));
let year, month, day;
if (typeof item === 'object' && item) {
if (ES.IsTemporalDate(item)) {
year = GetSlot(item, YEAR);
month = GetSlot(item, MONTH);
day = GetSlot(item, DAY);
} else {
// Intentionally alphabetical
({ year, month, day } = ES.ToRecord(item, [['day'], ['month'], ['year']]));
}
} else {
({ year, month, day } = ES.ParseTemporalDateString(ES.ToString(item)));
}
({ year, month, day } = ES.RegulateDate(year, month, day, disambiguation));
const result = new this(year, month, day);
if (!ES.IsTemporalDate(result)) throw new TypeError('invalid result');
return result;
}
static compare(one, two) {
if (!ES.IsTemporalDate(one) || !ES.IsTemporalDate(two)) throw new TypeError('invalid Date object');
Expand Down
Loading