Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: appengine_apps/chromium_status/static/js/common/date_util.js

Issue 778533003: Moved chromium_status to appengine/ (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011 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
5 /**
6 * Represents a span of time from [endTime, startTime).
7 *
8 * (Note that startTime is more recent than endTime).
9 *
10 * @param {int} startTime Unix timestamp in milliseconds.
11 * @param {int} endTime Unix timestamp in milliseconds.
12 * @constructor
13 */
14 function TimeRange(startTime, endTime) {
15 this.startTime = startTime;
16 this.endTime = endTime;
17 }
18
19 /**
20 * Helper class with time/date functions functions.
21 */
22 var DateUtil = {};
23
24 /**
25 * The number of seconds in an hour.
26 */
27 DateUtil.SECONDS_PER_HOUR = 60 * 60;
28
29 /**
30 * The number of milliseconds in an hour.
31 */
32 DateUtil.MILLIS_PER_HOUR = DateUtil.SECONDS_PER_HOUR * 1000;
33
34 /**
35 * The number of milliseconds in a day.
36 */
37 DateUtil.MILLIS_PER_DAY = DateUtil.MILLIS_PER_HOUR * 24;
38
39 /**
40 * Parses a date resembling "2009-10-23" into a unix timestamp (milliseconds).
41 * Assumes the timezone for the date is UTC. Sets the time component to
42 * midnight.
43 *
44 * @param {string} dateStr
45 * @return {int} Unix timestamp in milliseconds.
46 */
47 DateUtil.ParseUTCDateString = function(dateStr) {
48 var parts = dateStr.split("-");
49 return Date.UTC(parseInt(parts[0], 10), // year
50 parseInt(parts[1], 10) - 1, // month
51 parseInt(parts[2], 10), // day
52 0, 0, 0);
53 };
54
55 /**
56 * Parses a date resembling "2009-10-23" into a Date object.
57 * Assumes the local timezeone for the date. Sets the time component to
58 * midnight (of local timezone).
59 *
60 * @param {string} dateStr
61 * @return {Date} Valid date object, or null if failed.
62 */
63 DateUtil.ParseStringToLocalDate = function(dateStr) {
64 // We support both "2009/10/23" and "2009-10-23".
65 // Normalize to a format using "-".
66 dateStr = dateStr.replace(/-/g, "/");
67
68 var parts = dateStr.split("/");
69
70 if (parts.length != 3)
71 return null; // Failed to parse.
72
73 var d = new Date(parseInt(parts[0], 10), // year
74 parseInt(parts[1], 10) - 1, // month
75 parseInt(parts[2], 10), // day
76 0, 0, 0);
77 return d;
78 };
79
80 /**
81 * Parses a date/time string resembling "2009-10-06 22:53:32 UTC" to a unix
82 * timestamp.
83 *
84 * @param {string} dateStr
85 * @return {int} Unix timestamp in milliseconds.
86 */
87 DateUtil.ParseUTCDateTimeString = function(dateStr) {
88 var parts = dateStr.split(" ");
89
90 if (parts.length != 3 || parts[2] != "UTC") {
91 Log("Invalid formatted dateStr: " + dateStr);
92 return 0;
93 }
94
95 var d = new Date();
96 d.setTime(DateUtil.ParseUTCDateString(parts[0]));
97
98 var timeParts = parts[1].split(":");
99
100 if (timeParts.length < 2) {
101 Log("Invalid formatted dateStr: " + dateStr);
102 return 0;
103 }
104
105 d.setUTCHours(parseInt(timeParts[0], 10));
106 d.setUTCMinutes(parseInt(timeParts[1], 10));
107 if (timeParts.length > 2)
108 d.setUTCSeconds(parseInt(timeParts[2], 10));
109 return d.getTime();
110 };
111
112 /**
113 * Formats |x| in decimal such that it occupies |count| characters.
114 */
115 function PadWithZero(x, count) {
116 var s = "" + x;
117 while (s.length < count)
118 s = "0" + s;
119 return s;
120 }
121
122 /**
123 * Returns a time range for the day that encloses |t| (in the local
124 * timezone. Anchored at midnight.
125 *
126 * @param {int} t Unix timestamp in milliseconds.
127 * @return {TimeRange}
128 */
129 DateUtil.GetLocalDayRange = function(t) {
130 var d = new Date();
131 d.setTime(t);
132
133 // Midnight
134 d.setHours(0);
135 d.setMinutes(0);
136 d.setSeconds(0);
137 d.setMilliseconds(0);
138
139 var endTime = d.getTime();
140 var startTime = endTime + DateUtil.MILLIS_PER_DAY;
141
142 return new TimeRange(startTime, endTime);
143 };
144
145 /**
146 * Returns a time range for the day that encloses |t| in UTC
147 * Anchored at midnight.
148 *
149 * @param {int} t Unix timestamp in milliseconds.
150 * @return {TimeRange}
151 */
152 DateUtil.GetUTCDayRange = function(t) {
153 var d = new Date();
154 d.setTime(t);
155
156 // Midnight
157 d.setUTCHours(0);
158 d.setUTCMinutes(0);
159 d.setUTCSeconds(0);
160 d.setMilliseconds(0);
161
162 var endTime = d.getTime();
163 var startTime = endTime + DateUtil.MILLIS_PER_DAY;
164
165 return new TimeRange(startTime, endTime);
166 };
167
168 /**
169 * Returns a list of all of the days contained by |timeRange|,
170 * using the current timezone.
171 *
172 * @param {TimeRange} timeRange.
173 * @return {array<TimeRange>}
174 */
175 DateUtil.GetLocalDaysInRange = function(timeRange) {
176 var days = [];
177
178 var t = timeRange.startTime;
179 t -= DateUtil.MILLIS_PER_DAY;
180
181 while (t >= timeRange.endTime) {
182 var day = DateUtil.GetLocalDayRange(t);
183 days.push(day);
184 t -= DateUtil.MILLIS_PER_DAY;
185 }
186
187 return days;
188 };
189
190 /**
191 * Returns a list of all of the days contained by |timeRange|,
192 * as UTC days.
193 *
194 * @param {TimeRange} timeRange.
195 * @return {array<TimeRange>}
196 */
197 DateUtil.GetUTCDaysInRange = function(timeRange) {
198 var days = [];
199
200 var t = timeRange.startTime;
201 t -= DateUtil.MILLIS_PER_DAY;
202
203 while (t >= timeRange.endTime) {
204 var day = DateUtil.GetUTCDayRange(t);
205 days.push(day);
206 t -= DateUtil.MILLIS_PER_DAY;
207 }
208
209 return days;
210 };
211
212 /**
213 * Formats |t| as something human readable in the user's current locale.
214 *
215 * @param {int} t Unix timestamp in milliseconds.
216 * @return {string}
217 */
218 DateUtil.FormatAsLocalDate = function(t) {
219 // Format the date into something readable.
220 var d = new Date();
221 d.setTime(t);
222 return d.toLocaleString();
223 };
224
225 /**
226 * Formats |t| as ISO-8601 in local time.
227 *
228 * @param {int} t Unix timestamp in milliseconds.
229 * @return {string}
230 */
231 DateUtil.FormatLocaleISO = function(t) {
232 // Formats the date into something readable.
233 var d = new Date();
234 d.setTime(t);
235 return d.getFullYear() + "-" +
236 zeroPad(d.getMonth() + 1, 2) + "-" +
237 zeroPad(d.getDay(), 2) + " " +
238 zeroPad(d.getHours() + 1, 2) + ":" +
239 zeroPad(d.getMinutes() + 1, 2) + ":" +
240 zeroPad(d.getSeconds() + 1, 2);
241 };
242
243 /**
244 * Formats |t| as ISO-8601 in UTC.
245 *
246 * @param {int} t Unix timestamp in milliseconds.
247 * @return {string}
248 */
249 DateUtil.FormatUTCISO = function(t) {
250 // Formats the date into something readable.
251 var d = new Date();
252 d.setTime(t);
253 return d.getUTCFullYear() + "-" +
254 zeroPad(d.getUTCMonth() + 1, 2) + "-" +
255 zeroPad(d.getUTCDay(), 2) + " " +
256 zeroPad(d.getUTCHours() + 1, 2) + ":" +
257 zeroPad(d.getUTCMinutes() + 1, 2) + ":" +
258 zeroPad(d.getUTCSeconds() + 1, 2);
259 };
260
261 /**
262 * Converts milliseconds to seconds (rounding down).
263 *
264 * @param {int} millis
265 * @return {int}
266 */
267 DateUtil.MillisToSeconds = function(millis) {
268 return parseInt((millis / 1000).toFixed(0));
269 };
270
271 /**
272 * Helper function to add required zero characters to a string.
273 **/
274 function zeroPad(num, width) {
275 num = num.toString();
276 while (num.length < width) {
277 num = "0" + num;
278 }
279 return num;
280 }
OLDNEW
« no previous file with comments | « appengine_apps/chromium_status/static/js/common/bind.js ('k') | appengine_apps/chromium_status/static/js/common/debug.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698