TimeZoneString.js 1.6 KB

123456789101112131415161718192021222324252627282930313233
  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/9.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, let offsetSign be "+"; otherwise, let offsetSign be "-".
  17. // 5. Let offsetMin be the String representation of MinFromTime(abs(offset)), formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  18. // 6. Let offsetHour be the String representation of HourFromTime(abs(offset)), formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  19. // 7. 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-dependent timezone name, and the code unit 0x0029 (RIGHT PARENTHESIS).
  20. // 8. Return the string-concatenation of offsetSign, offsetHour, offsetMin, and tzName.
  21. // hack until LocalTZA, and "implementation-defined string" are available
  22. var ts = $toTimeString(new $Date(tv));
  23. return $slice(ts, $indexOf(ts, '(') + 1, $indexOf(ts, ')'));
  24. };