gitweb / static / js / lib / datetime.json commit Merge git://ozlabs.org/~paulus/gitk (2dbfa67)
   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 */
  28function 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);
  33
  34        return 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 */
  42function localTimezoneOffset() {
  43        // getTimezoneOffset returns the time-zone offset from UTC,
  44        // in _minutes_, for the current locale
  45        return ((new Date()).getTimezoneOffset() * -60);
  46}
  47
  48/**
  49 * return local (browser) timezone as numeric timezone '(+|-)HHMM'
  50 *
  51 * @returns {String} locat timezone as -/+ZZZZ
  52 */
  53function localTimezoneInfo() {
  54        var tzOffsetMinutes = (new Date()).getTimezoneOffset() * -1;
  55
  56        return formatTimezoneInfo(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 */
  66function 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;
  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 */
  89function formatTimezoneInfo(hours, minutes, sep) {
  90        minutes = minutes || 0; // to be able to use formatTimezoneInfo(hh)
  91        sep = sep || ''; // default format is +/-ZZZZ
  92
  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
  97        }
  98
  99        var tzSign = hours >= 0 ? '+' : '-';
 100        if (hours < 0) {
 101                hours = -hours; // sign is stored in tzSign
 102        }
 103
 104        return 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 */
 111function normalizeTimezoneInfo(timezoneInfo) {
 112        switch (timezoneInfo) {
 113        case 'utc':
 114                return '+0000';
 115        case 'local': // 'local' is browser timezone
 116                return localTimezoneInfo();
 117        }
 118        return 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 */
 130function formatDateISOLocal(epoch, timezoneInfo) {
 131        // date corrected by timezone
 132        var localDate = new Date(1000 * (epoch +
 133                timezoneOffset(timezoneInfo)));
 134        var localDateStr = // e.g. '2005-08-07'
 135                localDate.getUTCFullYear()                 + '-' +
 136                padLeft(localDate.getUTCMonth()+1, 2, '0') + '-' +
 137                padLeft(localDate.getUTCDate(),    2, '0');
 138        var localTimeStr = // e.g. '21:49:46'
 139                padLeft(localDate.getUTCHours(),   2, '0') + ':' +
 140                padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
 141                padLeft(localDate.getUTCSeconds(), 2, '0');
 142
 143        return 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 */
 155function formatDateRFC2882(epoch, timezoneInfo, padDay) {
 156        // A short textual representation of a month, three letters
 157        var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
 158        // A textual representation of a day, three letters
 159        var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
 160        // date corrected by timezone
 161        var localDate = new Date(1000 * (epoch +
 162                timezoneOffset(timezoneInfo)));
 163        var 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();
 168        var localTimeStr = // e.g. '21:49:46'
 169                padLeft(localDate.getUTCHours(),   2, '0') + ':' +
 170                padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
 171                padLeft(localDate.getUTCSeconds(), 2, '0');
 172
 173        return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
 174}
 175
 176/* end of datetime.js */