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

Side by Side Diff: milo/appengine/frontend/static/common/js/time.js

Issue 2835193006: Milo: Add timestamp name and tooltip (Closed)
Patch Set: Review Created 3 years, 7 months 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
1 // Copyright 2016 The LUCI Authors. All rights reserved. 1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 // A Series of time based utilites for Milo. 5 // A Series of time based utilites for Milo.
6 // Requires: moment.js, moment-timezone.js, jquery, jquery-ui
6 7
7 8
8 (function(window) { 9 (function(window) {
9 'use strict'; 10 'use strict';
10 11
11 var milo = window.milo || {}; 12 var milo = window.milo || {};
12 13
14 milo.tz = moment.tz.guess();
15
13 /** 16 /**
14 * Given a Date, return a time string in the user's local timezone. 17 * Given a Date, return a time string in the user's local timezone.
18 * Also return the time string in relative time from now, MTV time, and UTC
19 * time.
15 */ 20 */
16 milo.formatDate = function(t) { 21 milo.formatDate = function(t) {
17 if (!t || t.toString() == "Invalid Date") { 22 var mt = moment.tz(t, milo.tz);
18 return null; 23 if (!mt.isValid()) {
24 return null;
19 } 25 }
20 var shortDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; 26 var hover = mt.fromNow();
27 hover += "\n" + moment.tz(mt, "America/Los_Angeles").format("YYYY-MM-DD LT [ (MTV)]");
28 hover += "\n" + moment.tz(mt, "UTC").format("YYYY-MM-DD LT [(UTC)]");
21 29
22 var month = (t.getMonth() + 1); 30 return {
23 if (month < 10) { 31 main: mt.format("YYYY-MM-DD LT (z)"),
24 month = '0' + month; 32 hover: hover
25 } 33 }
26 var date = t.getDate(); 34 };
27 if (date < 10) { 35
28 date = '0' + date; 36 /**
37 * Given two Dates (or a Date and null, prepresenting "now"), return
38 * a duration string, with a hover string of the start/end time in the user's
39 * timezone and locale.
40 */
41 milo.formatDuration = function(start, end) {
42 var st = moment.tz(start, milo.tz);
43 if (!st.isValid()) {
44 return null;
29 } 45 }
30 var s = shortDayNames[t.getDay()] + ', '; 46 var hover = st.format("[Started: ] YYYY-MM-DD LT (z)")
31 s += t.getFullYear() + '-' + month + '-' + date + ' '; 47 hover += "\nEnded: "
32 s += t.toLocaleTimeString() + ' (local time)'; 48 if (end == null) {
33 49 hover += "N/A";
34 return s; 50 } else {
51 var et = moment.tz(end, milo.tz)
52 if (!et.isValid()) {
53 return null
54 }
55 hover += et.format("YYYY-MM-DD LT (z)");
56 }
57 return hover;
35 }; 58 };
36 59
37 milo.makeTimesLocal = function() { 60 milo.makeTimesLocal = function() {
38 var timeSpans = document.getElementsByClassName('local-time'); 61 var timeSpans = document.getElementsByClassName('local-time');
39 for (var i = 0; i < timeSpans.length; i++) { 62 for (var i = 0; i < timeSpans.length; i++) {
40 var span = timeSpans[i]; 63 var span = timeSpans[i];
41 try { 64 try {
42 var oldTimestamp = span.innerText; 65 var oldTimestamp = span.innerText;
43 var timestamp = span.getAttribute('data-timestamp'); 66 var timestamp = span.getAttribute('data-timestamp');
44 var date = new Date(parseInt(timestamp, 10)); 67 var date = new Date(parseInt(timestamp, 10));
45 var newTimestamp = milo.formatDate(date); 68 var newTimestamp = milo.formatDate(date);
46 if (newTimestamp != null) { 69 if (newTimestamp != null) {
47 span.innerText = newTimestamp; 70 span.innerText = newTimestamp.main;
48 span.setAttribute("title", oldTimestamp) 71 span.setAttribute("title", newTimestamp.hover);
49 } 72 }
50 } 73 } catch (e) {
51 catch (e) {
52 console.error('could not convert time of span', span, 'to local:', e) 74 console.error('could not convert time of span', span, 'to local:', e)
53 } 75 }
54 } 76 }
55 }; 77 };
56 78
79 milo.annotateDurations = function() {
80 var durations = document.getElementsByClassName('duration');
81 for (var i = 0; i < durations.length; i++) {
82 var dur = durations[i];
83 try {
84 var start = dur.getAttribute('data-starttime');
85 var end = dur.getAttribute('data-endtime');
86 var hover = milo.formatDuration(start, end);
87 if (hover != null) {
88 dur.setAttribute("title", hover);
89 }
90 } catch (e) {
91 console.error('could not annotate duration', dur, e)
92 }
93 }
94 }
95
57 window.milo = milo; 96 window.milo = milo;
58 97
59 }(window)); 98 }(window));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698