TimeZoneString.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $Date = GetIntrinsic('%Date%');
  4. var $TypeError = require('es-errors/type');
  5. var isNaN = require('../helpers/isNaN');
  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/13.0/#sec-timezoneestring
  11. module.exports = function TimeZoneString(tv) {
  12. if (typeof tv !== 'number' || isNaN(tv)) {
  13. throw new $TypeError('Assertion failed: `tv` must be a non-NaN Number');
  14. }
  15. // 1. Let offset be LocalTZA(tv, true).
  16. // 2. If offset is +0𝔽 or offset > +0𝔽, then
  17. // a. Let offsetSign be "+".
  18. // b. Let absOffset be offset.
  19. // 3. Else,
  20. // a. Let offsetSign be "-".
  21. // b. Let absOffset be -offset.
  22. // 4. Let offsetMin be ToZeroPaddedDecimalString(ℝ(MinFromTime(absOffset)), 2).
  23. // 5. Let offsetHour be ToZeroPaddedDecimalString(ℝ(HourFromTime(absOffset)), 2).
  24. // 6. 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).
  25. // 7. Return the string-concatenation of offsetSign, offsetHour, offsetMin, and tzName.
  26. // hack until LocalTZA, and "implementation-defined string" are available
  27. var ts = $toTimeString(new $Date(tv));
  28. return $slice(ts, $indexOf(ts, '(') + 1, $indexOf(ts, ')'));
  29. };