Skip to content

Commit c7dbdcd

Browse files
authored
Add UTC support (#178)
1 parent e14fcb3 commit c7dbdcd

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

github-static-time.user.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name GitHub Static Time
3-
// @version 1.0.10
3+
// @version 1.1.0
44
// @description A userscript that replaces relative times with a static time formatted as you like it
55
// @license MIT
66
// @author Rob Garrison
@@ -26,6 +26,7 @@
2626
let busy = false;
2727
let timeFormat = GM_getValue("ghst-format", "LLL");
2828
let locale = GM_getValue("ghst-locale", "en");
29+
let useUTC = GM_getValue("ghst-utc", "false");
2930

3031
// list copied from
3132
// https://github.com/moment/momentjs.com/blob/master/data/locale.js
@@ -195,14 +196,17 @@
195196
return;
196197
}
197198
el = els[indx];
198-
time = el.getAttribute("datetime") || "";
199-
if (el && time) {
199+
time = moment(el.getAttribute("datetime") || "");
200+
if (el && time.isValid()) {
201+
if (useUTC === "true") {
202+
time = time.utc();
203+
}
200204
if (tempFormat) {
201-
formatted = moment(time).format(tempFormat);
205+
formatted = time.format(tempFormat);
202206
el.textContent = formatted;
203207
el.title = formatted;
204208
} else {
205-
formatted = moment(time).format(timeFormat);
209+
formatted = time.format(timeFormat);
206210
node = block.cloneNode(true);
207211
node.setAttribute("datetime", time);
208212
node.textContent = formatted;
@@ -237,7 +241,7 @@
237241
#ghst-settings-inner { position:fixed; left:50%; top:50%; width:25rem;
238242
transform:translate(-50%,-50%); box-shadow:0 .5rem 1rem #111;
239243
color:#c0c0c0 }
240-
#ghst-settings-inner .boxed-group-inner { height: 205px; }
244+
#ghst-settings-inner .boxed-group-inner { height: 255px; }
241245
#ghst-footer { clear:both; border-top:1px solid rgba(68, 68, 68, .3);
242246
padding-top:5px; }
243247
`);
@@ -262,13 +266,23 @@
262266
<br>
263267
</dd>
264268
</dl>
269+
<dl class="form-group flattened">
270+
<dt>
271+
<label for="ghst-utc">Show UTC time (use "z" in format below)</label>
272+
</dt>
273+
<dd>
274+
<div class="form-checkbox">
275+
<input id="ghst-utc" type="checkbox" class="float-right">
276+
</div>
277+
<br>
278+
</dd>
279+
</dl>
265280
<dl class="form-group flattened">
266281
<dt>
267282
<label for="ghst-format">
268-
<p>Set <a href="https://momentjs.com/docs/#/displaying/format/">
283+
Set <a href="https://momentjs.com/docs/#/displaying/format/" target="_blank">
269284
MomentJS
270285
</a> format (e.g. "MMMM Do YYYY, h:mm A"):
271-
</p>
272286
</label>
273287
</dt>
274288
<dd>
@@ -282,6 +296,7 @@
282296
</div>
283297
</div>`;
284298
$("body").appendChild(div);
299+
$("#ghst-utc").checked = useUTC === "true";
285300
on($("#ghst-settings"), "click", closePanel);
286301
on($("body"), "keyup", event => {
287302
if (
@@ -297,13 +312,14 @@
297312
});
298313
on($("#ghst-settings-inner"), "click", event => {
299314
event.stopPropagation();
300-
event.preventDefault();
315+
// event.preventDefault();
301316
});
302317
on($("#ghst-save"), "click", () => {
303318
closePanel();
304319
update("save");
305320
});
306321
on($("#ghst-locale"), "change", update);
322+
on($("#ghst-utc"), "change", update);
307323
on($("#ghst-format"), "change", update);
308324
on($("#ghst-cancel"), "click", closePanel);
309325
}
@@ -319,14 +335,18 @@
319335
if (mode === "revert") {
320336
$("#ghst-locale").value = locale;
321337
$("#ghst-format").value = timeFormat;
338+
$("#ghst-utc").checked = useUTC === "true";
322339
}
323340
let loc = $("#ghst-locale").value || "en";
324341
let time = $("#ghst-format").value || "LLL";
342+
let utc = $("#ghst-utc").checked ? "true" : "false";
325343
if (mode === "save") {
326344
timeFormat = time;
327345
locale = loc;
346+
useUTC = utc;
328347
GM_setValue("ghst-format", timeFormat);
329348
GM_setValue("ghst-locale", locale);
349+
GM_setValue("ghst-utc", useUTC);
330350
}
331351
moment.locale(loc);
332352
staticTime(time);

0 commit comments

Comments
 (0)