Code coverage report for lib\common\util\iso8061date.js

Statements: 100% (18 / 18)      Branches: 50% (1 / 2)      Functions: 100% (3 / 3)      Lines: 100% (18 / 18)      Ignored: none     

All files » lib/common/util/ » iso8061date.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                1 19 19 16     19                     1 15 15                 1 19 19 19 19 19 19 19     19                   19  
// 
// Copyright (c) Microsoft and contributors.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
// 
 
var rightPad = function (n, number) {
  var currentN = '' + n;
  while (currentN.length < number) {
    currentN = currentN + '0';
  }
 
  return currentN;
};
 
/**
* Formats a date into an iso 8061 string.
*
* @param {date}    date The date to format.
* @param {bool}    skipMilliseconds Boolean value indicating if the miliseconds part of the date should not be included.
* @param {integer} millisecondsPading Number of digits to left pad the miliseconds.
* @return {string} The date formated in the ISO 8061 date format.
*/
exports.format = function (date) {
  var dateString = date.toISOString();
  return dateString.substring(0, dateString.length - 1) + '0000Z';
};
 
/**
* Parses an ISO 8061 date string into a date object.
*
* @param {string} stringDateTime The string with the date to parse in the ISO 8061 format.
* @return {date} The parsed date.
*/
exports.parse = function (stringDateTime) {
  var parts = stringDateTime.split('T');
  var ymd = parts[0].split('-');
  var time = parts[1].split('.');
  var hms = time[0].split(':');
  var ms = 0;
  Eif (time[1]) {
    ms = time[1].split('Z');
  }
 
  var date = new Date(Date.UTC(
    parseInt(ymd[0], 10),
    parseInt(ymd[1], 10) - 1,
    parseInt(ymd[2], 10),
    parseInt(hms[0], 10),
    parseInt(hms[1], 10),
    parseInt(hms[2], 10),
    Math.round(parseInt(rightPad(ms[0], 7), 10) / 10000)
  ));
 
  return date;
};