getTimezoneOffsetInMilliseconds.cjs 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. exports.getTimezoneOffsetInMilliseconds = getTimezoneOffsetInMilliseconds;
  3. var _index = require("../toDate.cjs");
  4. /**
  5. * Google Chrome as of 67.0.3396.87 introduced timezones with offset that includes seconds.
  6. * They usually appear for dates that denote time before the timezones were introduced
  7. * (e.g. for 'Europe/Prague' timezone the offset is GMT+00:57:44 before 1 October 1891
  8. * and GMT+01:00:00 after that date)
  9. *
  10. * Date#getTimezoneOffset returns the offset in minutes and would return 57 for the example above,
  11. * which would lead to incorrect calculations.
  12. *
  13. * This function returns the timezone offset in milliseconds that takes seconds in account.
  14. */
  15. function getTimezoneOffsetInMilliseconds(date) {
  16. const _date = (0, _index.toDate)(date);
  17. const utcDate = new Date(
  18. Date.UTC(
  19. _date.getFullYear(),
  20. _date.getMonth(),
  21. _date.getDate(),
  22. _date.getHours(),
  23. _date.getMinutes(),
  24. _date.getSeconds(),
  25. _date.getMilliseconds(),
  26. ),
  27. );
  28. utcDate.setUTCFullYear(_date.getFullYear());
  29. return +date - +utcDate;
  30. }