TimeZoneString.js 1.8 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/12.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'); // steps 1 - 2
  14. }
  15. // 3. Let offset be LocalTZA(tv, true).
  16. // 4. If offset ≥ +0𝔽, then
  17. // a. Let offsetSign be "+".
  18. // b. Let absOffset be offset.
  19. // 5. Else,
  20. // a. Let offsetSign be "-".
  21. // b. Let absOffset be -offset.
  22. // 6. Let offsetMin be the String representation of MinFromTime(absOffset), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
  23. // 7. Let offsetHour be the String representation of HourFromTime(absOffset), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
  24. // 8. 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. // 9. 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. };