forked from tc39/proposal-temporal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimezone.mjs
More file actions
165 lines (153 loc) · 6.13 KB
/
timezone.mjs
File metadata and controls
165 lines (153 loc) · 6.13 KB
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
/* global __debug__ */
import * as ES from './ecmascript.mjs';
import { GetIntrinsic, MakeIntrinsicClass, DefineIntrinsic } from './intrinsicclass.mjs';
import {
TIMEZONE_ID,
EPOCHNANOSECONDS,
ISO_YEAR,
ISO_MONTH,
ISO_DAY,
ISO_HOUR,
ISO_MINUTE,
ISO_SECOND,
ISO_MILLISECOND,
ISO_MICROSECOND,
ISO_NANOSECOND,
CreateSlots,
GetSlot,
SetSlot
} from './slots.mjs';
export class TimeZone {
constructor(identifier) {
// Note: if the argument is not passed, GetCanonicalTimeZoneIdentifier(undefined) will throw.
// This check exists only to improve the error message.
if (arguments.length < 1) {
throw new RangeError('missing argument: identifier is required');
}
let stringIdentifier = ES.ToString(identifier);
if (ES.IsTimeZoneOffsetString(stringIdentifier)) {
stringIdentifier = ES.CanonicalizeTimeZoneOffsetString(stringIdentifier);
} else {
const record = ES.GetAvailableNamedTimeZoneIdentifier(stringIdentifier);
if (!record) throw new RangeError(`Invalid time zone identifier: ${stringIdentifier}`);
stringIdentifier = record.primaryIdentifier;
}
CreateSlots(this);
SetSlot(this, TIMEZONE_ID, stringIdentifier);
if (typeof __debug__ !== 'undefined' && __debug__) {
Object.defineProperty(this, '_repr_', {
value: `${this[Symbol.toStringTag]} <${stringIdentifier}>`,
writable: false,
enumerable: false,
configurable: false
});
}
}
get id() {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
return GetSlot(this, TIMEZONE_ID);
}
getOffsetNanosecondsFor(instant) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
instant = ES.ToTemporalInstant(instant);
const id = GetSlot(this, TIMEZONE_ID);
if (ES.IsTimeZoneOffsetString(id)) {
return ES.ParseTimeZoneOffsetString(id);
}
return ES.GetNamedTimeZoneOffsetNanoseconds(id, GetSlot(instant, EPOCHNANOSECONDS));
}
getOffsetStringFor(instant) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
instant = ES.ToTemporalInstant(instant);
return ES.GetOffsetStringFor(this, instant);
}
getPlainDateTimeFor(instant, calendar = 'iso8601') {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
instant = ES.ToTemporalInstant(instant);
calendar = ES.ToTemporalCalendarSlotValue(calendar);
return ES.GetPlainDateTimeFor(this, instant, calendar);
}
getInstantFor(dateTime, options = undefined) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
dateTime = ES.ToTemporalDateTime(dateTime);
options = ES.GetOptionsObject(options);
const disambiguation = ES.ToTemporalDisambiguation(options);
return ES.GetInstantFor(this, dateTime, disambiguation);
}
getPossibleInstantsFor(dateTime) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
dateTime = ES.ToTemporalDateTime(dateTime);
const Instant = GetIntrinsic('%Temporal.Instant%');
const id = GetSlot(this, TIMEZONE_ID);
if (ES.IsTimeZoneOffsetString(id)) {
const epochNs = ES.GetUTCEpochNanoseconds(
GetSlot(dateTime, ISO_YEAR),
GetSlot(dateTime, ISO_MONTH),
GetSlot(dateTime, ISO_DAY),
GetSlot(dateTime, ISO_HOUR),
GetSlot(dateTime, ISO_MINUTE),
GetSlot(dateTime, ISO_SECOND),
GetSlot(dateTime, ISO_MILLISECOND),
GetSlot(dateTime, ISO_MICROSECOND),
GetSlot(dateTime, ISO_NANOSECOND)
);
if (epochNs === null) throw new RangeError('DateTime outside of supported range');
const offsetNs = ES.ParseTimeZoneOffsetString(id);
return [new Instant(epochNs.minus(offsetNs))];
}
const possibleEpochNs = ES.GetNamedTimeZoneEpochNanoseconds(
id,
GetSlot(dateTime, ISO_YEAR),
GetSlot(dateTime, ISO_MONTH),
GetSlot(dateTime, ISO_DAY),
GetSlot(dateTime, ISO_HOUR),
GetSlot(dateTime, ISO_MINUTE),
GetSlot(dateTime, ISO_SECOND),
GetSlot(dateTime, ISO_MILLISECOND),
GetSlot(dateTime, ISO_MICROSECOND),
GetSlot(dateTime, ISO_NANOSECOND)
);
return possibleEpochNs.map((ns) => new Instant(ns));
}
getNextTransition(startingPoint) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
startingPoint = ES.ToTemporalInstant(startingPoint);
const id = GetSlot(this, TIMEZONE_ID);
// Offset time zones or UTC have no transitions
if (ES.IsTimeZoneOffsetString(id) || id === 'UTC') {
return null;
}
let epochNanoseconds = GetSlot(startingPoint, EPOCHNANOSECONDS);
const Instant = GetIntrinsic('%Temporal.Instant%');
epochNanoseconds = ES.GetNamedTimeZoneNextTransition(id, epochNanoseconds);
return epochNanoseconds === null ? null : new Instant(epochNanoseconds);
}
getPreviousTransition(startingPoint) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
startingPoint = ES.ToTemporalInstant(startingPoint);
const id = GetSlot(this, TIMEZONE_ID);
// Offset time zones or UTC have no transitions
if (ES.IsTimeZoneOffsetString(id) || id === 'UTC') {
return null;
}
let epochNanoseconds = GetSlot(startingPoint, EPOCHNANOSECONDS);
const Instant = GetIntrinsic('%Temporal.Instant%');
epochNanoseconds = ES.GetNamedTimeZonePreviousTransition(id, epochNanoseconds);
return epochNanoseconds === null ? null : new Instant(epochNanoseconds);
}
toString() {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
return GetSlot(this, TIMEZONE_ID);
}
toJSON() {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
return GetSlot(this, TIMEZONE_ID);
}
static from(item) {
const timeZoneSlotValue = ES.ToTemporalTimeZoneSlotValue(item);
return ES.ToTemporalTimeZoneObject(timeZoneSlotValue);
}
}
MakeIntrinsicClass(TimeZone, 'Temporal.TimeZone');
DefineIntrinsic('Temporal.TimeZone.prototype.getOffsetNanosecondsFor', TimeZone.prototype.getOffsetNanosecondsFor);
DefineIntrinsic('Temporal.TimeZone.prototype.getPossibleInstantsFor', TimeZone.prototype.getPossibleInstantsFor);