Skip to content

Commit

Permalink
Remove time zone argument from Absolute.toString
Browse files Browse the repository at this point in the history
Instead of choosing a time zone in which to serialize an Absolute, the
recommended way to keep a time stamp with a time zone is LocalDateTime
(either serialized or not.)

Closes: #741
  • Loading branch information
ptomato committed Aug 25, 2020
1 parent 8266a63 commit 66c59c9
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 250 deletions.
8 changes: 1 addition & 7 deletions docs/absolute.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,7 @@ one.equals(two) // => false
one.equals(one) // => true
```

### absolute.**toString**(_timeZone_?: object | string) : string

**Parameters:**
- `timeZone` (optional string or object): the time zone to express `absolute` in, as a `Temporal.TimeZone` object, an object implementing the [time zone protocol](./timezone.md#protocol), or a string.
The default is to use UTC.
### absolute.**toString**() : string

**Returns:** a string in the ISO 8601 date format representing `absolute`.

Expand All @@ -373,8 +369,6 @@ Example usage:
```js
abs = new Temporal.Absolute(1574074321816000000n);
abs.toString(); // => 2019-11-18T10:52:01.816Z
abs.toString(Temporal.TimeZone.from('UTC')); // => 2019-11-18T10:52:01.816Z
abs.toString('Asia/Seoul'); // => 2019-11-18T19:52:01.816+09:00[Asia/Seoul]
```

### absolute.**toLocaleString**(_locales_?: string | array<string>, _options_?: object) : string
Expand Down
2 changes: 2 additions & 0 deletions docs/cookbook/getInstantWithLocalTimeInZone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function getInstantWithLocalTimeInZone(dateTime, timeZone, disambiguation = 'ear
const germany = Temporal.TimeZone.from('Europe/Berlin');
const nonexistentGermanWallTime = Temporal.DateTime.from('2019-03-31T02:45');

// FIXME: these need to use LocalDateTime

const germanResults = {
earlier: /* */ '2019-03-31T01:45+01:00[Europe/Berlin]',
later: /* */ '2019-03-31T03:45+02:00[Europe/Berlin]',
Expand Down
2 changes: 2 additions & 0 deletions docs/cookbook/getParseableZonedStringAtInstant.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FIXME: these need to use LocalDateTime

const absoluteTime = Temporal.Absolute.from('2020-01-03T10:41:51Z');

const result = absoluteTime.toString('Europe/Paris');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function getParseableZonedStringWithLocalTimeInOtherZone(
sourceDisambiguationPolicy = 'reject'
) {
let instant = sourceDateTime.toAbsolute(sourceTimeZone, { disambiguation: sourceDisambiguationPolicy });
// FIXME: this needs to use LocalDateTime
return instant.toString(targetTimeZone);
}

Expand Down
2 changes: 1 addition & 1 deletion polyfill/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export namespace Temporal {
toDateTime(tzLike: TimeZoneProtocol | string, calendar?: CalendarProtocol | string): Temporal.DateTime;
toLocaleString(locales?: string | string[], options?: Intl.DateTimeFormatOptions): string;
toJSON(): string;
toString(tzLike?: TimeZoneProtocol | string): string;
toString(): string;
}

export interface CalendarProtocol {
Expand Down
12 changes: 3 additions & 9 deletions polyfill/lib/absolute.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,9 @@ export class Absolute {
const two = GetSlot(other, EPOCHNANOSECONDS);
return bigInt(one).equals(two);
}
toString(temporalTimeZoneLike = 'UTC') {
toString() {
if (!ES.IsTemporalAbsolute(this)) throw new TypeError('invalid receiver');
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
return ES.TemporalAbsoluteToString(this, timeZone);
}
toJSON() {
if (!ES.IsTemporalAbsolute(this)) throw new TypeError('invalid receiver');
const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%');
const timeZone = new TemporalTimeZone('UTC');
return ES.TemporalAbsoluteToString(this, timeZone);
return ES.TemporalAbsoluteToString(this);
}
toLocaleString(...args) {
if (!ES.IsTemporalAbsolute(this)) throw new TypeError('invalid receiver');
Expand Down Expand Up @@ -214,6 +207,7 @@ export class Absolute {
return 0;
}
}
Absolute.prototype.toJSON = Absolute.prototype.toString;

MakeIntrinsicClass(Absolute, 'Temporal.Absolute');

Expand Down
8 changes: 4 additions & 4 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,9 @@ export const ES = ObjectAssign({}, ES2019, {
let post = parts.length ? `.${parts.join('')}` : '';
return `:${secs}${post}`;
},
TemporalAbsoluteToString: (absolute, timeZone) => {
const dateTime = ES.GetTemporalDateTimeFor(timeZone, absolute);
TemporalAbsoluteToString: (absolute) => {
const ns = GetSlot(absolute, EPOCHNANOSECONDS);
const dateTime = ES.GetPartsFromEpoch(ns);
const year = ES.ISOYearString(dateTime.year);
const month = ES.ISODateTimePartString(dateTime.month);
const day = ES.ISODateTimePartString(dateTime.day);
Expand All @@ -699,8 +700,7 @@ export const ES = ObjectAssign({}, ES2019, {
dateTime.microsecond,
dateTime.nanosecond
);
const timeZoneString = ES.ISOTimeZoneString(timeZone, absolute);
return `${year}-${month}-${day}T${hour}:${minute}${seconds}${timeZoneString}`;
return `${year}-${month}-${day}T${hour}:${minute}${seconds}Z`;
},
TemporalDurationToString: (duration) => {
function formatNumber(num) {
Expand Down
84 changes: 0 additions & 84 deletions polyfill/test/Absolute/prototype/toString/argument-missing.js

This file was deleted.

17 changes: 17 additions & 0 deletions polyfill/test/Absolute/prototype/toString/timezone-no-effect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.absolute.prototype.tostring
includes: [compareArray.js]
---*/

const absolute = Temporal.Absolute.from("1975-02-02T14:25:36.123456Z");

Object.defineProperty(Temporal.TimeZone, "from", {
get() {
throw new Test262Error("should not get Temporal.TimeZone.from");
},
});

assert.sameValue(absolute.toString(), "1975-02-02T14:25:36.123456Z");
31 changes: 0 additions & 31 deletions polyfill/test/Absolute/prototype/toString/timezone-no-tostring.js

This file was deleted.

78 changes: 0 additions & 78 deletions polyfill/test/Absolute/prototype/toString/timezone.js

This file was deleted.

15 changes: 0 additions & 15 deletions polyfill/test/absolute.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ describe('Absolute', () => {
assert(instant);
equal(`${instant}`, iso);
});
it('optional time zone parameter UTC', () => {
const iso = '1976-11-18T14:23:30.123456789Z';
const abs = Absolute.from(iso);
const tz = Temporal.TimeZone.from('UTC');
equal(abs.toString(tz), iso);
});
it('optional time zone parameter non-UTC', () => {
const abs = Absolute.from('1976-11-18T14:23:30.123456789Z');
const tz = Temporal.TimeZone.from('America/New_York');
equal(abs.toString(tz), '1976-11-18T09:23:30.123456789-05:00[America/New_York]');
});
});
describe('Absolute.toJSON() works', () => {
it('`1976-11-18T15:23:30.123456789+01:00`.toJSON()', () => {
Expand All @@ -100,10 +89,6 @@ describe('Absolute', () => {
assert(abs);
equal(abs.toJSON(), '1963-02-13T09:36:29.123456789Z');
});
it('argument is ignored', () => {
const abs = Absolute.from('1976-11-18T15:23:30.123456789+01:00');
equal(abs.toJSON('+01:00'), abs.toJSON());
});
});
describe('Absolute.getEpochSeconds() works', () => {
it('post-epoch', () => {
Expand Down
14 changes: 2 additions & 12 deletions polyfill/test/usertimezone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const { equal, throws } = assert;

import * as Temporal from 'proposal-temporal';

// FIXME: add tests to make sure these cases work with LocalDateTime

describe('Userland time zone', () => {
describe('Trivial subclass', () => {
class CustomUTCSubclass extends Temporal.TimeZone {
Expand Down Expand Up @@ -59,8 +61,6 @@ describe('Userland time zone', () => {
equal(`${dt.toAbsolute(obj)}`, '1976-11-18T15:23:30.123456789Z');
});
it('converts to string', () => equal(`${obj}`, obj.name));
it('prints in absolute.toString', () =>
equal(abs.toString(obj), '1970-01-01T00:00+00:00[Etc/Custom_UTC_Subclass]'));
it('has no next transitions', () => assert.equal(obj.getNextTransition(), null));
it('has no previous transitions', () => assert.equal(obj.getPreviousTransition(), null));
it('works in Temporal.now', () => {
Expand Down Expand Up @@ -95,10 +95,6 @@ describe('Userland time zone', () => {
const abs = Temporal.Absolute.from('1970-01-01T00:00+00:00[Etc/Custom_UTC_Subclass]');
equal(`${abs}`, '1970-01-01T00:00Z');
});
it('works for Absolute.toString', () => {
const abs = Temporal.Absolute.fromEpochSeconds(0);
equal(abs.toString('Etc/Custom_UTC_Subclass'), '1970-01-01T00:00+00:00[Etc/Custom_UTC_Subclass]');
});
it('works for Absolute.toDateTime', () => {
const abs = Temporal.Absolute.fromEpochSeconds(0);
equal(`${abs.toDateTime('Etc/Custom_UTC_Subclass')}`, '1970-01-01T00:00');
Expand Down Expand Up @@ -147,8 +143,6 @@ describe('Userland time zone', () => {
equal(`${Temporal.TimeZone.prototype.getAbsoluteFor.call(obj, dt)}`, '1976-11-18T15:23:30.123456789Z');
equal(`${dt.toAbsolute(obj)}`, '1976-11-18T15:23:30.123456789Z');
});
it('prints in absolute.toString', () =>
equal(abs.toString(obj), '1970-01-01T00:00+00:00[Etc/Custom_UTC_Protocol]'));
it('works in Temporal.now', () => {
assert(Temporal.now.dateTime(obj) instanceof Temporal.DateTime);
assert(Temporal.now.date(obj) instanceof Temporal.Date);
Expand Down Expand Up @@ -181,10 +175,6 @@ describe('Userland time zone', () => {
const abs = Temporal.Absolute.from('1970-01-01T00:00+00:00[Etc/Custom_UTC_Protocol]');
equal(`${abs}`, '1970-01-01T00:00Z');
});
it('works for Absolute.toString', () => {
const abs = Temporal.Absolute.fromEpochSeconds(0);
equal(abs.toString('Etc/Custom_UTC_Protocol'), '1970-01-01T00:00+00:00[Etc/Custom_UTC_Protocol]');
});
it('works for Absolute.toDateTime', () => {
const abs = Temporal.Absolute.fromEpochSeconds(0);
equal(`${abs.toDateTime('Etc/Custom_UTC_Protocol')}`, '1970-01-01T00:00');
Expand Down
Loading

0 comments on commit 66c59c9

Please sign in to comment.