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 * translate 'utc' and 'local' to numerical timezone 109 * @param {String} timezoneInfo: might be 'utc' or 'local' (browser) 110 */ 111functionnormalizeTimezoneInfo(timezoneInfo) { 112switch(timezoneInfo) { 113case'utc': 114return'+0000'; 115case'local':// 'local' is browser timezone 116returnlocalTimezoneInfo(); 117} 118return timezoneInfo; 119} 120 121 122/** 123 * return date in local time formatted in iso-8601 like format 124 * 'yyyy-mm-dd HH:MM:SS +/-ZZZZ' e.g. '2005-08-07 21:49:46 +0200' 125 * 126 * @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC' 127 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM' 128 * @returns {String} date in local time in iso-8601 like format 129 */ 130functionformatDateISOLocal(epoch, timezoneInfo) { 131// date corrected by timezone 132var localDate =newDate(1000* (epoch + 133timezoneOffset(timezoneInfo))); 134var localDateStr =// e.g. '2005-08-07' 135 localDate.getUTCFullYear() +'-'+ 136padLeft(localDate.getUTCMonth()+1,2,'0') +'-'+ 137padLeft(localDate.getUTCDate(),2,'0'); 138var localTimeStr =// e.g. '21:49:46' 139padLeft(localDate.getUTCHours(),2,'0') +':'+ 140padLeft(localDate.getUTCMinutes(),2,'0') +':'+ 141padLeft(localDate.getUTCSeconds(),2,'0'); 142 143return localDateStr +' '+ localTimeStr +' '+ timezoneInfo; 144} 145 146/** 147 * return date in local time formatted in rfc-2822 format 148 * e.g. 'Thu, 21 Dec 2000 16:01:07 +0200' 149 * 150 * @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC' 151 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM' 152 * @param {Boolean} [padDay] e.g. 'Sun, 07 Aug' if true, 'Sun, 7 Aug' otherwise 153 * @returns {String} date in local time in rfc-2822 format 154 */ 155functionformatDateRFC2882(epoch, timezoneInfo, padDay) { 156// A short textual representation of a month, three letters 157var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; 158// A textual representation of a day, three letters 159var days = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]; 160// date corrected by timezone 161var localDate =newDate(1000* (epoch + 162timezoneOffset(timezoneInfo))); 163var localDateStr =// e.g. 'Sun, 7 Aug 2005' or 'Sun, 07 Aug 2005' 164 days[localDate.getUTCDay()] +', '+ 165(padDay ?padLeft(localDate.getUTCDate(),2,'0') : localDate.getUTCDate()) +' '+ 166 months[localDate.getUTCMonth()] +' '+ 167 localDate.getUTCFullYear(); 168var localTimeStr =// e.g. '21:49:46' 169padLeft(localDate.getUTCHours(),2,'0') +':'+ 170padLeft(localDate.getUTCMinutes(),2,'0') +':'+ 171padLeft(localDate.getUTCSeconds(),2,'0'); 172 173return localDateStr +' '+ localTimeStr +' '+ timezoneInfo; 174} 175 176/* end of datetime.js */