gitweb.js: Extract and improve datetime handling
Move formatDateISOLocal(epoch, timezone) function (and also helper timezoneOffset(timezoneInfo) function it requires) from common-lib.js to datetime.js Add new functions: * localTimezoneOffset - to get browser timezone offset in seconds * localTimezoneInfo - to get browser timezone in '(+|-)HHMM' format * formatTimezoneInfo - turn offset in hours and minutes into '(+|-)HHMM' * parseRFC2822Date - to parse RFC-2822 dates that gitweb uses into epoch * formatDateRFC2882 - like formatDateISOLocal, only RFC-2822 format All those functions are meant to be used in future commit 'gitweb: javascript ability to adjust time based on timezone' An alternative would be to use e.g. Datejs (http://www.datejs.com) library, or JavaScript framework that has date formatting (perhaps as a plugin). While at it escape '-' in character class inside tzRe regexp, as recommended by JSLint (http://www.jslint.com). Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
4dfa207eec
commit
54b1479a77
@ -88,57 +88,6 @@ function createRequestObject() {
|
||||
}
|
||||
|
||||
|
||||
/* ............................................................ */
|
||||
/* time and data */
|
||||
|
||||
/**
|
||||
* used to extract hours and minutes from timezone info, e.g '-0900'
|
||||
* @constant
|
||||
*/
|
||||
var tzRe = /^([+-])([0-9][0-9])([0-9][0-9])$/;
|
||||
|
||||
/**
|
||||
* convert numeric timezone +/-ZZZZ to offset from UTC in seconds
|
||||
*
|
||||
* @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
|
||||
* @returns {Number} offset from UTC in seconds for timezone
|
||||
*
|
||||
* @globals tzRe
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* return date in local time formatted in iso-8601 like format
|
||||
* 'yyyy-mm-dd HH:MM:SS +/-ZZZZ' e.g. '2005-08-07 21:49:46 +0200'
|
||||
*
|
||||
* @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC'
|
||||
* @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
|
||||
* @returns {String} date in local time in iso-8601 like format
|
||||
*/
|
||||
function formatDateISOLocal(epoch, timezoneInfo) {
|
||||
// date corrected by timezone
|
||||
var localDate = new Date(1000 * (epoch +
|
||||
timezoneOffset(timezoneInfo)));
|
||||
var localDateStr = // e.g. '2005-08-07'
|
||||
localDate.getUTCFullYear() + '-' +
|
||||
padLeft(localDate.getUTCMonth()+1, 2, '0') + '-' +
|
||||
padLeft(localDate.getUTCDate(), 2, '0');
|
||||
var localTimeStr = // e.g. '21:49:46'
|
||||
padLeft(localDate.getUTCHours(), 2, '0') + ':' +
|
||||
padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
|
||||
padLeft(localDate.getUTCSeconds(), 2, '0');
|
||||
|
||||
return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
|
||||
}
|
||||
|
||||
|
||||
/* ............................................................ */
|
||||
/* unquoting/unescaping filenames */
|
||||
|
||||
|
Reference in New Issue
Block a user