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.
5 var timeutil
= (function() {
9 * Offset needed to convert event times to Date objects.
10 * Updated whenever constants are loaded.
12 var timeTickOffset
= 0;
15 * The time of the first observed event. Used for more friendly time display.
20 * Sets the offset used to convert tick counts to dates.
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;
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.
33 * @param {string} timeTicks A time represented in "time ticks".
34 * @return {number} The Javascript time that |timeTicks| represents.
36 function convertTimeTicksToTime(timeTicks
) {
37 return timeTickOffset
+ (timeTicks
- 0);
41 * The browser gives us times in terms of "time ticks" in milliseconds.
42 * This function converts the tick count to a Date() object.
44 * @param {string} timeTicks A time represented in "time ticks".
45 * @return {Date} The time that |timeTicks| represents.
47 function convertTimeTicksToDate(timeTicks
) {
48 return new Date(convertTimeTicksToTime(timeTicks
));
52 * Returns the current time.
54 * @return {number} Milliseconds since the Unix epoch.
56 function getCurrentTime() {
61 * Returns the curent time in time ticks.
63 * @return {number} Current time, in TimeTicks.
65 function getCurrentTimeTicks() {
66 return getCurrentTime() - timeTickOffset
;
70 * Sets the base time more friendly display.
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.
75 function setBaseTime(firstEventTime
) {
76 baseTime
= firstEventTime
;
80 * Sets the base time more friendly display.
82 * @return {number} Time set by setBaseTime, or 0 if no time has been set.
84 function getBaseTime() {
89 * Clears the base time, so isBaseTimeSet() returns 0.
91 function clearBaseTime() {
96 * Returns true if the base time has been initialized.
98 * @return {bool} True if the base time is set.
100 function isBaseTimeSet() {
101 return baseTime
!= 0;
105 * Takes in a "time ticks" and returns it as a time since the base time, in
108 * @param {string} timeTicks A time represented in "time ticks".
109 * @return {number} Milliseconds since the base time.
111 function convertTimeTicksToRelativeTime(timeTicks
) {
112 return convertTimeTicksToTime(timeTicks
) - baseTime
;
116 * Adds an HTML representation of |date| to |parentNode|.
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.
122 function addNodeWithDate(parentNode
, date
) {
123 var span
= addNodeWithText(parentNode
, 'span', dateToString(date
));
124 span
.title
= 't=' + date
.getTime();
129 * Returns a string representation of |date|.
131 * @param {Date} date The date to be represented.
132 * @return {string} A string representation of |date|.
134 function dateToString(date
) {
135 var dateStr
= date
.getFullYear() + '-' +
136 zeroPad_(date
.getMonth() + 1, 2) + '-' +
137 zeroPad_(date
.getDate(), 2);
139 var timeStr
= zeroPad_(date
.getHours(), 2) + ':' +
140 zeroPad_(date
.getMinutes(), 2) + ':' +
141 zeroPad_(date
.getSeconds(), 2) + '.' +
142 zeroPad_(date
.getMilliseconds(), 3);
144 return dateStr
+ ' ' + timeStr
;
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|.
153 function zeroPad_(num
, len
) {
155 while (str
.length
< len
)
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
,