1// Copyright (C) 2007, Fredrik Kuivinen <frekui@gmail.com> 2// 2007, Petr Baudis <pasky@suse.cz> 3// 2008-2011, Jakub Narebski <jnareb@gmail.com> 4 5/** 6 * @fileOverview Datetime manipulation: parsing and formatting 7 * @license GPLv2 or later 8 */ 9 10 11/* ............................................................ */ 12/* parsing and retrieving datetime related information */ 13 14/** 15 * used to extract hours and minutes from timezone info, e.g '-0900' 16 * @constant 17 */ 18var tzRe =/^([+\-])([0-9][0-9])([0-9][0-9])$/; 19 20/** 21 * convert numeric timezone +/-ZZZZ to offset from UTC in seconds 22 * 23 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM' 24 * @returns {Number} offset from UTC in seconds for timezone 25 * 26 * @globals tzRe 27 */ 28functiontimezoneOffset(timezoneInfo) { 29var match = tzRe.exec(timezoneInfo); 30var tz_sign = (match[1] ==='-'? -1: +1); 31var tz_hour =parseInt(match[2],10); 32var tz_min =parseInt(match[3],10); 33 34return tz_sign*(((tz_hour*60) + tz_min)*60); 35} 36 37/** 38 * return local (browser) timezone as offset from UTC in seconds 39 * 40 * @returns {Number} offset from UTC in seconds for local timezone 41 */ 42functionlocalTimezoneOffset() { 43// getTimezoneOffset returns the time-zone offset from UTC, 44// in _minutes_, for the current locale 45return((newDate()).getTimezoneOffset() * -60); 46} 47 48/** 49 * return local (browser) timezone as numeric timezone '(+|-)HHMM' 50 * 51 * @returns {String} locat timezone as -/+ZZZZ 52 */ 53functionlocalTimezoneInfo() { 54var tzOffsetMinutes = (newDate()).getTimezoneOffset() * -1; 55 56returnformatTimezoneInfo(0, tzOffsetMinutes); 57} 58 59 60/** 61 * Parse RFC-2822 date into a Unix timestamp (into epoch) 62 * 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' 65 */ 66functionparseRFC2822Date(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 70return Date.parse(date) /1000; 71} 72 73 74/* ............................................................ */ 75/* formatting date */ 76 77/** 78 * format timezone offset as numerical timezone '(+|-)HHMM' or '(+|-)HH:MM' 79 * 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'; 84 * defaults to 0 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 88 */ 89functionformatTimezoneInfo(hours, minutes, sep) { 90 minutes = minutes ||0;// to be able to use formatTimezoneInfo(hh) 91 sep = sep ||'';// default format is +/-ZZZZ 92 93if(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 97} 98 99var tzSign = hours >=0?'+':'-'; 100if(hours <0) { 101 hours = -hours;// sign is stored in tzSign 102} 103 104return tzSign +padLeft(hours,2,'0') + sep +padLeft(minutes,2,'0'); 105} 106 107/** 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' 110 * 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 114 */ 115functionformatDateISOLocal(epoch, timezoneInfo) { 116// date corrected by timezone 117var localDate =newDate(1000* (epoch + 118timezoneOffset(timezoneInfo))); 119var localDateStr =// e.g. '2005-08-07' 120 localDate.getUTCFullYear() +'-'+ 121padLeft(localDate.getUTCMonth()+1,2,'0') +'-'+ 122padLeft(localDate.getUTCDate(),2,'0'); 123var localTimeStr =// e.g. '21:49:46' 124padLeft(localDate.getUTCHours(),2,'0') +':'+ 125padLeft(localDate.getUTCMinutes(),2,'0') +':'+ 126padLeft(localDate.getUTCSeconds(),2,'0'); 127 128return localDateStr +' '+ localTimeStr +' '+ timezoneInfo; 129} 130 131/** 132 * return date in local time formatted in rfc-2822 format 133 * e.g. 'Thu, 21 Dec 2000 16:01:07 +0200' 134 * 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 139 */ 140functionformatDateRFC2882(epoch, timezoneInfo, padDay) { 141// A short textual representation of a month, three letters 142var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; 143// A textual representation of a day, three letters 144var days = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]; 145// date corrected by timezone 146var localDate =newDate(1000* (epoch + 147timezoneOffset(timezoneInfo))); 148var 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(); 153var localTimeStr =// e.g. '21:49:46' 154padLeft(localDate.getUTCHours(),2,'0') +':'+ 155padLeft(localDate.getUTCMinutes(),2,'0') +':'+ 156padLeft(localDate.getUTCSeconds(),2,'0'); 157 158return localDateStr +' '+ localTimeStr +' '+ timezoneInfo; 159} 160 161/* end of datetime.js */