ui.scheduler.timezones.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * DevExtreme (ui/scheduler/timezones/ui.scheduler.timezones.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var query = require("../../../data/query");
  11. var errors = require("../../../core/errors");
  12. var tzData = require("./ui.scheduler.timezones_data");
  13. var SchedulerTimezones = {
  14. _displayNames: tzData.displayNames,
  15. _list: tzData.timezones,
  16. getTimezones: function() {
  17. return this._list
  18. },
  19. getDisplayNames: function() {
  20. return this._displayNames
  21. },
  22. queryableTimezones: function() {
  23. return query(this.getTimezones())
  24. },
  25. getTimezoneById: function(id) {
  26. var result;
  27. var i = 0;
  28. var tzList = this.getTimezones();
  29. if (id) {
  30. while (!result) {
  31. if (!tzList[i]) {
  32. errors.log("W0009", id);
  33. return
  34. }
  35. var currentId = tzList[i].id;
  36. if (currentId === id) {
  37. result = tzList[i]
  38. }
  39. i++
  40. }
  41. }
  42. return result
  43. },
  44. getTimezoneOffsetById: function(id, dateTimeStamp) {
  45. var tz = this.getTimezoneById(id);
  46. var offsets;
  47. var offsetIndices;
  48. var untils;
  49. var result;
  50. if (tz) {
  51. if (tz.link) {
  52. var rootTz = this.getTimezones()[tz.link];
  53. offsets = rootTz.offsets;
  54. untils = rootTz.untils;
  55. offsetIndices = rootTz.offsetIndices
  56. } else {
  57. offsets = tz.offsets;
  58. untils = tz.untils;
  59. offsetIndices = tz.offsetIndices
  60. }
  61. result = this.getUtcOffset(offsets, offsetIndices, untils, dateTimeStamp)
  62. }
  63. return result
  64. },
  65. getUtcOffset: function(offsets, offsetIndices, untils, dateTimeStamp) {
  66. var index = 0;
  67. var offsetIndicesList = offsetIndices.split("");
  68. var untilsList = untils.split("|").map(function(until) {
  69. if ("Infinity" === until) {
  70. return null
  71. }
  72. return 1e3 * parseInt(until, 36)
  73. });
  74. var currentUntil = 0;
  75. for (var i = 0, listLength = untilsList.length; i < listLength; i++) {
  76. currentUntil += untilsList[i];
  77. if (dateTimeStamp >= currentUntil) {
  78. index = i;
  79. continue
  80. } else {
  81. break
  82. }
  83. }
  84. if (untilsList[index + 1]) {
  85. index++
  86. }
  87. return offsets[Number(offsetIndicesList[index])]
  88. },
  89. getTimezoneShortDisplayNameById: function(id) {
  90. var tz = this.getTimezoneById(id);
  91. var result;
  92. if (tz) {
  93. result = tz.DisplayName.substring(0, 11)
  94. }
  95. return result
  96. },
  97. getTimezonesDisplayName: function() {
  98. return query(this.getDisplayNames()).sortBy().toArray()
  99. },
  100. getTimezoneDisplayNameById: function(id) {
  101. var tz = this.getTimezoneById(id);
  102. return tz ? this.getDisplayNames()[tz.winIndex] : ""
  103. },
  104. getSimilarTimezones: function(id) {
  105. if (!id) {
  106. return []
  107. }
  108. var tz = this.getTimezoneById(id);
  109. return this.getTimezonesIdsByWinIndex(tz.winIndex)
  110. },
  111. getTimezonesIdsByWinIndex: function(winIndex) {
  112. return this.queryableTimezones().filter(["winIndex", winIndex]).sortBy("title").toArray().map(function(item) {
  113. return {
  114. id: item.id,
  115. displayName: item.title
  116. }
  117. })
  118. },
  119. getTimezonesIdsByDisplayName: function(displayName) {
  120. var displayNameIndex = this.getDisplayNames().indexOf(displayName);
  121. return this.getTimezonesIdsByWinIndex(displayNameIndex)
  122. },
  123. getClientTimezoneOffset: function(date) {
  124. return 6e4 * date.getTimezoneOffset()
  125. },
  126. processDateDependOnTimezone: function(date, tzOffset) {
  127. var result = new Date(date);
  128. if (tzOffset) {
  129. var tzDiff = tzOffset + this.getClientTimezoneOffset(date) / 36e5;
  130. result = new Date(result.setHours(result.getHours() + tzDiff))
  131. }
  132. return result
  133. }
  134. };
  135. module.exports = SchedulerTimezones;