1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $Date = GetIntrinsic('%Date%');
|
---|
6 | var $TypeError = require('es-errors/type');
|
---|
7 |
|
---|
8 | var isInteger = require('../helpers/isInteger');
|
---|
9 |
|
---|
10 | var callBound = require('call-bind/callBound');
|
---|
11 |
|
---|
12 | var $indexOf = callBound('String.prototype.indexOf');
|
---|
13 | var $slice = callBound('String.prototype.slice');
|
---|
14 | var $toTimeString = callBound('Date.prototype.toTimeString');
|
---|
15 |
|
---|
16 | // https://262.ecma-international.org/14.0/#sec-timezoneestring
|
---|
17 |
|
---|
18 | module.exports = function TimeZoneString(tv) {
|
---|
19 | if (!isInteger(tv)) {
|
---|
20 | throw new $TypeError('Assertion failed: `tv` must be an integral Number');
|
---|
21 | }
|
---|
22 |
|
---|
23 | // 1. Let localTimeZone be DefaultTimeZone().
|
---|
24 | // 2. If IsTimeZoneOffsetString(localTimeZone) is true, then
|
---|
25 | // a. Let offsetNs be ParseTimeZoneOffsetString(localTimeZone).
|
---|
26 | // 3. Else,
|
---|
27 | // a. Let offsetNs be GetNamedTimeZoneOffsetNanoseconds(localTimeZone, ℤ(ℝ(tv) × 106)).
|
---|
28 | // 4. Let offset be 𝔽(truncate(offsetNs / 106)).
|
---|
29 | // 5. If offset is +0𝔽 or offset > +0𝔽, then
|
---|
30 | // a. Let offsetSign be "+".
|
---|
31 | // b. Let absOffset be offset.
|
---|
32 | // 6. Else,
|
---|
33 | // a. Let offsetSign be "-".
|
---|
34 | // b. Let absOffset be -offset.
|
---|
35 | // 7. Let offsetMin be ToZeroPaddedDecimalString(ℝ(MinFromTime(absOffset)), 2).
|
---|
36 | // 8. Let offsetHour be ToZeroPaddedDecimalString(ℝ(HourFromTime(absOffset)), 2).
|
---|
37 | // 9. Let tzName be an implementation-defined string that is either the empty String or the string-concatenation of the code unit 0x0020 (SPACE), the code unit 0x0028 (LEFT PARENTHESIS), an implementation-defined timezone name, and the code unit 0x0029 (RIGHT PARENTHESIS).
|
---|
38 | // 10. Return the string-concatenation of offsetSign, offsetHour, offsetMin, and tzName.
|
---|
39 |
|
---|
40 | // hack until DefaultTimeZone, IsTimeZoneOffsetString, ParseTimeZoneOffsetString, GetNamedTimeZoneOffsetNanoseconds, and "implementation-defined string" are available
|
---|
41 | var ts = $toTimeString(new $Date(tv));
|
---|
42 | return $slice(ts, $indexOf(ts, '(') + 1, $indexOf(ts, ')'));
|
---|
43 | };
|
---|