timezone_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.i18n.TimeZoneTest');
  15. goog.setTestOnly('goog.i18n.TimeZoneTest');
  16. goog.require('goog.i18n.TimeZone');
  17. goog.require('goog.testing.jsunit');
  18. // Where could such data be found
  19. // In js_i18n_data in http://go/i18n_dir, we have a bunch of files with names
  20. // like TimeZoneConstant__<locale>.js
  21. // We strongly discourage you to use them directly as those data can make
  22. // your client code bloated. You should try to provide this data from server
  23. // in a selective manner. In typical scenario, user's time zone is retrieved
  24. // and only data for that time zone should be provided.
  25. // This piece of data is in JSON format. It requires double quote.
  26. var americaLosAngelesData = {
  27. 'transitions': [
  28. 2770, 60, 7137, 0, 11506, 60, 16041, 0, 20410, 60, 24777, 0,
  29. 29146, 60, 33513, 0, 35194, 60, 42249, 0, 45106, 60, 50985, 0,
  30. 55354, 60, 59889, 0, 64090, 60, 68625, 0, 72994, 60, 77361, 0,
  31. 81730, 60, 86097, 0, 90466, 60, 94833, 0, 99202, 60, 103569, 0,
  32. 107938, 60, 112473, 0, 116674, 60, 121209, 0, 125578, 60, 129945, 0,
  33. 134314, 60, 138681, 0, 143050, 60, 147417, 0, 151282, 60, 156153, 0,
  34. 160018, 60, 165057, 0, 168754, 60, 173793, 0, 177490, 60, 182529, 0,
  35. 186394, 60, 191265, 0, 195130, 60, 200001, 0, 203866, 60, 208905, 0,
  36. 212602, 60, 217641, 0, 221338, 60, 226377, 0, 230242, 60, 235113, 0,
  37. 238978, 60, 243849, 0, 247714, 60, 252585, 0, 256450, 60, 261489, 0,
  38. 265186, 60, 270225, 0, 273922, 60, 278961, 0, 282826, 60, 287697, 0,
  39. 291562, 60, 296433, 0, 300298, 60, 305337, 0, 309034, 60, 314073, 0,
  40. 317770, 60, 322809, 0, 326002, 60, 331713, 0, 334738, 60, 340449, 0,
  41. 343474, 60, 349185, 0, 352378, 60, 358089, 0, 361114, 60, 366825, 0,
  42. 369850, 60, 375561, 0, 378586, 60, 384297, 0, 387322, 60, 393033, 0,
  43. 396058, 60, 401769, 0, 404962, 60, 410673, 0, 413698, 60, 419409, 0,
  44. 422434, 60, 428145, 0, 431170, 60, 436881, 0, 439906, 60, 445617, 0,
  45. 448810, 60, 454521, 0, 457546, 60, 463257, 0, 466282, 60, 471993, 0,
  46. 475018, 60, 480729, 0, 483754, 60, 489465, 0, 492490, 60, 498201, 0,
  47. 501394, 60, 507105, 0, 510130, 60, 515841, 0, 518866, 60, 524577, 0,
  48. 527602, 60, 533313, 0, 536338, 60, 542049, 0, 545242, 60, 550953, 0,
  49. 553978, 60, 559689, 0, 562714, 60, 568425, 0, 571450, 60, 577161, 0,
  50. 580186, 60, 585897, 0, 588922, 60, 594633, 0
  51. ],
  52. 'names': ['PST', 'Pacific Standard Time', 'PDT', 'Pacific Daylight Time'],
  53. 'names_ext': {
  54. STD_LONG_NAME_GMT: 'GMT-08:00',
  55. STD_GENERIC_LOCATION: 'Los Angeles Time',
  56. DST_LONG_NAME_GMT: 'GMT-07:00',
  57. DST_GENERIC_LOCATION: 'Los Angeles Time'
  58. },
  59. 'id': 'America/Los_Angeles',
  60. 'std_offset': -480
  61. };
  62. var americaLosAngelesDataStringKeys = {
  63. 'transitions': [
  64. 2770, 60, 7137, 0, 11506, 60, 16041, 0, 20410, 60, 24777, 0,
  65. 29146, 60, 33513, 0, 35194, 60, 42249, 0, 45106, 60, 50985, 0,
  66. 55354, 60, 59889, 0, 64090, 60, 68625, 0, 72994, 60, 77361, 0,
  67. 81730, 60, 86097, 0, 90466, 60, 94833, 0, 99202, 60, 103569, 0,
  68. 107938, 60, 112473, 0, 116674, 60, 121209, 0, 125578, 60, 129945, 0,
  69. 134314, 60, 138681, 0, 143050, 60, 147417, 0, 151282, 60, 156153, 0,
  70. 160018, 60, 165057, 0, 168754, 60, 173793, 0, 177490, 60, 182529, 0,
  71. 186394, 60, 191265, 0, 195130, 60, 200001, 0, 203866, 60, 208905, 0,
  72. 212602, 60, 217641, 0, 221338, 60, 226377, 0, 230242, 60, 235113, 0,
  73. 238978, 60, 243849, 0, 247714, 60, 252585, 0, 256450, 60, 261489, 0,
  74. 265186, 60, 270225, 0, 273922, 60, 278961, 0, 282826, 60, 287697, 0,
  75. 291562, 60, 296433, 0, 300298, 60, 305337, 0, 309034, 60, 314073, 0,
  76. 317770, 60, 322809, 0, 326002, 60, 331713, 0, 334738, 60, 340449, 0,
  77. 343474, 60, 349185, 0, 352378, 60, 358089, 0, 361114, 60, 366825, 0,
  78. 369850, 60, 375561, 0, 378586, 60, 384297, 0, 387322, 60, 393033, 0,
  79. 396058, 60, 401769, 0, 404962, 60, 410673, 0, 413698, 60, 419409, 0,
  80. 422434, 60, 428145, 0, 431170, 60, 436881, 0, 439906, 60, 445617, 0,
  81. 448810, 60, 454521, 0, 457546, 60, 463257, 0, 466282, 60, 471993, 0,
  82. 475018, 60, 480729, 0, 483754, 60, 489465, 0, 492490, 60, 498201, 0,
  83. 501394, 60, 507105, 0, 510130, 60, 515841, 0, 518866, 60, 524577, 0,
  84. 527602, 60, 533313, 0, 536338, 60, 542049, 0, 545242, 60, 550953, 0,
  85. 553978, 60, 559689, 0, 562714, 60, 568425, 0, 571450, 60, 577161, 0,
  86. 580186, 60, 585897, 0, 588922, 60, 594633, 0
  87. ],
  88. 'names': ['PST', 'Pacific Standard Time', 'PDT', 'Pacific Daylight Time'],
  89. 'names_ext': {
  90. 'STD_LONG_NAME_GMT': 'GMT-08:00',
  91. 'STD_GENERIC_LOCATION': 'Los Angeles Time',
  92. 'DST_LONG_NAME_GMT': 'GMT-07:00',
  93. 'DST_GENERIC_LOCATION': 'Los Angeles Time'
  94. },
  95. 'id': 'America/Los_Angeles',
  96. 'std_offset': -480
  97. };
  98. function testIsDaylightTime() {
  99. var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  100. var dt = new Date(2007, 7 - 1, 1);
  101. assertTrue(usPacific.isDaylightTime(dt));
  102. // 2007/03/11 2:00am has daylight change. We set time through UTC so that
  103. // this test won't be affected by browser's local time handling.
  104. dt = new Date(2007, 3 - 1, 11);
  105. // The date above is created with a timezone of the computer it is run on.
  106. // Therefore the UTC day has to be set explicitly to be sure that the date
  107. // is correct for timezones east of Greenwich where 2007/3/11 0:00 is still
  108. // 2007/03/10 in UTC.
  109. dt.setUTCDate(11);
  110. dt.setUTCHours(2 + 8);
  111. dt.setUTCMinutes(1);
  112. assertTrue(usPacific.isDaylightTime(dt));
  113. dt.setUTCHours(1 + 8);
  114. dt.setUTCMinutes(59);
  115. assertTrue(!usPacific.isDaylightTime(dt));
  116. dt = new Date(2007, 11 - 1, 4);
  117. // Set the UTC day explicitly to make it work in timezones east of
  118. // Greenwich.
  119. dt.setUTCDate(4);
  120. dt.setUTCHours(2 + 7);
  121. dt.setUTCMinutes(1);
  122. assertTrue(!usPacific.isDaylightTime(dt));
  123. // there seems to be a browser bug. local time 1:59am should still be PDT.
  124. dt.setUTCHours(0 + 7);
  125. dt.setUTCMinutes(59);
  126. assertTrue(usPacific.isDaylightTime(dt));
  127. }
  128. function testGetters() {
  129. var date = new Date();
  130. var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  131. assertEquals('America/Los_Angeles', usPacific.getTimeZoneId());
  132. assertObjectEquals(americaLosAngelesData, usPacific.getTimeZoneData());
  133. }
  134. function testNames() {
  135. var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  136. var dt = new Date(2007, 7 - 1, 1);
  137. assertTrue(usPacific.isDaylightTime(dt));
  138. assertEquals('PDT', usPacific.getShortName(dt));
  139. assertEquals('Pacific Daylight Time', usPacific.getLongName(dt));
  140. dt = new Date(2007, 12 - 1, 1);
  141. assertTrue(!usPacific.isDaylightTime(dt));
  142. assertEquals('PST', usPacific.getShortName(dt));
  143. assertEquals('Pacific Standard Time', usPacific.getLongName(dt));
  144. }
  145. function testNamesExt() {
  146. var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  147. var dt = new Date(2007, 7 - 1, 1);
  148. assertTrue(usPacific.isDaylightTime(dt));
  149. assertEquals('GMT-07:00', usPacific.getLongNameGMT(dt));
  150. assertEquals('Los Angeles Time', usPacific.getGenericLocation(dt));
  151. dt = new Date(2007, 12 - 1, 1);
  152. assertTrue(!usPacific.isDaylightTime(dt));
  153. assertEquals('GMT-08:00', usPacific.getLongNameGMT(dt));
  154. assertEquals('Los Angeles Time', usPacific.getGenericLocation(dt));
  155. }
  156. function testNamesExtStringKeys() {
  157. var usPacific =
  158. goog.i18n.TimeZone.createTimeZone(americaLosAngelesDataStringKeys);
  159. var dt = new Date(2007, 7 - 1, 1);
  160. assertTrue(usPacific.isDaylightTime(dt));
  161. assertEquals('GMT-07:00', usPacific.getLongNameGMT(dt));
  162. assertEquals('Los Angeles Time', usPacific.getGenericLocation(dt));
  163. dt = new Date(2007, 12 - 1, 1);
  164. assertTrue(!usPacific.isDaylightTime(dt));
  165. assertEquals('GMT-08:00', usPacific.getLongNameGMT(dt));
  166. assertEquals('Los Angeles Time', usPacific.getGenericLocation(dt));
  167. }
  168. function testGeneratedData() {
  169. var usPacific = goog.i18n.TimeZone.createTimeZone(americaLosAngelesData);
  170. var dt = new Date(2007, 7 - 1, 1);
  171. assertTrue(usPacific.isDaylightTime(dt));
  172. assertEquals(420, usPacific.getOffset(dt));
  173. assertEquals('GMT-07:00', usPacific.getGMTString(dt));
  174. assertEquals('-0700', usPacific.getRFCTimeZoneString(dt));
  175. dt = new Date(2007, 12 - 1, 1);
  176. assertTrue(!usPacific.isDaylightTime(dt));
  177. assertEquals(480, usPacific.getOffset(dt));
  178. assertEquals('GMT-08:00', usPacific.getGMTString(dt));
  179. assertEquals('-0800', usPacific.getRFCTimeZoneString(dt));
  180. }
  181. function testSimpleTimeZonePositive() {
  182. var date = new Date();
  183. var simpleTimeZone = goog.i18n.TimeZone.createTimeZone(480);
  184. assertEquals(480, simpleTimeZone.getOffset(date));
  185. assertEquals('GMT-08:00', simpleTimeZone.getGMTString(date));
  186. assertEquals('Etc/GMT+8', simpleTimeZone.getTimeZoneId());
  187. assertEquals('UTC-8', simpleTimeZone.getLongName(date));
  188. assertEquals('UTC-8', simpleTimeZone.getShortName(date));
  189. assertEquals('-0800', simpleTimeZone.getRFCTimeZoneString(date));
  190. assertEquals('GMT-08:00', simpleTimeZone.getLongNameGMT(date));
  191. assertEquals('GMT-08:00', simpleTimeZone.getGenericLocation(date));
  192. assertEquals(false, simpleTimeZone.isDaylightTime(date));
  193. simpleTimeZone = goog.i18n.TimeZone.createTimeZone(630);
  194. assertEquals(630, simpleTimeZone.getOffset(date));
  195. assertEquals('GMT-10:30', simpleTimeZone.getGMTString(date));
  196. assertEquals('Etc/GMT+10:30', simpleTimeZone.getTimeZoneId());
  197. assertEquals('UTC-10:30', simpleTimeZone.getLongName(date));
  198. assertEquals('UTC-10:30', simpleTimeZone.getShortName(date));
  199. assertEquals('-1030', simpleTimeZone.getRFCTimeZoneString(date));
  200. assertEquals('GMT-10:30', simpleTimeZone.getLongNameGMT(date));
  201. assertEquals('GMT-10:30', simpleTimeZone.getGenericLocation(date));
  202. assertEquals(false, simpleTimeZone.isDaylightTime(date));
  203. }
  204. function testSimpleTimeZoneNegative() {
  205. var date = new Date();
  206. var simpleTimeZone = goog.i18n.TimeZone.createTimeZone(-480);
  207. assertEquals(-480, simpleTimeZone.getOffset(date));
  208. assertEquals('GMT+08:00', simpleTimeZone.getGMTString(date));
  209. assertEquals('Etc/GMT-8', simpleTimeZone.getTimeZoneId());
  210. assertEquals('UTC+8', simpleTimeZone.getLongName(date));
  211. assertEquals('UTC+8', simpleTimeZone.getShortName(date));
  212. assertEquals('+0800', simpleTimeZone.getRFCTimeZoneString(date));
  213. assertEquals('GMT+08:00', simpleTimeZone.getLongNameGMT(date));
  214. assertEquals('GMT+08:00', simpleTimeZone.getGenericLocation(date));
  215. assertEquals(false, simpleTimeZone.isDaylightTime(date));
  216. }
  217. function testSimpleTimeZoneZero() {
  218. var date = new Date();
  219. var simpleTimeZone = goog.i18n.TimeZone.createTimeZone(0);
  220. assertEquals(0, simpleTimeZone.getOffset(date));
  221. assertEquals('GMT+00:00', simpleTimeZone.getGMTString(date));
  222. assertEquals('Etc/GMT', simpleTimeZone.getTimeZoneId());
  223. assertEquals('UTC', simpleTimeZone.getLongName(date));
  224. assertEquals('UTC', simpleTimeZone.getShortName(date));
  225. assertEquals('+0000', simpleTimeZone.getRFCTimeZoneString(date));
  226. assertEquals('GMT+00:00', simpleTimeZone.getLongNameGMT(date));
  227. assertEquals('GMT+00:00', simpleTimeZone.getGenericLocation(date));
  228. assertEquals(false, simpleTimeZone.isDaylightTime(date));
  229. }