dst.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (C) 2011-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  2. // Copyright (C) 2011-2012 Regis Houssin <regis.houssin@inodbox.com>
  3. // Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. // or see https://www.gnu.org/
  18. //
  19. //
  20. // \file htdocs/core/js/dst.js
  21. // \brief File that include javascript functions for detect user tz, tz_string, dst_observed, dst_first, dst_second,
  22. // screenwidth and screenheight
  23. //
  24. $(document).ready(function () {
  25. var timezone = jstz.determine();
  26. console.log("Timezone detected for user: "+timezone.name());
  27. // Detect and save TZ and DST
  28. var rightNow = new Date();
  29. var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
  30. var temp = jan1.toGMTString();
  31. var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
  32. var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
  33. var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
  34. temp = june1.toGMTString();
  35. var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
  36. var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
  37. var dst;
  38. if (std_time_offset == daylight_time_offset) {
  39. dst = "0"; // daylight savings time is NOT observed
  40. } else {
  41. dst = "1"; // daylight savings time is observed
  42. }
  43. var now=new Date();
  44. //alert('date=' + now + ' string=' + now.toTimeString());
  45. var dst_first=DisplayDstSwitchDates('first');
  46. var dst_second=DisplayDstSwitchDates('second');
  47. //alert(dst);
  48. $('#tz').val(std_time_offset); // returns TZ
  49. $('#tz_string').val(timezone.name()); // returns TZ string
  50. $('#dst_observed').val(dst); // returns if DST is observed on summer
  51. $('#dst_first').val(dst_first); // returns DST first switch in year
  52. $('#dst_second').val(dst_second); // returns DST second switch in year
  53. // Detect and save screen resolution
  54. $('#screenwidth').val($(window).width()); // returns width of browser viewport
  55. $('#screenheight').val($(window).height()); // returns width of browser viewport
  56. });
  57. function DisplayDstSwitchDates(firstsecond)
  58. {
  59. var year = new Date().getYear();
  60. if (year < 1000) year += 1900;
  61. var firstSwitch = 0;
  62. var secondSwitch = 0;
  63. var lastOffset = 99;
  64. // Loop through every month of the current year
  65. for (i = 0; i < 12; i++)
  66. {
  67. // Fetch the timezone value for the month
  68. var newDate = new Date(Date.UTC(year, i, 0, 0, 0, 0, 0));
  69. var tz = -1 * newDate.getTimezoneOffset() / 60;
  70. // Capture when a timzezone change occurs
  71. if (tz > lastOffset)
  72. firstSwitch = i-1;
  73. else if (tz < lastOffset)
  74. secondSwitch = i-1;
  75. lastOffset = tz;
  76. }
  77. // Go figure out date/time occurences a minute before
  78. // a DST adjustment occurs
  79. var secondDstDate = FindDstSwitchDate(year, secondSwitch);
  80. var firstDstDate = FindDstSwitchDate(year, firstSwitch);
  81. if (firstsecond == 'first') return firstDstDate;
  82. if (firstsecond == 'second') return secondDstDate;
  83. if (firstDstDate == null && secondDstDate == null)
  84. return 'Daylight Savings is not observed in your timezone.';
  85. else
  86. return 'Last minute before DST change occurs in ' +
  87. year + ': ' + firstDstDate + ' and ' + secondDstDate;
  88. }
  89. function FindDstSwitchDate(year, month)
  90. {
  91. // Set the starting date
  92. var baseDate = new Date(Date.UTC(year, month, 0, 0, 0, 0, 0));
  93. var changeDay = 0;
  94. var changeMinute = -1;
  95. var baseOffset = -1 * baseDate.getTimezoneOffset() / 60;
  96. var dstDate;
  97. // Loop to find the exact day a timezone adjust occurs
  98. for (day = 0; day < 50; day++)
  99. {
  100. var tmpDate = new Date(Date.UTC(year, month, day, 0, 0, 0, 0));
  101. var tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;
  102. // Check if the timezone changed from one day to the next
  103. if (tmpOffset != baseOffset)
  104. {
  105. var minutes = 0;
  106. changeDay = day;
  107. // Back-up one day and grap the offset
  108. tmpDate = new Date(Date.UTC(year, month, day-1, 0, 0, 0, 0));
  109. tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;
  110. // Count the minutes until a timezone chnage occurs
  111. while (changeMinute == -1)
  112. {
  113. tmpDate = new Date(Date.UTC(year, month, day-1, 0, minutes, 0, 0));
  114. tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;
  115. // Determine the exact minute a timezone change
  116. // occurs
  117. if (tmpOffset != baseOffset)
  118. {
  119. // Back-up a minute to get the date/time just
  120. // before a timezone change occurs
  121. tmpOffset = new Date(Date.UTC(year, month,
  122. day-1, 0, minutes-1, 0, 0));
  123. changeMinute = minutes;
  124. break;
  125. }
  126. else
  127. minutes++;
  128. }
  129. // Add a month (for display) since JavaScript counts
  130. // months from 0 to 11
  131. dstDate = tmpOffset.getMonth() + 1;
  132. // Pad the month as needed
  133. if (dstDate < 10) dstDate = "0" + dstDate;
  134. // Add the day and year
  135. dstDate = year + '-' + dstDate + '-' + tmpOffset.getDate() + 'T';
  136. // Capture the time stamp
  137. tmpDate = new Date(Date.UTC(year, month,
  138. day-1, 0, minutes-1, 0, 0));
  139. dstDate += tmpDate.toTimeString().split(' ')[0] + 'Z';
  140. return dstDate;
  141. }
  142. }
  143. }