Gets the current UTC time to seconds accuracy (max available with date
).
Using date
and sed
so these can run in Alpine Linux.
For example: 1999/12/31/23/59/59
date -Iseconds -u | sed -En '1{s/[-+:T]/\//g; s/^(.{0,19}).*$/\1/g; p}'
(new Date()).toISOString().replace(/[-:T]/g, '/').slice(0, 19)
const ensure_int = R.unless(R.is(Number), parseInt)
const timestamp_to_iso8601_text = R.pipe(ensure_int, R.constructN(1, Date), R.invoker(0, 'toISOString'))
const as_path = R.pipe(timestamp_to_iso8601_text, R.replace(/[-:T]/g, '/'), R.slice(0, 19))
as_path(Date.now())
To include milliseconds:
const as_path = R.pipe(timestamp_to_iso8601_text, R.replace(/[-.:T]/g, '/'), R.init)
Note the Ramda example is longer because it creates other functions that may be useful.
To convert path back to timestamp:
const collapse_path = R.pipe(R.split('/'), R.map(parseInt), R.apply(R.constructN(7, Date)), R.invoker(0, 'valueOf'))
For example: 19991231t235959
With cut
and tr
:
date -Is -u | cut -c -19 | tr -d '-' | tr -d ':' | tr 'T' 't'
With sed
:
date -Iseconds -u | sed -En '1{s/[-+:]//g; s/^(.{0,15}).*$/\L\1/g; p}'