TimeZoneString.js 1.9 KB

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