timezone.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. // -----
  2. // The `timezoneJS.Date` object gives you full-blown timezone support, independent from the timezone set on the end-user's machine running the browser. It uses the Olson zoneinfo files for its timezone data.
  3. //
  4. // The constructor function and setter methods use proxy JavaScript Date objects behind the scenes, so you can use strings like '10/22/2006' with the constructor. You also get the same sensible wraparound behavior with numeric parameters (like setting a value of 14 for the month wraps around to the next March).
  5. //
  6. // The other significant difference from the built-in JavaScript Date is that `timezoneJS.Date` also has named properties that store the values of year, month, date, etc., so it can be directly serialized to JSON and used for data transfer.
  7. /*
  8. * Copyright 2010 Matthew Eernisse (mde@fleegix.org)
  9. * and Open Source Applications Foundation
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. * Credits: Ideas included from incomplete JS implementation of Olson
  24. * parser, "XMLDAte" by Philippe Goetz (philippe.goetz@wanadoo.fr)
  25. *
  26. * Contributions:
  27. * Jan Niehusmann
  28. * Ricky Romero
  29. * Preston Hunt (prestonhunt@gmail.com)
  30. * Dov. B Katz (dov.katz@morganstanley.com)
  31. * Peter Bergström (pbergstr@mac.com)
  32. * Long Ho
  33. */
  34. (function () {
  35. // Standard initialization stuff to make sure the library is
  36. // usable on both client and server (node) side.
  37. "use strict";
  38. var root = this;
  39. var timezoneJS;
  40. if (typeof exports !== 'undefined') {
  41. timezoneJS = exports;
  42. } else {
  43. timezoneJS = root.timezoneJS = {};
  44. }
  45. timezoneJS.VERSION = '0.4.4';
  46. // Grab the ajax library from global context.
  47. // This can be jQuery, Zepto or fleegix.
  48. // You can also specify your own transport mechanism by declaring
  49. // `timezoneJS.timezone.transport` to a `function`. More details will follow
  50. var $ = root.$ || root.jQuery || root.Zepto
  51. , fleegix = root.fleegix
  52. , _arrIndexOf
  53. // Declare constant list of days and months. Unfortunately this doesn't leave room for i18n due to the Olson data being in English itself
  54. , DAYS = timezoneJS.Days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  55. , MONTHS = timezoneJS.Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  56. , SHORT_MONTHS = {}
  57. , SHORT_DAYS = {}
  58. , EXACT_DATE_TIME = {}
  59. , TZ_REGEXP = new RegExp('^[a-zA-Z]+/');
  60. //`{ "Jan": 0, "Feb": 1, "Mar": 2, "Apr": 3, "May": 4, "Jun": 5, "Jul": 6, "Aug": 7, "Sep": 8, "Oct": 9, "Nov": 10, "Dec": 11 }`
  61. for (var i = 0; i < MONTHS.length; i++) {
  62. SHORT_MONTHS[MONTHS[i].substr(0, 3)] = i;
  63. }
  64. //`{ "Sun": 0, "Mon": 1, "Tue": 2, "Wed": 3, "Thu": 4, "Fri": 5, "Sat": 6 }`
  65. for (i = 0; i < DAYS.length; i++) {
  66. SHORT_DAYS[DAYS[i].substr(0, 3)] = i;
  67. }
  68. //Handle array indexOf in IE
  69. //From https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
  70. //Extending Array prototype causes IE to iterate thru extra element
  71. _arrIndexOf = Array.prototype.indexOf || function (el) {
  72. if (this === null) {
  73. throw new TypeError();
  74. }
  75. var t = Object(this);
  76. var len = t.length >>> 0;
  77. if (len === 0) {
  78. return -1;
  79. }
  80. var n = 0;
  81. if (arguments.length > 1) {
  82. n = Number(arguments[1]);
  83. if (n != n) { // shortcut for verifying if it's NaN
  84. n = 0;
  85. } else if (n !== 0 && n !== Infinity && n !== -Infinity) {
  86. n = (n > 0 || -1) * Math.floor(Math.abs(n));
  87. }
  88. }
  89. if (n >= len) {
  90. return -1;
  91. }
  92. var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
  93. for (; k < len; k++) {
  94. if (k in t && t[k] === el) {
  95. return k;
  96. }
  97. }
  98. return -1;
  99. };
  100. // Format a number to the length = digits. For ex:
  101. //
  102. // `_fixWidth(2, 2) = '02'`
  103. //
  104. // `_fixWidth(1998, 2) = '98'`
  105. //
  106. // This is used to pad numbers in converting date to string in ISO standard.
  107. var _fixWidth = function (number, digits) {
  108. if (typeof number !== "number") { throw "not a number: " + number; }
  109. var s = number.toString();
  110. if (number.length > digits) {
  111. return number.substr(number.length - digits, number.length);
  112. }
  113. while (s.length < digits) {
  114. s = '0' + s;
  115. }
  116. return s;
  117. };
  118. // Abstraction layer for different transport layers, including fleegix/jQuery/Zepto
  119. //
  120. // Object `opts` include
  121. //
  122. // - `url`: url to ajax query
  123. //
  124. // - `async`: true for asynchronous, false otherwise. If false, return value will be response from URL. This is true by default
  125. //
  126. // - `success`: success callback function
  127. //
  128. // - `error`: error callback function
  129. // Returns response from URL if async is false, otherwise the AJAX request object itself
  130. var _transport = function (opts) {
  131. if ((!fleegix || typeof fleegix.xhr === 'undefined') && (!$ || typeof $.ajax === 'undefined')) {
  132. throw new Error('Please use the Fleegix.js XHR module, jQuery ajax, Zepto ajax, or define your own transport mechanism for downloading zone files.');
  133. }
  134. if (!opts) return;
  135. if (!opts.url) throw new Error ('URL must be specified');
  136. if (!('async' in opts)) opts.async = true;
  137. if (!opts.async) {
  138. return fleegix && fleegix.xhr
  139. ? fleegix.xhr.doReq({ url: opts.url, async: false })
  140. : $.ajax({ url : opts.url, async : false }).responseText;
  141. }
  142. return fleegix && fleegix.xhr
  143. ? fleegix.xhr.send({
  144. url : opts.url,
  145. method : 'get',
  146. handleSuccess : opts.success,
  147. handleErr : opts.error
  148. })
  149. : $.ajax({
  150. url : opts.url,
  151. dataType: 'text',
  152. method : 'GET',
  153. error : opts.error,
  154. success : opts.success
  155. });
  156. };
  157. // Constructor, which is similar to that of the native Date object itself
  158. timezoneJS.Date = function () {
  159. var args = Array.prototype.slice.apply(arguments)
  160. , dt = null
  161. , tz = null
  162. , arr = [];
  163. //We support several different constructors, including all the ones from `Date` object
  164. // with a timezone string at the end.
  165. //
  166. //- `[tz]`: Returns object with time in `tz` specified.
  167. //
  168. // - `utcMillis`, `[tz]`: Return object with UTC time = `utcMillis`, in `tz`.
  169. //
  170. // - `Date`, `[tz]`: Returns object with UTC time = `Date.getTime()`, in `tz`.
  171. //
  172. // - `year, month, [date,] [hours,] [minutes,] [seconds,] [millis,] [tz]: Same as `Date` object
  173. // with tz.
  174. //
  175. // - `Array`: Can be any combo of the above.
  176. //
  177. //If 1st argument is an array, we can use it as a list of arguments itself
  178. if (Object.prototype.toString.call(args[0]) === '[object Array]') {
  179. args = args[0];
  180. }
  181. if (typeof args[args.length - 1] === 'string' && TZ_REGEXP.test(args[args.length - 1])) {
  182. tz = args.pop();
  183. }
  184. switch (args.length) {
  185. case 0:
  186. dt = new Date();
  187. break;
  188. case 1:
  189. dt = new Date(args[0]);
  190. break;
  191. default:
  192. for (var i = 0; i < 7; i++) {
  193. arr[i] = args[i] || 0;
  194. }
  195. dt = new Date(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6]);
  196. break;
  197. }
  198. this._useCache = false;
  199. this._tzInfo = {};
  200. this._day = 0;
  201. this.year = 0;
  202. this.month = 0;
  203. this.date = 0;
  204. this.hours = 0;
  205. this.minutes = 0;
  206. this.seconds = 0;
  207. this.milliseconds = 0;
  208. this.timezone = tz || null;
  209. //Tricky part:
  210. // For the cases where there are 1/2 arguments: `timezoneJS.Date(millis, [tz])` and `timezoneJS.Date(Date, [tz])`. The
  211. // Date `dt` created should be in UTC. Thus the way I detect such cases is to determine if `arr` is not populated & `tz`
  212. // is specified. Because if `tz` is not specified, `dt` can be in local time.
  213. if (arr.length) {
  214. this.setFromDateObjProxy(dt);
  215. } else {
  216. this.setFromTimeProxy(dt.getTime(), tz);
  217. }
  218. };
  219. // Implements most of the native Date object
  220. timezoneJS.Date.prototype = {
  221. getDate: function () { return this.date; },
  222. getDay: function () { return this._day; },
  223. getFullYear: function () { return this.year; },
  224. getMonth: function () { return this.month; },
  225. getYear: function () { return this.year - 1900; },
  226. getHours: function () { return this.hours; },
  227. getMilliseconds: function () { return this.milliseconds; },
  228. getMinutes: function () { return this.minutes; },
  229. getSeconds: function () { return this.seconds; },
  230. getUTCDate: function () { return this.getUTCDateProxy().getUTCDate(); },
  231. getUTCDay: function () { return this.getUTCDateProxy().getUTCDay(); },
  232. getUTCFullYear: function () { return this.getUTCDateProxy().getUTCFullYear(); },
  233. getUTCHours: function () { return this.getUTCDateProxy().getUTCHours(); },
  234. getUTCMilliseconds: function () { return this.getUTCDateProxy().getUTCMilliseconds(); },
  235. getUTCMinutes: function () { return this.getUTCDateProxy().getUTCMinutes(); },
  236. getUTCMonth: function () { return this.getUTCDateProxy().getUTCMonth(); },
  237. getUTCSeconds: function () { return this.getUTCDateProxy().getUTCSeconds(); },
  238. // Time adjusted to user-specified timezone
  239. getTime: function () {
  240. return this._timeProxy + (this.getTimezoneOffset() * 60 * 1000);
  241. },
  242. getTimezone: function () { return this.timezone; },
  243. getTimezoneOffset: function () { return this.getTimezoneInfo().tzOffset; },
  244. getTimezoneAbbreviation: function () { return this.getTimezoneInfo().tzAbbr; },
  245. getTimezoneInfo: function () {
  246. if (this._useCache) return this._tzInfo;
  247. var res;
  248. // If timezone is specified, get the correct timezone info based on the Date given
  249. if (this.timezone) {
  250. res = this.timezone === 'Etc/UTC' || this.timezone === 'Etc/GMT'
  251. ? { tzOffset: 0, tzAbbr: 'UTC' }
  252. : timezoneJS.timezone.getTzInfo(this._timeProxy, this.timezone);
  253. }
  254. // If no timezone was specified, use the local browser offset
  255. else {
  256. res = { tzOffset: this.getLocalOffset(), tzAbbr: null };
  257. }
  258. this._tzInfo = res;
  259. this._useCache = true;
  260. return res;
  261. },
  262. getUTCDateProxy: function () {
  263. var dt = new Date(this._timeProxy);
  264. dt.setUTCMinutes(dt.getUTCMinutes() + this.getTimezoneOffset());
  265. return dt;
  266. },
  267. setDate: function (date) {
  268. this.setAttribute('date', date);
  269. return this.getTime();
  270. },
  271. setFullYear: function (year, month, date) {
  272. if (date !== undefined) { this.setAttribute('date', 1); }
  273. this.setAttribute('year', year);
  274. if (month !== undefined) { this.setAttribute('month', month); }
  275. if (date !== undefined) { this.setAttribute('date', date); }
  276. return this.getTime();
  277. },
  278. setMonth: function (month, date) {
  279. this.setAttribute('month', month);
  280. if (date !== undefined) { this.setAttribute('date', date); }
  281. return this.getTime();
  282. },
  283. setYear: function (year) {
  284. year = Number(year);
  285. if (0 <= year && year <= 99) { year += 1900; }
  286. this.setUTCAttribute('year', year);
  287. return this.getTime();
  288. },
  289. setHours: function (hours, minutes, seconds, milliseconds) {
  290. this.setAttribute('hours', hours);
  291. if (minutes !== undefined) { this.setAttribute('minutes', minutes); }
  292. if (seconds !== undefined) { this.setAttribute('seconds', seconds); }
  293. if (milliseconds !== undefined) { this.setAttribute('milliseconds', milliseconds); }
  294. return this.getTime();
  295. },
  296. setMinutes: function (minutes, seconds, milliseconds) {
  297. this.setAttribute('minutes', minutes);
  298. if (seconds !== undefined) { this.setAttribute('seconds', seconds); }
  299. if (milliseconds !== undefined) { this.setAttribute('milliseconds', milliseconds); }
  300. return this.getTime();
  301. },
  302. setSeconds: function (seconds, milliseconds) {
  303. this.setAttribute('seconds', seconds);
  304. if (milliseconds !== undefined) { this.setAttribute('milliseconds', milliseconds); }
  305. return this.getTime();
  306. },
  307. setMilliseconds: function (milliseconds) {
  308. this.setAttribute('milliseconds', milliseconds);
  309. return this.getTime();
  310. },
  311. setTime: function (n) {
  312. if (isNaN(n)) { throw new Error('Units must be a number.'); }
  313. this.setFromTimeProxy(n, this.timezone);
  314. return this.getTime();
  315. },
  316. setUTCFullYear: function (year, month, date) {
  317. if (date !== undefined) { this.setUTCAttribute('date', 1); }
  318. this.setUTCAttribute('year', year);
  319. if (month !== undefined) { this.setUTCAttribute('month', month); }
  320. if (date !== undefined) { this.setUTCAttribute('date', date); }
  321. return this.getTime();
  322. },
  323. setUTCMonth: function (month, date) {
  324. this.setUTCAttribute('month', month);
  325. if (date !== undefined) { this.setUTCAttribute('date', date); }
  326. return this.getTime();
  327. },
  328. setUTCDate: function (date) {
  329. this.setUTCAttribute('date', date);
  330. return this.getTime();
  331. },
  332. setUTCHours: function (hours, minutes, seconds, milliseconds) {
  333. this.setUTCAttribute('hours', hours);
  334. if (minutes !== undefined) { this.setUTCAttribute('minutes', minutes); }
  335. if (seconds !== undefined) { this.setUTCAttribute('seconds', seconds); }
  336. if (milliseconds !== undefined) { this.setUTCAttribute('milliseconds', milliseconds); }
  337. return this.getTime();
  338. },
  339. setUTCMinutes: function (minutes, seconds, milliseconds) {
  340. this.setUTCAttribute('minutes', minutes);
  341. if (seconds !== undefined) { this.setUTCAttribute('seconds', seconds); }
  342. if (milliseconds !== undefined) { this.setUTCAttribute('milliseconds', milliseconds); }
  343. return this.getTime();
  344. },
  345. setUTCSeconds: function (seconds, milliseconds) {
  346. this.setUTCAttribute('seconds', seconds);
  347. if (milliseconds !== undefined) { this.setUTCAttribute('milliseconds', milliseconds); }
  348. return this.getTime();
  349. },
  350. setUTCMilliseconds: function (milliseconds) {
  351. this.setUTCAttribute('milliseconds', milliseconds);
  352. return this.getTime();
  353. },
  354. setFromDateObjProxy: function (dt) {
  355. this.year = dt.getFullYear();
  356. this.month = dt.getMonth();
  357. this.date = dt.getDate();
  358. this.hours = dt.getHours();
  359. this.minutes = dt.getMinutes();
  360. this.seconds = dt.getSeconds();
  361. this.milliseconds = dt.getMilliseconds();
  362. this._day = dt.getDay();
  363. this._dateProxy = dt;
  364. this._timeProxy = Date.UTC(this.year, this.month, this.date, this.hours, this.minutes, this.seconds, this.milliseconds);
  365. this._useCache = false;
  366. },
  367. setFromTimeProxy: function (utcMillis, tz) {
  368. var dt = new Date(utcMillis);
  369. var tzOffset;
  370. tzOffset = tz ? timezoneJS.timezone.getTzInfo(dt, tz).tzOffset : dt.getTimezoneOffset();
  371. dt.setTime(utcMillis + (dt.getTimezoneOffset() - tzOffset) * 60000);
  372. this.setFromDateObjProxy(dt);
  373. },
  374. setAttribute: function (unit, n) {
  375. if (isNaN(n)) { throw new Error('Units must be a number.'); }
  376. var dt = this._dateProxy;
  377. var meth = unit === 'year' ? 'FullYear' : unit.substr(0, 1).toUpperCase() + unit.substr(1);
  378. dt['set' + meth](n);
  379. this.setFromDateObjProxy(dt);
  380. },
  381. setUTCAttribute: function (unit, n) {
  382. if (isNaN(n)) { throw new Error('Units must be a number.'); }
  383. var meth = unit === 'year' ? 'FullYear' : unit.substr(0, 1).toUpperCase() + unit.substr(1);
  384. var dt = this.getUTCDateProxy();
  385. dt['setUTC' + meth](n);
  386. dt.setUTCMinutes(dt.getUTCMinutes() - this.getTimezoneOffset());
  387. this.setFromTimeProxy(dt.getTime() + this.getTimezoneOffset() * 60000, this.timezone);
  388. },
  389. setTimezone: function (tz) {
  390. var previousOffset = this.getTimezoneInfo().tzOffset;
  391. this.timezone = tz;
  392. this._useCache = false;
  393. // Set UTC minutes offsets by the delta of the two timezones
  394. this.setUTCMinutes(this.getUTCMinutes() - this.getTimezoneInfo().tzOffset + previousOffset);
  395. },
  396. removeTimezone: function () {
  397. this.timezone = null;
  398. this._useCache = false;
  399. },
  400. valueOf: function () { return this.getTime(); },
  401. clone: function () {
  402. return this.timezone ? new timezoneJS.Date(this.getTime(), this.timezone) : new timezoneJS.Date(this.getTime());
  403. },
  404. toGMTString: function () { return this.toString('EEE, dd MMM yyyy HH:mm:ss Z', 'Etc/GMT'); },
  405. toLocaleString: function () {},
  406. toLocaleDateString: function () {},
  407. toLocaleTimeString: function () {},
  408. toSource: function () {},
  409. toISOString: function () { return this.toString('yyyy-MM-ddTHH:mm:ss.SSS', 'Etc/UTC') + 'Z'; },
  410. toJSON: function () { return this.toISOString(); },
  411. // Allows different format following ISO8601 format:
  412. toString: function (format, tz) {
  413. // Default format is the same as toISOString
  414. if (!format) format = 'yyyy-MM-dd HH:mm:ss';
  415. var result = format;
  416. var tzInfo = tz ? timezoneJS.timezone.getTzInfo(this.getTime(), tz) : this.getTimezoneInfo();
  417. var _this = this;
  418. // If timezone is specified, get a clone of the current Date object and modify it
  419. if (tz) {
  420. _this = this.clone();
  421. _this.setTimezone(tz);
  422. }
  423. var hours = _this.getHours();
  424. return result
  425. // fix the same characters in Month names
  426. .replace(/a+/g, function () { return 'k'; })
  427. // `y`: year
  428. .replace(/y+/g, function (token) { return _fixWidth(_this.getFullYear(), token.length); })
  429. // `d`: date
  430. .replace(/d+/g, function (token) { return _fixWidth(_this.getDate(), token.length); })
  431. // `m`: minute
  432. .replace(/m+/g, function (token) { return _fixWidth(_this.getMinutes(), token.length); })
  433. // `s`: second
  434. .replace(/s+/g, function (token) { return _fixWidth(_this.getSeconds(), token.length); })
  435. // `S`: millisecond
  436. .replace(/S+/g, function (token) { return _fixWidth(_this.getMilliseconds(), token.length); })
  437. // `M`: month. Note: `MM` will be the numeric representation (e.g February is 02) but `MMM` will be text representation (e.g February is Feb)
  438. .replace(/M+/g, function (token) {
  439. var _month = _this.getMonth(),
  440. _len = token.length;
  441. if (_len > 3) {
  442. return timezoneJS.Months[_month];
  443. } else if (_len > 2) {
  444. return timezoneJS.Months[_month].substring(0, _len);
  445. }
  446. return _fixWidth(_month + 1, _len);
  447. })
  448. // `k`: AM/PM
  449. .replace(/k+/g, function () {
  450. if (hours >= 12) {
  451. if (hours > 12) {
  452. hours -= 12;
  453. }
  454. return 'PM';
  455. }
  456. return 'AM';
  457. })
  458. // `H`: hour
  459. .replace(/H+/g, function (token) { return _fixWidth(hours, token.length); })
  460. // `E`: day
  461. .replace(/E+/g, function (token) { return DAYS[_this.getDay()].substring(0, token.length); })
  462. // `Z`: timezone abbreviation
  463. .replace(/Z+/gi, function () { return tzInfo.tzAbbr; });
  464. },
  465. toUTCString: function () { return this.toGMTString(); },
  466. civilToJulianDayNumber: function (y, m, d) {
  467. var a;
  468. // Adjust for zero-based JS-style array
  469. m++;
  470. if (m > 12) {
  471. a = parseInt(m/12, 10);
  472. m = m % 12;
  473. y += a;
  474. }
  475. if (m <= 2) {
  476. y -= 1;
  477. m += 12;
  478. }
  479. a = Math.floor(y / 100);
  480. var b = 2 - a + Math.floor(a / 4)
  481. , jDt = Math.floor(365.25 * (y + 4716)) + Math.floor(30.6001 * (m + 1)) + d + b - 1524;
  482. return jDt;
  483. },
  484. getLocalOffset: function () {
  485. return this._dateProxy.getTimezoneOffset();
  486. }
  487. };
  488. timezoneJS.timezone = new function () {
  489. var _this = this
  490. , regionMap = {'Etc':'etcetera','EST':'northamerica','MST':'northamerica','HST':'northamerica','EST5EDT':'northamerica','CST6CDT':'northamerica','MST7MDT':'northamerica','PST8PDT':'northamerica','America':'northamerica','Pacific':'australasia','Atlantic':'europe','Africa':'africa','Indian':'africa','Antarctica':'antarctica','Asia':'asia','Australia':'australasia','Europe':'europe','WET':'europe','CET':'europe','MET':'europe','EET':'europe'}
  491. , regionExceptions = {'Pacific/Honolulu':'northamerica','Atlantic/Bermuda':'northamerica','Atlantic/Cape_Verde':'africa','Atlantic/St_Helena':'africa','Indian/Kerguelen':'antarctica','Indian/Chagos':'asia','Indian/Maldives':'asia','Indian/Christmas':'australasia','Indian/Cocos':'australasia','America/Danmarkshavn':'europe','America/Scoresbysund':'europe','America/Godthab':'europe','America/Thule':'europe','Asia/Yekaterinburg':'europe','Asia/Omsk':'europe','Asia/Novosibirsk':'europe','Asia/Krasnoyarsk':'europe','Asia/Irkutsk':'europe','Asia/Yakutsk':'europe','Asia/Vladivostok':'europe','Asia/Sakhalin':'europe','Asia/Magadan':'europe','Asia/Kamchatka':'europe','Asia/Anadyr':'europe','Africa/Ceuta':'europe','America/Argentina/Buenos_Aires':'southamerica','America/Argentina/Cordoba':'southamerica','America/Argentina/Tucuman':'southamerica','America/Argentina/La_Rioja':'southamerica','America/Argentina/San_Juan':'southamerica','America/Argentina/Jujuy':'southamerica','America/Argentina/Catamarca':'southamerica','America/Argentina/Mendoza':'southamerica','America/Argentina/Rio_Gallegos':'southamerica','America/Argentina/Ushuaia':'southamerica','America/Aruba':'southamerica','America/La_Paz':'southamerica','America/Noronha':'southamerica','America/Belem':'southamerica','America/Fortaleza':'southamerica','America/Recife':'southamerica','America/Araguaina':'southamerica','America/Maceio':'southamerica','America/Bahia':'southamerica','America/Sao_Paulo':'southamerica','America/Campo_Grande':'southamerica','America/Cuiaba':'southamerica','America/Porto_Velho':'southamerica','America/Boa_Vista':'southamerica','America/Manaus':'southamerica','America/Eirunepe':'southamerica','America/Rio_Branco':'southamerica','America/Santiago':'southamerica','Pacific/Easter':'southamerica','America/Bogota':'southamerica','America/Curacao':'southamerica','America/Guayaquil':'southamerica','Pacific/Galapagos':'southamerica','Atlantic/Stanley':'southamerica','America/Cayenne':'southamerica','America/Guyana':'southamerica','America/Asuncion':'southamerica','America/Lima':'southamerica','Atlantic/South_Georgia':'southamerica','America/Paramaribo':'southamerica','America/Port_of_Spain':'southamerica','America/Montevideo':'southamerica','America/Caracas':'southamerica'};
  492. function invalidTZError(t) { throw new Error('Timezone "' + t + '" is either incorrect, or not loaded in the timezone registry.'); }
  493. function builtInLoadZoneFile(fileName, opts) {
  494. var url = _this.zoneFileBasePath + '/' + fileName;
  495. return !opts || !opts.async
  496. ? _this.parseZones(_this.transport({ url : url, async : false }))
  497. : _this.transport({
  498. async: true,
  499. url : url,
  500. success : function (str) {
  501. if (_this.parseZones(str) && typeof opts.callback === 'function') {
  502. opts.callback();
  503. }
  504. return true;
  505. },
  506. error : function () {
  507. throw new Error('Error retrieving "' + url + '" zoneinfo files');
  508. }
  509. });
  510. }
  511. function getRegionForTimezone(tz) {
  512. var exc = regionExceptions[tz]
  513. , reg
  514. , ret;
  515. if (exc) return exc;
  516. reg = tz.split('/')[0];
  517. ret = regionMap[reg];
  518. // If there's nothing listed in the main regions for this TZ, check the 'backward' links
  519. if (ret) return ret;
  520. var link = _this.zones[tz];
  521. if (typeof link === 'string') {
  522. return getRegionForTimezone(link);
  523. }
  524. // Backward-compat file hasn't loaded yet, try looking in there
  525. if (!_this.loadedZones.backward) {
  526. // This is for obvious legacy zones (e.g., Iceland) that don't even have a prefix like "America/" that look like normal zones
  527. _this.loadZoneFile('backward');
  528. return getRegionForTimezone(tz);
  529. }
  530. invalidTZError(tz);
  531. }
  532. function parseTimeString(str) {
  533. var pat = /(\d+)(?::0*(\d*))?(?::0*(\d*))?([wsugz])?$/;
  534. var hms = str.match(pat);
  535. hms[1] = parseInt(hms[1], 10);
  536. hms[2] = hms[2] ? parseInt(hms[2], 10) : 0;
  537. hms[3] = hms[3] ? parseInt(hms[3], 10) : 0;
  538. return hms;
  539. }
  540. function processZone(z) {
  541. if (!z[3]) { return; }
  542. var yea = parseInt(z[3], 10);
  543. var mon = 11;
  544. var dat = 31;
  545. if (z[4]) {
  546. mon = SHORT_MONTHS[z[4].substr(0, 3)];
  547. dat = parseInt(z[5], 10) || 1;
  548. }
  549. var string = z[6] ? z[6] : '00:00:00'
  550. , t = parseTimeString(string);
  551. return [yea, mon, dat, t[1], t[2], t[3]];
  552. }
  553. function getZone(dt, tz) {
  554. var utcMillis = typeof dt === 'number' ? dt : new Date(dt).getTime();
  555. var t = tz;
  556. var zoneList = _this.zones[t];
  557. // Follow links to get to an actual zone
  558. while (typeof zoneList === "string") {
  559. t = zoneList;
  560. zoneList = _this.zones[t];
  561. }
  562. if (!zoneList) {
  563. // Backward-compat file hasn't loaded yet, try looking in there
  564. if (!_this.loadedZones.backward) {
  565. //This is for backward entries like "America/Fort_Wayne" that
  566. // getRegionForTimezone *thinks* it has a region file and zone
  567. // for (e.g., America => 'northamerica'), but in reality it's a
  568. // legacy zone we need the backward file for.
  569. _this.loadZoneFile('backward');
  570. return getZone(dt, tz);
  571. }
  572. invalidTZError(t);
  573. }
  574. if (zoneList.length === 0) {
  575. throw new Error('No Zone found for "' + tz + '" on ' + dt);
  576. }
  577. //Do backwards lookup since most use cases deal with newer dates.
  578. for (var i = zoneList.length - 1; i >= 0; i--) {
  579. var z = zoneList[i];
  580. if (z[3] && utcMillis > z[3]) break;
  581. }
  582. return zoneList[i+1];
  583. }
  584. function getBasicOffset(time) {
  585. var off = parseTimeString(time)
  586. , adj = time.charAt(0) === '-' ? -1 : 1;
  587. off = adj * (((off[1] * 60 + off[2]) * 60 + off[3]) * 1000);
  588. return off/60/1000;
  589. }
  590. //if isUTC is true, date is given in UTC, otherwise it's given
  591. // in local time (ie. date.getUTC*() returns local time components)
  592. function getRule(dt, zone, isUTC) {
  593. var date = typeof dt === 'number' ? new Date(dt) : dt;
  594. var ruleset = zone[1];
  595. var basicOffset = zone[0];
  596. // If the zone has a DST rule like '1:00', create a rule and return it
  597. // instead of looking it up in the parsed rules
  598. var staticDstMatch = ruleset.match(/^([0-9]):([0-9][0-9])$/);
  599. if (staticDstMatch) {
  600. return [-1000000,'max','-','Jan',1,parseTimeString('0:00'),parseInt(staticDstMatch[1]) * 60 + parseInt(staticDstMatch[2]), '-'];
  601. }
  602. //Convert a date to UTC. Depending on the 'type' parameter, the date
  603. // parameter may be:
  604. //
  605. // - `u`, `g`, `z`: already UTC (no adjustment).
  606. //
  607. // - `s`: standard time (adjust for time zone offset but not for DST)
  608. //
  609. // - `w`: wall clock time (adjust for both time zone and DST offset).
  610. //
  611. // DST adjustment is done using the rule given as third argument.
  612. var convertDateToUTC = function (date, type, rule) {
  613. var offset = 0;
  614. if (type === 'u' || type === 'g' || type === 'z') { // UTC
  615. offset = 0;
  616. } else if (type === 's') { // Standard Time
  617. offset = basicOffset;
  618. } else if (type === 'w' || !type) { // Wall Clock Time
  619. offset = getAdjustedOffset(basicOffset, rule);
  620. } else {
  621. throw("unknown type " + type);
  622. }
  623. offset *= 60 * 1000; // to millis
  624. return new Date(date.getTime() + offset);
  625. };
  626. //Step 1: Find applicable rules for this year.
  627. //
  628. //Step 2: Sort the rules by effective date.
  629. //
  630. //Step 3: Check requested date to see if a rule has yet taken effect this year. If not,
  631. //
  632. //Step 4: Get the rules for the previous year. If there isn't an applicable rule for last year, then
  633. // there probably is no current time offset since they seem to explicitly turn off the offset
  634. // when someone stops observing DST.
  635. //
  636. // FIXME if this is not the case and we'll walk all the way back (ugh).
  637. //
  638. //Step 5: Sort the rules by effective date.
  639. //Step 6: Apply the most recent rule before the current time.
  640. var convertRuleToExactDateAndTime = function (yearAndRule, prevRule) {
  641. var year = yearAndRule[0]
  642. , rule = yearAndRule[1];
  643. // Assume that the rule applies to the year of the given date.
  644. var hms = rule[5];
  645. var effectiveDate;
  646. if (!EXACT_DATE_TIME[year])
  647. EXACT_DATE_TIME[year] = {};
  648. // Result for given parameters is already stored
  649. if (EXACT_DATE_TIME[year][rule])
  650. effectiveDate = EXACT_DATE_TIME[year][rule];
  651. else {
  652. //If we have a specific date, use that!
  653. if (!isNaN(rule[4])) {
  654. effectiveDate = new Date(Date.UTC(year, SHORT_MONTHS[rule[3]], rule[4], hms[1], hms[2], hms[3], 0));
  655. }
  656. //Let's hunt for the date.
  657. else {
  658. var targetDay
  659. , operator;
  660. //Example: `lastThu`
  661. if (rule[4].substr(0, 4) === "last") {
  662. // Start at the last day of the month and work backward.
  663. effectiveDate = new Date(Date.UTC(year, SHORT_MONTHS[rule[3]] + 1, 1, hms[1] - 24, hms[2], hms[3], 0));
  664. targetDay = SHORT_DAYS[rule[4].substr(4, 3)];
  665. operator = "<=";
  666. }
  667. //Example: `Sun>=15`
  668. else {
  669. //Start at the specified date.
  670. effectiveDate = new Date(Date.UTC(year, SHORT_MONTHS[rule[3]], rule[4].substr(5), hms[1], hms[2], hms[3], 0));
  671. targetDay = SHORT_DAYS[rule[4].substr(0, 3)];
  672. operator = rule[4].substr(3, 2);
  673. }
  674. var ourDay = effectiveDate.getUTCDay();
  675. //Go forwards.
  676. if (operator === ">=") {
  677. effectiveDate.setUTCDate(effectiveDate.getUTCDate() + (targetDay - ourDay + ((targetDay < ourDay) ? 7 : 0)));
  678. }
  679. //Go backwards. Looking for the last of a certain day, or operator is "<=" (less likely).
  680. else {
  681. effectiveDate.setUTCDate(effectiveDate.getUTCDate() + (targetDay - ourDay - ((targetDay > ourDay) ? 7 : 0)));
  682. }
  683. }
  684. EXACT_DATE_TIME[year][rule] = effectiveDate;
  685. }
  686. //If previous rule is given, correct for the fact that the starting time of the current
  687. // rule may be specified in local time.
  688. if (prevRule) {
  689. effectiveDate = convertDateToUTC(effectiveDate, hms[4], prevRule);
  690. }
  691. return effectiveDate;
  692. };
  693. var findApplicableRules = function (year, ruleset) {
  694. var applicableRules = [];
  695. for (var i = 0; ruleset && i < ruleset.length; i++) {
  696. //Exclude future rules.
  697. if (ruleset[i][0] <= year &&
  698. (
  699. // Date is in a set range.
  700. ruleset[i][1] >= year ||
  701. // Date is in an "only" year.
  702. (ruleset[i][0] === year && ruleset[i][1] === "only") ||
  703. //We're in a range from the start year to infinity.
  704. ruleset[i][1] === "max"
  705. )
  706. ) {
  707. //It's completely okay to have any number of matches here.
  708. // Normally we should only see two, but that doesn't preclude other numbers of matches.
  709. // These matches are applicable to this year.
  710. applicableRules.push([year, ruleset[i]]);
  711. }
  712. }
  713. return applicableRules;
  714. };
  715. var compareDates = function (a, b, prev) {
  716. var year, rule;
  717. if (a.constructor !== Date) {
  718. year = a[0];
  719. rule = a[1];
  720. a = (!prev && EXACT_DATE_TIME[year] && EXACT_DATE_TIME[year][rule])
  721. ? EXACT_DATE_TIME[year][rule]
  722. : convertRuleToExactDateAndTime(a, prev);
  723. } else if (prev) {
  724. a = convertDateToUTC(a, isUTC ? 'u' : 'w', prev);
  725. }
  726. if (b.constructor !== Date) {
  727. year = b[0];
  728. rule = b[1];
  729. b = (!prev && EXACT_DATE_TIME[year] && EXACT_DATE_TIME[year][rule]) ? EXACT_DATE_TIME[year][rule]
  730. : convertRuleToExactDateAndTime(b, prev);
  731. } else if (prev) {
  732. b = convertDateToUTC(b, isUTC ? 'u' : 'w', prev);
  733. }
  734. a = Number(a);
  735. b = Number(b);
  736. return a - b;
  737. };
  738. var year = date.getUTCFullYear();
  739. var applicableRules;
  740. applicableRules = findApplicableRules(year, _this.rules[ruleset]);
  741. applicableRules.push(date);
  742. //While sorting, the time zone in which the rule starting time is specified
  743. // is ignored. This is ok as long as the timespan between two DST changes is
  744. // larger than the DST offset, which is probably always true.
  745. // As the given date may indeed be close to a DST change, it may get sorted
  746. // to a wrong position (off by one), which is corrected below.
  747. applicableRules.sort(compareDates);
  748. //If there are not enough past DST rules...
  749. if (_arrIndexOf.call(applicableRules, date) < 2) {
  750. applicableRules = applicableRules.concat(findApplicableRules(year-1, _this.rules[ruleset]));
  751. applicableRules.sort(compareDates);
  752. }
  753. var pinpoint = _arrIndexOf.call(applicableRules, date);
  754. if (pinpoint > 1 && compareDates(date, applicableRules[pinpoint-1], applicableRules[pinpoint-2][1]) < 0) {
  755. //The previous rule does not really apply, take the one before that.
  756. return applicableRules[pinpoint - 2][1];
  757. } else if (pinpoint > 0 && pinpoint < applicableRules.length - 1 && compareDates(date, applicableRules[pinpoint+1], applicableRules[pinpoint-1][1]) > 0) {
  758. //The next rule does already apply, take that one.
  759. return applicableRules[pinpoint + 1][1];
  760. } else if (pinpoint === 0) {
  761. //No applicable rule found in this and in previous year.
  762. return null;
  763. }
  764. return applicableRules[pinpoint - 1][1];
  765. }
  766. function getAdjustedOffset(off, rule) {
  767. return -Math.ceil(rule[6] - off);
  768. }
  769. function getAbbreviation(zone, rule) {
  770. var res;
  771. var base = zone[2];
  772. if (base.indexOf('%s') > -1) {
  773. var repl;
  774. if (rule) {
  775. repl = rule[7] === '-' ? '' : rule[7];
  776. }
  777. //FIXME: Right now just falling back to Standard --
  778. // apparently ought to use the last valid rule,
  779. // although in practice that always ought to be Standard
  780. else {
  781. repl = 'S';
  782. }
  783. res = base.replace('%s', repl);
  784. }
  785. else if (base.indexOf('/') > -1) {
  786. //Chose one of two alternative strings.
  787. res = base.split("/", 2)[rule[6] ? 1 : 0];
  788. } else {
  789. res = base;
  790. }
  791. return res;
  792. }
  793. this.zoneFileBasePath = null;
  794. this.zoneFiles = ['africa', 'antarctica', 'asia', 'australasia', 'backward', 'etcetera', 'europe', 'northamerica', 'pacificnew', 'southamerica'];
  795. this.loadingSchemes = {
  796. PRELOAD_ALL: 'preloadAll',
  797. LAZY_LOAD: 'lazyLoad',
  798. MANUAL_LOAD: 'manualLoad'
  799. };
  800. this.loadingScheme = this.loadingSchemes.LAZY_LOAD;
  801. this.loadedZones = {};
  802. this.zones = {};
  803. this.rules = {};
  804. this.init = function (o) {
  805. var opts = { async: true }
  806. , def = this.loadingScheme === this.loadingSchemes.PRELOAD_ALL
  807. ? this.zoneFiles
  808. : (this.defaultZoneFile || 'northamerica')
  809. , done = 0
  810. , callbackFn;
  811. //Override default with any passed-in opts
  812. for (var p in o) {
  813. opts[p] = o[p];
  814. }
  815. if (typeof def === 'string') {
  816. return this.loadZoneFile(def, opts);
  817. }
  818. //Wraps callback function in another one that makes
  819. // sure all files have been loaded.
  820. callbackFn = opts.callback;
  821. opts.callback = function () {
  822. done++;
  823. (done === def.length) && typeof callbackFn === 'function' && callbackFn();
  824. };
  825. for (var i = 0; i < def.length; i++) {
  826. this.loadZoneFile(def[i], opts);
  827. }
  828. };
  829. //Get the zone files via XHR -- if the sync flag
  830. // is set to true, it's being called by the lazy-loading
  831. // mechanism, so the result needs to be returned inline.
  832. this.loadZoneFile = function (fileName, opts) {
  833. if (typeof this.zoneFileBasePath === 'undefined') {
  834. throw new Error('Please define a base path to your zone file directory -- timezoneJS.timezone.zoneFileBasePath.');
  835. }
  836. //Ignore already loaded zones.
  837. if (this.loadedZones[fileName]) {
  838. return;
  839. }
  840. this.loadedZones[fileName] = true;
  841. return builtInLoadZoneFile(fileName, opts);
  842. };
  843. this.loadZoneJSONData = function (url, sync) {
  844. var processData = function (data) {
  845. data = eval('('+ data +')');
  846. for (var z in data.zones) {
  847. _this.zones[z] = data.zones[z];
  848. }
  849. for (var r in data.rules) {
  850. _this.rules[r] = data.rules[r];
  851. }
  852. };
  853. return sync
  854. ? processData(_this.transport({ url : url, async : false }))
  855. : _this.transport({ url : url, success : processData });
  856. };
  857. this.loadZoneDataFromObject = function (data) {
  858. if (!data) { return; }
  859. for (var z in data.zones) {
  860. _this.zones[z] = data.zones[z];
  861. }
  862. for (var r in data.rules) {
  863. _this.rules[r] = data.rules[r];
  864. }
  865. };
  866. this.getAllZones = function () {
  867. var arr = [];
  868. for (var z in this.zones) { arr.push(z); }
  869. return arr.sort();
  870. };
  871. this.parseZones = function (str) {
  872. var lines = str.split('\n')
  873. , arr = []
  874. , chunk = ''
  875. , l
  876. , zone = null
  877. , rule = null;
  878. for (var i = 0; i < lines.length; i++) {
  879. l = lines[i];
  880. if (l.match(/^\s/)) {
  881. l = "Zone " + zone + l;
  882. }
  883. l = l.split("#")[0];
  884. if (l.length > 3) {
  885. arr = l.split(/\s+/);
  886. chunk = arr.shift();
  887. //Ignore Leap.
  888. switch (chunk) {
  889. case 'Zone':
  890. zone = arr.shift();
  891. if (!_this.zones[zone]) {
  892. _this.zones[zone] = [];
  893. }
  894. if (arr.length < 3) break;
  895. //Process zone right here and replace 3rd element with the processed array.
  896. arr.splice(3, arr.length, processZone(arr));
  897. if (arr[3]) arr[3] = Date.UTC.apply(null, arr[3]);
  898. arr[0] = -getBasicOffset(arr[0]);
  899. _this.zones[zone].push(arr);
  900. break;
  901. case 'Rule':
  902. rule = arr.shift();
  903. if (!_this.rules[rule]) {
  904. _this.rules[rule] = [];
  905. }
  906. //Parse int FROM year and TO year
  907. arr[0] = parseInt(arr[0], 10);
  908. arr[1] = parseInt(arr[1], 10) || arr[1];
  909. //Parse time string AT
  910. arr[5] = parseTimeString(arr[5]);
  911. //Parse offset SAVE
  912. arr[6] = getBasicOffset(arr[6]);
  913. _this.rules[rule].push(arr);
  914. break;
  915. case 'Link':
  916. //No zones for these should already exist.
  917. if (_this.zones[arr[1]]) {
  918. throw new Error('Error with Link ' + arr[1] + '. Cannot create link of a preexisted zone.');
  919. }
  920. //Create the link.
  921. _this.zones[arr[1]] = arr[0];
  922. break;
  923. }
  924. }
  925. }
  926. return true;
  927. };
  928. //Expose transport mechanism and allow overwrite.
  929. this.transport = _transport;
  930. this.getTzInfo = function (dt, tz, isUTC) {
  931. //Lazy-load any zones not yet loaded.
  932. if (this.loadingScheme === this.loadingSchemes.LAZY_LOAD) {
  933. //Get the correct region for the zone.
  934. var zoneFile = getRegionForTimezone(tz);
  935. if (!zoneFile) {
  936. throw new Error('Not a valid timezone ID.');
  937. }
  938. if (!this.loadedZones[zoneFile]) {
  939. //Get the file and parse it -- use synchronous XHR.
  940. this.loadZoneFile(zoneFile);
  941. }
  942. }
  943. var z = getZone(dt, tz);
  944. var off = z[0];
  945. //See if the offset needs adjustment.
  946. var rule = getRule(dt, z, isUTC);
  947. if (rule) {
  948. off = getAdjustedOffset(off, rule);
  949. }
  950. var abbr = getAbbreviation(z, rule);
  951. return { tzOffset: off, tzAbbr: abbr };
  952. };
  953. };
  954. }).call(this);