Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
|
nodir
2017/04/29 01:26:40
we should convert this to TypeScript at some point
hinoka
2017/05/02 03:33:14
Acknowledged.
| |
| 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 int he user's | |
|
nodir
2017/04/29 01:26:40
in the
hinoka
2017/05/02 03:33:14
Done.
| |
| 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()) { | |
|
nodir
2017/04/29 01:26:40
if start time is invalid, we return null, but if t
hinoka
2017/05/02 03:33:13
Done.
| |
| 53 hover += et.format("YYYY-MM-DD LT (z)"); | |
| 54 } | |
| 55 } | |
| 56 return hover; | |
| 35 }; | 57 }; |
| 36 | 58 |
| 37 milo.makeTimesLocal = function() { | 59 milo.makeTimesLocal = function() { |
| 38 var timeSpans = document.getElementsByClassName('local-time'); | 60 var timeSpans = document.getElementsByClassName('local-time'); |
| 39 for (var i = 0; i < timeSpans.length; i++) { | 61 for (var i = 0; i < timeSpans.length; i++) { |
| 40 var span = timeSpans[i]; | 62 var span = timeSpans[i]; |
| 41 try { | 63 try { |
| 42 var oldTimestamp = span.innerText; | 64 var oldTimestamp = span.innerText; |
| 43 var timestamp = span.getAttribute('data-timestamp'); | 65 var timestamp = span.getAttribute('data-timestamp'); |
| 44 var date = new Date(parseInt(timestamp, 10)); | 66 var date = new Date(parseInt(timestamp, 10)); |
| 45 var newTimestamp = milo.formatDate(date); | 67 var newTimestamp = milo.formatDate(date); |
| 46 if (newTimestamp != null) { | 68 if (newTimestamp != null) { |
| 47 span.innerText = newTimestamp; | 69 span.innerText = newTimestamp.main; |
| 48 span.setAttribute("title", oldTimestamp) | 70 span.setAttribute("title", newTimestamp.hover); |
| 49 } | 71 } |
| 50 } | 72 } catch (e) { |
| 51 catch (e) { | |
| 52 console.error('could not convert time of span', span, 'to local:', e) | 73 console.error('could not convert time of span', span, 'to local:', e) |
| 53 } | 74 } |
| 54 } | 75 } |
| 55 }; | 76 }; |
| 56 | 77 |
| 78 milo.annotateDurations = function() { | |
| 79 var durations = document.getElementsByClassName('duration'); | |
| 80 for (var i = 0; i < durations.length; i++) { | |
| 81 var dur = durations[i]; | |
| 82 try { | |
| 83 var start = dur.getAttribute('data-starttime'); | |
| 84 var end = dur.getAttribute('data-endtime'); | |
| 85 var hover = milo.formatDuration(start, end); | |
| 86 if (hover != null) { | |
| 87 dur.setAttribute("title", hover); | |
| 88 } | |
| 89 } catch (e) { | |
| 90 console.error('could not annotated duration', dur, e) | |
|
nodir
2017/04/29 01:26:40
s/annotated/annotate/
hinoka
2017/05/02 03:33:13
Done.
| |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 | |
| 57 window.milo = milo; | 95 window.milo = milo; |
| 58 | 96 |
| 59 }(window)); | 97 }(window)); |
| OLD | NEW |