OLD | NEW |
| (Empty) |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
2 // Use of this source code is governed under the Apache License, Version 2.0 | |
3 // that can be found in the LICENSE file. | |
4 | |
5 // A Series of time based utilites for Milo. | |
6 // Requires: moment.js, moment-timezone.js, jquery, jquery-ui | |
7 | |
8 | |
9 (function(window) { | |
10 'use strict'; | |
11 | |
12 var milo = window.milo || {}; | |
13 | |
14 milo.tz = moment.tz.guess(); | |
15 | |
16 /** | |
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. | |
20 */ | |
21 milo.formatDate = function(t) { | |
22 var mt = moment.tz(t, milo.tz); | |
23 if (!mt.isValid()) { | |
24 return null; | |
25 } | |
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)]"); | |
29 | |
30 return { | |
31 main: mt.format("YYYY-MM-DD LT (z)"), | |
32 hover: hover | |
33 } | |
34 }; | |
35 | |
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; | |
45 } | |
46 var hover = st.format("[Started: ] YYYY-MM-DD LT (z)") | |
47 hover += "\nEnded: " | |
48 if (end == null) { | |
49 hover += "N/A"; | |
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; | |
58 }; | |
59 | |
60 milo.makeTimesLocal = function() { | |
61 var timeSpans = document.getElementsByClassName('local-time'); | |
62 for (var i = 0; i < timeSpans.length; i++) { | |
63 var span = timeSpans[i]; | |
64 try { | |
65 var oldTimestamp = span.innerText; | |
66 var timestamp = span.getAttribute('data-timestamp'); | |
67 var date = new Date(parseInt(timestamp, 10)); | |
68 var newTimestamp = milo.formatDate(date); | |
69 if (newTimestamp != null) { | |
70 span.innerText = newTimestamp.main; | |
71 span.setAttribute("title", newTimestamp.hover); | |
72 } | |
73 } catch (e) { | |
74 console.error('could not convert time of span', span, 'to local:', e) | |
75 } | |
76 } | |
77 }; | |
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 | |
96 window.milo = milo; | |
97 | |
98 }(window)); | |
OLD | NEW |