// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var timeutil = (function() {
  'use strict';

  /**
   * Offset needed to convert event times to Date objects.
   * Updated whenever constants are loaded.
   */
  var timeTickOffset = 0;

  /**
   * Sets the offset used to convert tick counts to dates.
   */
  function setTimeTickOffset(offset) {
    // Note that the subtraction by 0 is to cast to a number (probably a float
    // since the numbers are big).
    timeTickOffset = offset - 0;
  }

  /**
   * The browser gives us times in terms of "time ticks" in milliseconds.
   * This function converts the tick count to a Date() object.
   *
   * @param {String} timeTicks.
   * @returns {Date} The time that |timeTicks| represents.
   */
  function convertTimeTicksToDate(timeTicks) {
    var timeStampMs = timeTickOffset + (timeTicks - 0);
    return new Date(timeStampMs);
  }

  /**
   * Returns the current time.
   *
   * @returns {number} Milliseconds since the Unix epoch.
   */
  function getCurrentTime() {
    return (new Date()).getTime();
  }

  /**
   * Adds an HTML representation of |date| to |parentNode|.
   *
   * @param {DomNode} parentNode
   * @param {Date} date
   * @returns {DomNode} The node containing the date/time.
   */
  function addNodeWithDate(parentNode, date) {
    var span = addNodeWithText(parentNode, 'span', dateToString(date));
    span.title = 't=' + date.getTime();
    return span;
  }

  /**
   * Returns a string representation of |date|.
   *
   * @param {Date} date
   * @returns {String}
   */
  function dateToString(date) {
    var dateStr =
        date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();

    // Prefix the milliseconds with enough zeros to make it three characters
    // long.
    var paddedMilliseconds = '' + date.getMilliseconds();
    while (paddedMilliseconds.length < 3)
      paddedMilliseconds = '0' + paddedMilliseconds;

    var timeStr =
        date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() +
        '.' + paddedMilliseconds;

    return '[' + dateStr + '] ' + timeStr;
  }

  return {
    setTimeTickOffset: setTimeTickOffset,
    convertTimeTicksToDate: convertTimeTicksToDate,
    getCurrentTime: getCurrentTime,
    addNodeWithDate: addNodeWithDate,
    dateToString: dateToString
  };
})();

@@ -1707,32 +1707,24 @@
 ar dateStr =
-%0A       
  date.getFul
@@ -1734,21 +1734,47 @@
 ar() + '
-/' + 
+-' +%0A                  zeroPad_
 (date.ge
@@ -1789,18 +1789,48 @@
  + 1
+, 2
 ) + '
-/' + 
+-' +%0A                  zeroPad_(
 date
@@ -1843,143 +1843,162 @@
 te()
+, 2)
 ;%0A%0A    
-// Prefix the milliseconds with enough zeros to make it three characters%0A    // long.%0A    var paddedMilliseconds =
+var timeStr = zeroPad_(date.getHours(), 2) + ':' +%0A                  zeroPad_(date.getMinutes(), 2) +
  '
+:
 ' +
- 
+%0A                  zeroPad_(
 date.get
 Mill
@@ -1997,22 +1997,17 @@
 .get
-Millis
+S
 econds()
 ;%0A  
@@ -2006,91 +2006,56 @@
 ds()
-;%0A    while (paddedMilliseconds.length %3C 3)%0A      paddedMilliseconds = '0' + padded
+, 2) + '.' +%0A                  zeroPad_(date.get
 Mill
@@ -2062,34 +2062,59 @@
 iseconds
+(), 3)
 ;%0A%0A    
-var
+return dateStr + ' ' +
  timeStr
  =%0A     
@@ -2109,163 +2109,212 @@
 eStr
- =%0A        date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() +
+;%0A  %7D%0A%0A  /**%0A   * Prefixes enough zeros to %7Cnum%7C so that it has length %7Clen%7C.%0A   */%0A  function zeroPad_(num, len) %7B
 %0A    
-    '.' + paddedMilliseconds;%0A%0A    return '%5B
+var str = num + '';%0A    while (str.length %3C len)%0A      str = '0
 ' + 
-dateStr + '%5D ' + timeS
+str;%0A    return s
 tr;%0A

Tweak date format used on net-internals. Adds zero-padding for minutes/hours/days, removes the brackets, and uses '-' rather than '/' as date separator.