• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5var timeutil = (function() {
6  'use strict';
7
8  /**
9   * Offset needed to convert event times to Date objects.
10   * Updated whenever constants are loaded.
11   */
12  var timeTickOffset = 0;
13
14  /**
15   * The time of the first observed event.  Used for more friendly time display.
16   */
17  var baseTime = 0;
18
19  /**
20   * Sets the offset used to convert tick counts to dates.
21   */
22  function setTimeTickOffset(offset) {
23    // Note that the subtraction by 0 is to cast to a number (probably a float
24    // since the numbers are big).
25    timeTickOffset = offset - 0;
26  }
27
28  /**
29   * The browser gives us times in terms of "time ticks" in milliseconds.
30   * This function converts the tick count to a Javascript "time", which is
31   * the UTC time in milliseconds.
32   *
33   * @param {string} timeTicks A time represented in "time ticks".
34   * @return {number} The Javascript time that |timeTicks| represents.
35   */
36  function convertTimeTicksToTime(timeTicks) {
37    return timeTickOffset + (timeTicks - 0);
38  }
39
40  /**
41   * The browser gives us times in terms of "time ticks" in milliseconds.
42   * This function converts the tick count to a Date() object.
43   *
44   * @param {string} timeTicks A time represented in "time ticks".
45   * @return {Date} The time that |timeTicks| represents.
46   */
47  function convertTimeTicksToDate(timeTicks) {
48    return new Date(convertTimeTicksToTime(timeTicks));
49  }
50
51  /**
52   * Returns the current time.
53   *
54   * @return {number} Milliseconds since the Unix epoch.
55   */
56  function getCurrentTime() {
57    return Date.now();
58  }
59
60  /**
61   * Returns the curent time in time ticks.
62   *
63   * @return {number} Current time, in TimeTicks.
64   */
65  function getCurrentTimeTicks() {
66    return getCurrentTime() - timeTickOffset;
67  }
68
69  /**
70   * Sets the base time more friendly display.
71   *
72   * @param {string} firstEventTime The time of the first event, as a Javascript
73   *     numeric time.  Other times can be displayed relative to this time.
74   */
75  function setBaseTime(firstEventTime) {
76    baseTime = firstEventTime;
77  }
78
79  /**
80   * Sets the base time more friendly display.
81   *
82   * @return {number} Time set by setBaseTime, or 0 if no time has been set.
83   */
84  function getBaseTime() {
85    return baseTime;
86  }
87
88  /**
89   * Clears the base time, so isBaseTimeSet() returns 0.
90   */
91  function clearBaseTime() {
92    baseTime = 0;
93  }
94
95  /**
96   * Returns true if the base time has been initialized.
97   *
98   * @return {bool} True if the base time is set.
99   */
100  function isBaseTimeSet() {
101    return baseTime != 0;
102  }
103
104  /**
105   * Takes in a "time ticks" and returns it as a time since the base time, in
106   * milliseconds.
107   *
108   * @param {string} timeTicks A time represented in "time ticks".
109   * @return {number} Milliseconds since the base time.
110   */
111  function convertTimeTicksToRelativeTime(timeTicks) {
112    return convertTimeTicksToTime(timeTicks) - baseTime;
113  }
114
115  /**
116   * Adds an HTML representation of |date| to |parentNode|.
117   *
118   * @param {DomNode} parentNode The node that will contain the new node.
119   * @param {Date} date The date to be displayed.
120   * @return {DomNode} The new node containing the date/time.
121   */
122  function addNodeWithDate(parentNode, date) {
123    var span = addNodeWithText(parentNode, 'span', dateToString(date));
124    span.title = 't=' + date.getTime();
125    return span;
126  }
127
128  /**
129   * Returns a string representation of |date|.
130   *
131   * @param {Date} date The date to be represented.
132   * @return {string} A string representation of |date|.
133   */
134  function dateToString(date) {
135    var dateStr = date.getFullYear() + '-' +
136                  zeroPad_(date.getMonth() + 1, 2) + '-' +
137                  zeroPad_(date.getDate(), 2);
138
139    var timeStr = zeroPad_(date.getHours(), 2) + ':' +
140                  zeroPad_(date.getMinutes(), 2) + ':' +
141                  zeroPad_(date.getSeconds(), 2) + '.' +
142                  zeroPad_(date.getMilliseconds(), 3);
143
144    return dateStr + ' ' + timeStr;
145  }
146
147  /**
148   * Prefixes enough zeros to |num| so that it has length |len|.
149   * @param {number} num The number to be padded.
150   * @param {number} len The desired length of the returned string.
151   * @return {string} The zero-padded representation of |num|.
152   */
153  function zeroPad_(num, len) {
154    var str = num + '';
155    while (str.length < len)
156      str = '0' + str;
157    return str;
158  }
159
160  return {
161    setTimeTickOffset: setTimeTickOffset,
162    convertTimeTicksToTime: convertTimeTicksToTime,
163    convertTimeTicksToDate: convertTimeTicksToDate,
164    getCurrentTime: getCurrentTime,
165    getCurrentTimeTicks: getCurrentTimeTicks,
166    setBaseTime: setBaseTime,
167    getBaseTime: getBaseTime,
168    clearBaseTime: clearBaseTime,
169    isBaseTimeSet: isBaseTimeSet,
170    convertTimeTicksToRelativeTime: convertTimeTicksToRelativeTime,
171    addNodeWithDate: addNodeWithDate,
172    dateToString: dateToString,
173  };
174})();
175