dst.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (C) 2011-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  2. // Copyright (C) 2011-2012 Regis Houssin <regis.houssin@capnetworks.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 <http://www.gnu.org/licenses/>.
  17. // or see http://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. // Detect and save TZ and DST
  27. var rightNow = new Date();
  28. var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
  29. var temp = jan1.toGMTString();
  30. var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
  31. var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
  32. var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
  33. temp = june1.toGMTString();
  34. var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
  35. var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
  36. var dst;
  37. if (std_time_offset == daylight_time_offset) {
  38. dst = "0"; // daylight savings time is NOT observed
  39. } else {
  40. dst = "1"; // daylight savings time is observed
  41. }
  42. var now=new Date();
  43. //alert('date=' + now + ' string=' + now.toTimeString());
  44. var dst_first=DisplayDstSwitchDates('first');
  45. var dst_second=DisplayDstSwitchDates('second');
  46. //alert(dst);
  47. $('#tz').val(std_time_offset); // returns TZ
  48. $('#tz_string').val(timezone.name()); // returns TZ string
  49. $('#dst_observed').val(dst); // returns if DST is observed on summer
  50. $('#dst_first').val(dst_first); // returns DST first switch in year
  51. $('#dst_second').val(dst_second); // returns DST second switch in year
  52. // Detect and save screen resolution
  53. $('#screenwidth').val($(window).width()); // returns width of browser viewport
  54. $('#screenheight').val($(window).height()); // returns width of browser viewport
  55. });
  56. function DisplayDstSwitchDates(firstsecond)
  57. {
  58. var year = new Date().getYear();
  59. if (year < 1000) year += 1900;
  60. var firstSwitch = 0;
  61. var secondSwitch = 0;
  62. var lastOffset = 99;
  63. // Loop through every month of the current year
  64. for (i = 0; i < 12; i++)
  65. {
  66. // Fetch the timezone value for the month
  67. var newDate = new Date(Date.UTC(year, i, 0, 0, 0, 0, 0));
  68. var tz = -1 * newDate.getTimezoneOffset() / 60;
  69. // Capture when a timzezone change occurs
  70. if (tz > lastOffset)
  71. firstSwitch = i-1;
  72. else if (tz < lastOffset)
  73. secondSwitch = i-1;
  74. lastOffset = tz;
  75. }
  76. // Go figure out date/time occurences a minute before
  77. // a DST adjustment occurs
  78. var secondDstDate = FindDstSwitchDate(year, secondSwitch);
  79. var firstDstDate = FindDstSwitchDate(year, firstSwitch);
  80. if (firstsecond == 'first') return firstDstDate;
  81. if (firstsecond == 'second') return secondDstDate;
  82. if (firstDstDate == null && secondDstDate == null)
  83. return 'Daylight Savings is not observed in your timezone.';
  84. else
  85. return 'Last minute before DST change occurs in ' +
  86. year + ': ' + firstDstDate + ' and ' + secondDstDate;
  87. }
  88. function FindDstSwitchDate(year, month)
  89. {
  90. // Set the starting date
  91. var baseDate = new Date(Date.UTC(year, month, 0, 0, 0, 0, 0));
  92. var changeDay = 0;
  93. var changeMinute = -1;
  94. var baseOffset = -1 * baseDate.getTimezoneOffset() / 60;
  95. var dstDate;
  96. // Loop to find the exact day a timezone adjust occurs
  97. for (day = 0; day < 50; day++)
  98. {
  99. var tmpDate = new Date(Date.UTC(year, month, day, 0, 0, 0, 0));
  100. var tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;
  101. // Check if the timezone changed from one day to the next
  102. if (tmpOffset != baseOffset)
  103. {
  104. var minutes = 0;
  105. changeDay = day;
  106. // Back-up one day and grap the offset
  107. tmpDate = new Date(Date.UTC(year, month, day-1, 0, 0, 0, 0));
  108. tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;
  109. // Count the minutes until a timezone chnage occurs
  110. while (changeMinute == -1)
  111. {
  112. tmpDate = new Date(Date.UTC(year, month, day-1, 0, minutes, 0, 0));
  113. tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;
  114. // Determine the exact minute a timezone change
  115. // occurs
  116. if (tmpOffset != baseOffset)
  117. {
  118. // Back-up a minute to get the date/time just
  119. // before a timezone change occurs
  120. tmpOffset = new Date(Date.UTC(year, month,
  121. day-1, 0, minutes-1, 0, 0));
  122. changeMinute = minutes;
  123. break;
  124. }
  125. else
  126. minutes++;
  127. }
  128. // Add a month (for display) since JavaScript counts
  129. // months from 0 to 11
  130. dstDate = tmpOffset.getMonth() + 1;
  131. // Pad the month as needed
  132. if (dstDate < 10) dstDate = "0" + dstDate;
  133. // Add the day and year
  134. dstDate = year + '-' + dstDate + '-' + tmpOffset.getDate() + 'T';
  135. // Capture the time stamp
  136. tmpDate = new Date(Date.UTC(year, month,
  137. day-1, 0, minutes-1, 0, 0));
  138. dstDate += tmpDate.toTimeString().split(' ')[0] + 'Z';
  139. return dstDate;
  140. }
  141. }
  142. }