js/xpconnect/loader/ISO8601DateUtils.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/loader/ISO8601DateUtils.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,144 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +const HOURS_TO_MINUTES = 60;
    1.10 +const MINUTES_TO_SECONDS = 60;
    1.11 +const SECONDS_TO_MILLISECONDS = 1000;
    1.12 +const MINUTES_TO_MILLISECONDS = MINUTES_TO_SECONDS * SECONDS_TO_MILLISECONDS;
    1.13 +const HOURS_TO_MILLISECONDS = HOURS_TO_MINUTES * MINUTES_TO_MILLISECONDS;
    1.14 +
    1.15 +this.EXPORTED_SYMBOLS = ["ISO8601DateUtils"];
    1.16 +
    1.17 +debug("*** loading ISO8601DateUtils\n");
    1.18 +
    1.19 +this.ISO8601DateUtils = {
    1.20 +
    1.21 +  /**
    1.22 +  * XXX Thunderbird's W3C-DTF function
    1.23 +  *
    1.24 +  * Converts a W3C-DTF (subset of ISO 8601) date string to a Javascript
    1.25 +  * date object. W3C-DTF is described in this note:
    1.26 +  * http://www.w3.org/TR/NOTE-datetime IETF is obtained via the Date
    1.27 +  * object's toUTCString() method.  The object's toString() method is
    1.28 +  * insufficient because it spells out timezones on Win32
    1.29 +  * (f.e. "Pacific Standard Time" instead of "PST"), which Mail doesn't
    1.30 +  * grok.  For info, see
    1.31 +  * http://lxr.mozilla.org/mozilla/source/js/src/jsdate.c#1526.
    1.32 +  */
    1.33 +  parse: function ISO8601_parse(aDateString) {
    1.34 +    var dateString = aDateString;
    1.35 +    if (!dateString.match('-')) {
    1.36 +      // Workaround for server sending
    1.37 +      // dates such as: 20030530T11:18:50-08:00
    1.38 +      // instead of: 2003-05-30T11:18:50-08:00
    1.39 +      var year = dateString.slice(0, 4);
    1.40 +      var month = dateString.slice(4, 6);
    1.41 +      var rest = dateString.slice(6, dateString.length);
    1.42 +      dateString = year + "-" + month + "-" + rest;
    1.43 +    }
    1.44 +
    1.45 +    var parts = dateString.match(/(\d{4})(-(\d{2,3}))?(-(\d{2}))?(T(\d{2}):(\d{2})(:(\d{2})(\.(\d+))?)?(Z|([+-])(\d{2}):(\d{2}))?)?/);
    1.46 +
    1.47 +    // Here's an example of a W3C-DTF date string and what .match returns for it.
    1.48 +    //
    1.49 +    // date: 2003-05-30T11:18:50.345-08:00
    1.50 +    // date.match returns array values:
    1.51 +    //
    1.52 +    //   0: 2003-05-30T11:18:50-08:00,
    1.53 +    //   1: 2003,
    1.54 +    //   2: -05,
    1.55 +    //   3: 05,
    1.56 +    //   4: -30,
    1.57 +    //   5: 30,
    1.58 +    //   6: T11:18:50-08:00,
    1.59 +    //   7: 11,
    1.60 +    //   8: 18,
    1.61 +    //   9: :50,
    1.62 +    //   10: 50,
    1.63 +    //   11: .345,
    1.64 +    //   12: 345,
    1.65 +    //   13: -08:00,
    1.66 +    //   14: -,
    1.67 +    //   15: 08,
    1.68 +    //   16: 00
    1.69 +
    1.70 +    // Create a Date object from the date parts.  Note that the Date
    1.71 +    // object apparently can't deal with empty string parameters in lieu
    1.72 +    // of numbers, so optional values (like hours, minutes, seconds, and
    1.73 +    // milliseconds) must be forced to be numbers.
    1.74 +    var date = new Date(parts[1], parts[3] - 1, parts[5], parts[7] || 0,
    1.75 +      parts[8] || 0, parts[10] || 0, parts[12] || 0);
    1.76 +
    1.77 +    // We now have a value that the Date object thinks is in the local
    1.78 +    // timezone but which actually represents the date/time in the
    1.79 +    // remote timezone (f.e. the value was "10:00 EST", and we have
    1.80 +    // converted it to "10:00 PST" instead of "07:00 PST").  We need to
    1.81 +    // correct that.  To do so, we're going to add the offset between
    1.82 +    // the remote timezone and UTC (to convert the value to UTC), then
    1.83 +    // add the offset between UTC and the local timezone //(to convert
    1.84 +    // the value to the local timezone).
    1.85 +
    1.86 +    // Ironically, W3C-DTF gives us the offset between UTC and the
    1.87 +    // remote timezone rather than the other way around, while the
    1.88 +    // getTimezoneOffset() method of a Date object gives us the offset
    1.89 +    // between the local timezone and UTC rather than the other way
    1.90 +    // around.  Both of these are the additive inverse (i.e. -x for x)
    1.91 +    // of what we want, so we have to invert them to use them by
    1.92 +    // multipying by -1 (f.e. if "the offset between UTC and the remote
    1.93 +    // timezone" is -5 hours, then "the offset between the remote
    1.94 +    // timezone and UTC" is -5*-1 = 5 hours).
    1.95 +
    1.96 +    // Note that if the timezone portion of the date/time string is
    1.97 +    // absent (which violates W3C-DTF, although ISO 8601 allows it), we
    1.98 +    // assume the value to be in UTC.
    1.99 +
   1.100 +    // The offset between the remote timezone and UTC in milliseconds.
   1.101 +    var remoteToUTCOffset = 0;
   1.102 +    if (parts[13] && parts[13] != "Z") {
   1.103 +      var direction = (parts[14] == "+" ? 1 : -1);
   1.104 +      if (parts[15])
   1.105 +        remoteToUTCOffset += direction * parts[15] * HOURS_TO_MILLISECONDS;
   1.106 +      if (parts[16])
   1.107 +        remoteToUTCOffset += direction * parts[16] * MINUTES_TO_MILLISECONDS;
   1.108 +    }
   1.109 +    remoteToUTCOffset = remoteToUTCOffset * -1; // invert it
   1.110 +
   1.111 +    // The offset between UTC and the local timezone in milliseconds.
   1.112 +    var UTCToLocalOffset = date.getTimezoneOffset() * MINUTES_TO_MILLISECONDS;
   1.113 +    UTCToLocalOffset = UTCToLocalOffset * -1; // invert it
   1.114 +    date.setTime(date.getTime() + remoteToUTCOffset + UTCToLocalOffset);
   1.115 +
   1.116 +    return date;
   1.117 +  },
   1.118 +
   1.119 +  create: function ISO8601_create(aDate) {
   1.120 +    function zeropad (s, l) {
   1.121 +      s = s.toString(); // force it to a string
   1.122 +      while (s.length < l) {
   1.123 +        s = '0' + s;
   1.124 +      }
   1.125 +      return s;
   1.126 +    }
   1.127 +
   1.128 +    var myDate;
   1.129 +    // if d is a number, turn it into a date
   1.130 +    if (typeof aDate == 'number') {
   1.131 +      myDate = new Date()
   1.132 +      myDate.setTime(aDate);
   1.133 +    } else {
   1.134 +      myDate = aDate;
   1.135 +    }
   1.136 +
   1.137 +    // YYYY-MM-DDThh:mm:ssZ
   1.138 +    var result = zeropad(myDate.getUTCFullYear (), 4) +
   1.139 +                 zeropad(myDate.getUTCMonth () + 1, 2) +
   1.140 +                 zeropad(myDate.getUTCDate (), 2) + 'T' +
   1.141 +                 zeropad(myDate.getUTCHours (), 2) + ':' +
   1.142 +                 zeropad(myDate.getUTCMinutes (), 2) + ':' +
   1.143 +                 zeropad(myDate.getUTCSeconds (), 2) + 'Z';
   1.144 +
   1.145 +    return result;
   1.146 +  }
   1.147 +}

mercurial