Last active
August 30, 2021 14:16
-
-
Save zaneb/85e991608a1cc1cfc9ae49a53cda36d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const iso8601RegEx = /([0-9]{4})-([0-1][0-9])-([0-3][0-9])T([0-2][0-9]):([0-5][0-9])Z/ | |
function format2Digits(num) { | |
let asStr = '0' + num.toString() | |
return asStr.slice(-2) | |
} | |
function EntryTimestamp(node) { | |
this.node = node | |
this.date = function() { | |
let iso8601 = node.title | |
let timeFields = iso8601.match(iso8601RegEx) | |
let timestamp | |
if (timeFields != null) { | |
timestamp = Date.UTC(timeFields[1], timeFields[2]-1, timeFields[3], | |
timeFields[4], timeFields[5]) | |
} else { | |
timestamp = Date.parse(iso8601) | |
} | |
return new Date(timestamp) | |
}() | |
if (Intl == undefined) { | |
this.dayFormatter = { | |
format: function(date) { | |
return date.getDate() | |
} | |
} | |
this.monthFormatter = { | |
format: function(date) { | |
return ['Jan', 'Feb', 'Mar', | |
'Apr', 'May', 'Jun', | |
'Jul', 'Aug', 'Sep', | |
'Oct', 'Nov', 'Dec'][date.getMonth()] | |
} | |
} | |
this.yearFormatter = { | |
format: function(date) { | |
return date.getFullYear() | |
} | |
} | |
this.timeFormatter = { | |
format: function(date) { | |
let hours = format2Digits(date.getHours()) | |
let mins = format2Digits(date.getMinutes()) | |
return `${hours}:${mins}` | |
} | |
} | |
} else { | |
this.dayFormatter = new Intl.DateTimeFormat([], { | |
day: "numeric" | |
}) | |
this.monthFormatter = new Intl.DateTimeFormat([], { | |
month: "short" | |
}) | |
this.yearFormatter = new Intl.DateTimeFormat([], { | |
year: "numeric" | |
}) | |
this.timeFormatter = new Intl.DateTimeFormat([], { | |
hour: "numeric", | |
minute: "numeric", | |
hour12: false | |
}) | |
} | |
this.getFieldText = function(fieldClass) { | |
switch (fieldClass) { | |
case "day": | |
return this.dayFormatter.format(this.date) | |
case "mo": | |
return this.monthFormatter.format(this.date) | |
case "yr": | |
return this.yearFormatter.format(this.date) | |
case "time": | |
return this.timeFormatter.format(this.date) | |
} | |
} | |
this.localtimeNode = function() { | |
let node = this.node.cloneNode(true) | |
for (let i in node.children) { | |
let c = node.children[i] | |
let t = this.getFieldText(c.className) | |
if (t != undefined) { | |
let text = document.createTextNode(t) | |
c.replaceChild(text, c.firstChild) | |
} | |
} | |
return node | |
} | |
this.updateDisplay = function() { | |
let newNode = this.localtimeNode() | |
this.node.parentNode.replaceChild(newNode, node) | |
this.node = newNode | |
} | |
} | |
function* getEntryTimestamps() { | |
let dts = document.getElementsByClassName('datetime') | |
for (let dt of dts) { | |
for (let node of dt.getElementsByClassName('iso8601')) { | |
if (node.getElementsByClassName('time').length > 0) { | |
yield new EntryTimestamp(node) | |
} | |
} | |
} | |
} | |
function switchAllEntryTimestampsToLocalTime() { | |
for (let timestamp of getEntryTimestamps()) { | |
timestamp.updateDisplay() | |
} | |
} | |
switchAllEntryTimestampsToLocalTime() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment