1 // Copyright (C) 2007, Fredrik Kuivinen <frekui@gmail.com>
2 // 2007, Petr Baudis <pasky@suse.cz>
3 // 2008-2011, Jakub Narebski <jnareb@gmail.com>
6 * @fileOverview Datetime manipulation: parsing and formatting
7 * @license GPLv2 or later
11 /* ............................................................ */
12 /* parsing and retrieving datetime related information */
15 * used to extract hours and minutes from timezone info, e.g '-0900'
18 var tzRe = /^([+\-])([0-9][0-9])([0-9][0-9])$/;
21 * convert numeric timezone +/-ZZZZ to offset from UTC in seconds
23 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
24 * @returns {Number} offset from UTC in seconds for timezone
28 function timezoneOffset(timezoneInfo) {
29 var match = tzRe.exec(timezoneInfo);
30 var tz_sign = (match[1] === '-' ? -1 : +1);
31 var tz_hour = parseInt(match[2],10);
32 var tz_min = parseInt(match[3],10);
34 return tz_sign*(((tz_hour*60) + tz_min)*60);
38 * return local (browser) timezone as offset from UTC in seconds
40 * @returns {Number} offset from UTC in seconds for local timezone
42 function localTimezoneOffset() {
43 // getTimezoneOffset returns the time-zone offset from UTC,
44 // in _minutes_, for the current locale
45 return ((new Date()).getTimezoneOffset() * -60);
49 * return local (browser) timezone as numeric timezone '(+|-)HHMM'
51 * @returns {String} locat timezone as -/+ZZZZ
53 function localTimezoneInfo() {
54 var tzOffsetMinutes = (new Date()).getTimezoneOffset() * -1;
56 return formatTimezoneInfo(0, tzOffsetMinutes);
61 * Parse RFC-2822 date into a Unix timestamp (into epoch)
63 * @param {String} date: date in RFC-2822 format, e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'
64 * @returns {Number} epoch i.e. seconds since '00:00:00 1970-01-01 UTC'
66 function parseRFC2822Date(date) {
67 // Date.parse accepts the IETF standard (RFC 1123 Section 5.2.14 and elsewhere)
68 // date syntax, which is defined in RFC 2822 (obsoletes RFC 822)
69 // and returns number of _milli_seconds since January 1, 1970, 00:00:00 UTC
70 return Date.parse(date) / 1000;
74 /* ............................................................ */
78 * format timezone offset as numerical timezone '(+|-)HHMM' or '(+|-)HH:MM'
80 * @param {Number} hours: offset in hours, e.g. 2 for '+0200'
81 * @param {Number} [minutes] offset in minutes, e.g. 30 for '-4030';
82 * it is split into hours if not 0 <= minutes < 60,
83 * for example 1200 would give '+0100';
85 * @param {String} [sep] separator between hours and minutes part,
86 * default is '', might be ':' for W3CDTF (rfc-3339)
87 * @returns {String} timezone in '(+|-)HHMM' or '(+|-)HH:MM' format
89 function formatTimezoneInfo(hours, minutes, sep) {
90 minutes = minutes || 0; // to be able to use formatTimezoneInfo(hh)
91 sep = sep || ''; // default format is +/-ZZZZ
93 if (minutes < 0 || minutes > 59) {
94 hours = minutes > 0 ? Math.floor(minutes / 60) : Math.ceil(minutes / 60);
95 minutes = Math.abs(minutes - 60*hours); // sign of minutes is sign of hours
96 // NOTE: this works correctly because there is no UTC-00:30 timezone
99 var tzSign = hours >= 0 ? '+' : '-';
101 hours = -hours; // sign is stored in tzSign
104 return tzSign + padLeft(hours, 2, '0') + sep + padLeft(minutes, 2, '0');
108 * return date in local time formatted in iso-8601 like format
109 * 'yyyy-mm-dd HH:MM:SS +/-ZZZZ' e.g. '2005-08-07 21:49:46 +0200'
111 * @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC'
112 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
113 * @returns {String} date in local time in iso-8601 like format
115 function formatDateISOLocal(epoch, timezoneInfo) {
116 // date corrected by timezone
117 var localDate = new Date(1000 * (epoch +
118 timezoneOffset(timezoneInfo)));
119 var localDateStr = // e.g. '2005-08-07'
120 localDate.getUTCFullYear() + '-' +
121 padLeft(localDate.getUTCMonth()+1, 2, '0') + '-' +
122 padLeft(localDate.getUTCDate(), 2, '0');
123 var localTimeStr = // e.g. '21:49:46'
124 padLeft(localDate.getUTCHours(), 2, '0') + ':' +
125 padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
126 padLeft(localDate.getUTCSeconds(), 2, '0');
128 return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
132 * return date in local time formatted in rfc-2822 format
133 * e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'
135 * @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC'
136 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
137 * @param {Boolean} [padDay] e.g. 'Sun, 07 Aug' if true, 'Sun, 7 Aug' otherwise
138 * @returns {String} date in local time in rfc-2822 format
140 function formatDateRFC2882(epoch, timezoneInfo, padDay) {
141 // A short textual representation of a month, three letters
142 var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
143 // A textual representation of a day, three letters
144 var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
145 // date corrected by timezone
146 var localDate = new Date(1000 * (epoch +
147 timezoneOffset(timezoneInfo)));
148 var localDateStr = // e.g. 'Sun, 7 Aug 2005' or 'Sun, 07 Aug 2005'
149 days[localDate.getUTCDay()] + ', ' +
150 (padDay ? padLeft(localDate.getUTCDate(),2,'0') : localDate.getUTCDate()) + ' ' +
151 months[localDate.getUTCMonth()] + ' ' +
152 localDate.getUTCFullYear();
153 var localTimeStr = // e.g. '21:49:46'
154 padLeft(localDate.getUTCHours(), 2, '0') + ':' +
155 padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
156 padLeft(localDate.getUTCSeconds(), 2, '0');
158 return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
161 /* end of datetime.js */