var tzRe = /^([+\-])([0-9][0-9])([0-9][0-9])$/;
function timezoneOffset(timezoneInfo) {
var match = tzRe.exec(timezoneInfo);
var tz_sign = (match[1] === '-' ? -1 : +1);
var tz_hour = parseInt(match[2],10);
var tz_min = parseInt(match[3],10);
return tz_sign*(((tz_hour*60) + tz_min)*60);
}
function localTimezoneOffset() {
return ((new Date()).getTimezoneOffset() * -60);
}
function localTimezoneInfo() {
var tzOffsetMinutes = (new Date()).getTimezoneOffset() * -1;
return formatTimezoneInfo(0, tzOffsetMinutes);
}
function parseRFC2822Date(date) {
return Date.parse(date) / 1000;
}
/* ............................................................ */
/* formatting date */
/**
* format timezone offset as numerical timezone '(+|-)HHMM' or '(+|-)HH:MM'
*
* @param {Number} hours: offset in hours, e.g. 2 for '+0200'
* @param {Number} [minutes] offset in minutes, e.g. 30 for '-4030';
* it is split into hours if not 0 <= minutes < 60,
* for example 1200 would give '+0100';
* defaults to 0
* @param {String} [sep] separator between hours and minutes part,
* default is '', might be ':' for W3CDTF (rfc-3339)
* @returns {String} timezone in '(+|-)HHMM' or '(+|-)HH:MM' format
*/
function formatTimezoneInfo(hours, minutes, sep) {
minutes = minutes || 0; // to be able to use formatTimezoneInfo(hh)
sep = sep || ''; // default format is +/-ZZZZ
if (minutes < 0 || minutes > 59) {
hours = minutes > 0 ? Math.floor(minutes / 60) : Math.ceil(minutes / 60);
minutes = Math.abs(minutes - 60*hours); }
var tzSign = hours >= 0 ? '+' : '-';
if (hours < 0) {
hours = -hours; }
return tzSign + padLeft(hours, 2, '0') + sep + padLeft(minutes, 2, '0');
}
function normalizeTimezoneInfo(timezoneInfo) {
switch (timezoneInfo) {
case 'utc':
return '+0000';
case 'local': return localTimezoneInfo();
}
return timezoneInfo;
}
function formatDateISOLocal(epoch, timezoneInfo) {
var localDate = new Date(1000 * (epoch +
timezoneOffset(timezoneInfo)));
var localDateStr = localDate.getUTCFullYear() + '-' +
padLeft(localDate.getUTCMonth()+1, 2, '0') + '-' +
padLeft(localDate.getUTCDate(), 2, '0');
var localTimeStr = padLeft(localDate.getUTCHours(), 2, '0') + ':' +
padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
padLeft(localDate.getUTCSeconds(), 2, '0');
return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
}
function formatDateRFC2882(epoch, timezoneInfo, padDay) {
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var localDate = new Date(1000 * (epoch +
timezoneOffset(timezoneInfo)));
var localDateStr = days[localDate.getUTCDay()] + ', ' +
(padDay ? padLeft(localDate.getUTCDate(),2,'0') : localDate.getUTCDate()) + ' ' +
months[localDate.getUTCMonth()] + ' ' +
localDate.getUTCFullYear();
var localTimeStr = padLeft(localDate.getUTCHours(), 2, '0') + ':' +
padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
padLeft(localDate.getUTCSeconds(), 2, '0');
return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
}