index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { MIN, MS } from '../../constant';
  2. var typeToPos = {
  3. year: 0,
  4. month: 1,
  5. day: 2,
  6. hour: 3,
  7. minute: 4,
  8. second: 5
  9. }; // Cache time-zone lookups from Intl.DateTimeFormat,
  10. // as it is a *very* slow method.
  11. var dtfCache = {};
  12. var getDateTimeFormat = function getDateTimeFormat(timezone, options) {
  13. if (options === void 0) {
  14. options = {};
  15. }
  16. var timeZoneName = options.timeZoneName || 'short';
  17. var key = timezone + "|" + timeZoneName;
  18. var dtf = dtfCache[key];
  19. if (!dtf) {
  20. dtf = new Intl.DateTimeFormat('en-US', {
  21. hour12: false,
  22. timeZone: timezone,
  23. year: 'numeric',
  24. month: '2-digit',
  25. day: '2-digit',
  26. hour: '2-digit',
  27. minute: '2-digit',
  28. second: '2-digit',
  29. timeZoneName: timeZoneName
  30. });
  31. dtfCache[key] = dtf;
  32. }
  33. return dtf;
  34. };
  35. export default (function (o, c, d) {
  36. var defaultTimezone;
  37. var makeFormatParts = function makeFormatParts(timestamp, timezone, options) {
  38. if (options === void 0) {
  39. options = {};
  40. }
  41. var date = new Date(timestamp);
  42. var dtf = getDateTimeFormat(timezone, options);
  43. return dtf.formatToParts(date);
  44. };
  45. var tzOffset = function tzOffset(timestamp, timezone) {
  46. var formatResult = makeFormatParts(timestamp, timezone);
  47. var filled = [];
  48. for (var i = 0; i < formatResult.length; i += 1) {
  49. var _formatResult$i = formatResult[i],
  50. type = _formatResult$i.type,
  51. value = _formatResult$i.value;
  52. var pos = typeToPos[type];
  53. if (pos >= 0) {
  54. filled[pos] = parseInt(value, 10);
  55. }
  56. }
  57. var hour = filled[3]; // Workaround for the same behavior in different node version
  58. // https://github.com/nodejs/node/issues/33027
  59. /* istanbul ignore next */
  60. var fixedHour = hour === 24 ? 0 : hour;
  61. var utcString = filled[0] + "-" + filled[1] + "-" + filled[2] + " " + fixedHour + ":" + filled[4] + ":" + filled[5] + ":000";
  62. var utcTs = d.utc(utcString).valueOf();
  63. var asTS = +timestamp;
  64. var over = asTS % 1000;
  65. asTS -= over;
  66. return (utcTs - asTS) / (60 * 1000);
  67. }; // find the right offset a given local time. The o input is our guess, which determines which
  68. // offset we'll pick in ambiguous cases (e.g. there are two 3 AMs b/c Fallback DST)
  69. // https://github.com/moment/luxon/blob/master/src/datetime.js#L76
  70. var fixOffset = function fixOffset(localTS, o0, tz) {
  71. // Our UTC time is just a guess because our offset is just a guess
  72. var utcGuess = localTS - o0 * 60 * 1000; // Test whether the zone matches the offset for this ts
  73. var o2 = tzOffset(utcGuess, tz); // If so, offset didn't change and we're done
  74. if (o0 === o2) {
  75. return [utcGuess, o0];
  76. } // If not, change the ts by the difference in the offset
  77. utcGuess -= (o2 - o0) * 60 * 1000; // If that gives us the local time we want, we're done
  78. var o3 = tzOffset(utcGuess, tz);
  79. if (o2 === o3) {
  80. return [utcGuess, o2];
  81. } // If it's different, we're in a hole time.
  82. // The offset has changed, but the we don't adjust the time
  83. return [localTS - Math.min(o2, o3) * 60 * 1000, Math.max(o2, o3)];
  84. };
  85. var proto = c.prototype;
  86. proto.tz = function (timezone, keepLocalTime) {
  87. if (timezone === void 0) {
  88. timezone = defaultTimezone;
  89. }
  90. var oldOffset = this.utcOffset();
  91. var date = this.toDate();
  92. var target = date.toLocaleString('en-US', {
  93. timeZone: timezone
  94. });
  95. var diff = Math.round((date - new Date(target)) / 1000 / 60);
  96. var ins = d(target, {
  97. locale: this.$L
  98. }).$set(MS, this.$ms).utcOffset(-Math.round(date.getTimezoneOffset() / 15) * 15 - diff, true);
  99. if (keepLocalTime) {
  100. var newOffset = ins.utcOffset();
  101. ins = ins.add(oldOffset - newOffset, MIN);
  102. }
  103. ins.$x.$timezone = timezone;
  104. return ins;
  105. };
  106. proto.offsetName = function (type) {
  107. // type: short(default) / long
  108. var zone = this.$x.$timezone || d.tz.guess();
  109. var result = makeFormatParts(this.valueOf(), zone, {
  110. timeZoneName: type
  111. }).find(function (m) {
  112. return m.type.toLowerCase() === 'timezonename';
  113. });
  114. return result && result.value;
  115. };
  116. var oldStartOf = proto.startOf;
  117. proto.startOf = function (units, startOf) {
  118. if (!this.$x || !this.$x.$timezone) {
  119. return oldStartOf.call(this, units, startOf);
  120. }
  121. var withoutTz = d(this.format('YYYY-MM-DD HH:mm:ss:SSS'), {
  122. locale: this.$L
  123. });
  124. var startOfWithoutTz = oldStartOf.call(withoutTz, units, startOf);
  125. return startOfWithoutTz.tz(this.$x.$timezone, true);
  126. };
  127. d.tz = function (input, arg1, arg2) {
  128. var parseFormat = arg2 && arg1;
  129. var timezone = arg2 || arg1 || defaultTimezone;
  130. var previousOffset = tzOffset(+d(), timezone);
  131. if (typeof input !== 'string') {
  132. // timestamp number || js Date || Day.js
  133. return d(input).tz(timezone);
  134. }
  135. var localTs = d.utc(input, parseFormat).valueOf();
  136. var _fixOffset = fixOffset(localTs, previousOffset, timezone),
  137. targetTs = _fixOffset[0],
  138. targetOffset = _fixOffset[1];
  139. var ins = d(targetTs).utcOffset(targetOffset);
  140. ins.$x.$timezone = timezone;
  141. return ins;
  142. };
  143. d.tz.guess = function () {
  144. return Intl.DateTimeFormat().resolvedOptions().timeZone;
  145. };
  146. d.tz.setDefault = function (timezone) {
  147. defaultTimezone = timezone;
  148. };
  149. });