Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 | 6 |
| 7 | 7 |
| 8 (function(window) { | 8 (function(window) { |
| 9 'use strict'; | 9 'use strict'; |
| 10 | 10 |
| 11 var milo = window.milo || {}; | 11 var milo = window.milo || {}; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Given a Date, return a time string in the user's local timezone. | 14 * Given a Date, return a time string in the user's local timezone. |
| 15 */ | 15 */ |
| 16 milo.formatDate = function(t) { | 16 milo.formatDate = function(t) { |
| 17 if (!t || t.toString() == "Invalid Date") { | 17 if (!t || t.toString() == "Invalid Date") { |
| 18 return null; | 18 return null; |
| 19 } | 19 } |
| 20 var shortDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; | 20 var shortDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; |
| 21 var offset = -(new Date()).getTimezoneOffset(); | |
| 22 var offsetHr = Math.abs(Math.round(offset / 60)); | |
| 23 var offsetMin = Math.abs(Math.abs(offset) - (offsetHr * 60)); | |
| 24 if (offsetHr < 10) { | |
| 25 offsetHr = '0' + offsetHr; | |
| 26 } | |
| 27 if (offsetMin < 10) { | |
| 28 offsetMin = '0' + offsetMin; | |
| 29 } | |
| 30 | 21 |
| 31 var month = (t.getMonth() + 1); | 22 var month = (t.getMonth() + 1); |
| 32 if (month < 10) { | 23 if (month < 10) { |
| 33 month = '0' + month; | 24 month = '0' + month; |
| 34 } | 25 } |
| 35 var date = t.getDate(); | 26 var date = t.getDate(); |
| 36 if (date < 10) { | 27 if (date < 10) { |
| 37 date = '0' + date; | 28 date = '0' + date; |
| 38 } | 29 } |
| 39 var s = shortDayNames[t.getDay()] + ', '; | 30 var s = shortDayNames[t.getDay()] + ', '; |
| 40 s += t.getFullYear() + '-' + month + '-' + date + ' '; | 31 s += t.getFullYear() + '-' + month + '-' + date + ' '; |
| 41 s += t.toLocaleTimeString(); | 32 s += t.toLocaleTimeString() + ' (local time)'; |
|
mithro
2017/03/02 06:29:23
This text is not useful, who's local time?
Please
| |
| 42 | 33 |
| 43 return s; | 34 return s; |
| 44 }; | 35 }; |
| 45 | 36 |
| 46 milo.makeTimesLocal = function() { | 37 milo.makeTimesLocal = function() { |
| 47 var timeSpans = document.getElementsByClassName('local-time'); | 38 var timeSpans = document.getElementsByClassName('local-time'); |
| 48 for (var i = 0; i < timeSpans.length; i++) { | 39 for (var i = 0; i < timeSpans.length; i++) { |
| 49 var span = timeSpans[i]; | 40 var span = timeSpans[i]; |
| 50 try { | 41 try { |
| 51 var oldTimestamp = span.innerText; | 42 var oldTimestamp = span.innerText; |
| 52 var timestamp = span.getAttribute('data-timestamp'); | 43 var timestamp = span.getAttribute('data-timestamp'); |
| 53 var date = new Date(parseInt(timestamp, 10)); | 44 var date = new Date(parseInt(timestamp, 10)); |
| 54 var newTimestamp = milo.formatDate(date); | 45 var newTimestamp = milo.formatDate(date); |
| 55 if (newTimestamp != null) { | 46 if (newTimestamp != null) { |
| 56 span.innerText = newTimestamp; | 47 span.innerText = newTimestamp; |
| 57 span.setAttribute("title", oldTimestamp) | 48 span.setAttribute("title", oldTimestamp) |
| 58 } | 49 } |
| 59 } | 50 } |
| 60 catch (e) { | 51 catch (e) { |
| 61 console.error('could not convert time of span', span, 'to local:', e) | 52 console.error('could not convert time of span', span, 'to local:', e) |
| 62 } | 53 } |
| 63 } | 54 } |
| 64 }; | 55 }; |
| 65 | 56 |
| 66 window.milo = milo; | 57 window.milo = milo; |
| 67 | 58 |
| 68 }(window)); | 59 }(window)); |
| OLD | NEW |