mirror of
https://github.com/gurnec/removeddit.git
synced 2026-03-11 08:54:27 +00:00
30 lines
865 B
JavaScript
30 lines
865 B
JavaScript
// UTC time handling, very usefull when dealing with elasticsearch
|
|
var Time = (function () {
|
|
return {
|
|
utc() {
|
|
return Math.floor(_.now() / 1000)
|
|
},
|
|
|
|
toUTC(timeString) {
|
|
const parts = timeString.split(/[a-zA-Z]+/)
|
|
let times = 1
|
|
|
|
// Number before the time (e.g. 2years or 12hour)
|
|
if (parts.length === 2 && parts[0] !== '') {
|
|
times = _.parseInt(parts[0])
|
|
}
|
|
|
|
if (_.includes(timeString, 'hour')) return times * 3600
|
|
if (_.includes(timeString, 'day')) return times * 86400
|
|
if (_.includes(timeString, 'week')) return times * 604800
|
|
if (_.includes(timeString, 'month')) return times * 2592000
|
|
if (_.includes(timeString, 'year')) return times * 31536000
|
|
|
|
return Time.utc()
|
|
},
|
|
|
|
difference(timeString) {
|
|
return Time.utc() - Time.toUTC(timeString)
|
|
},
|
|
}
|
|
}())
|