January 14, 2016 aliases formatting time date

'(The 14-Digit Timestamp, et al)

Audience: anyone

The so-called “14-digit timestamp” is turning up in more and more places. For those unfamiliar, it takes the form of:


I’ve seen it recently in Migratus and Rails/Cap deploys (current symlink), and it’s been used widely by MySQL. They’re pretty handy for setting to-the-second precision for ordering of things, like database migrations. And, they’re fairly human-parsable.

At the time of this writing, the 14-digit time is 20160114180301. If you find that hard to eye-parse, here’s a (really ugly) shell function to help:

dt14p() { sed -r 's/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/\1-\2-\3 \4:\5:\6/' <<< $1 }
% dt14p 20160114184117
2016-01-14 18:41:17

This enables date (and your eyes) to parse it.

Something I see missed on occasion is the use of UTC on such stamps. That seems pretty important if you’re using these for ordering, and have a time zone-distributed team.

14s are not to be confused with the 10-digit epoch-second-stamp, or MongoDB’s similar 64-bit 13-digit stamp (1452758412345).

You might also find weekstamps useful for writing weekly reports and other organization.

Generating a 14-digit stamp

date --utc +%Y%m%d%H%M%S

Why not make it an alias?

alias dt14='date --utc +%Y%m%d%H%M%S'

And use it often:

mkdir `dt14`-something

(I usually find the 8-digit datestamp dt8 to be sufficient for directory names.)

While we’re at it, here are some other useful stamping aliases. I use the dt prefix to make them easy to both remember and tab-complete. Some of these I use every day. Put them in your .zshrc and enjoy!

alias dt='date +%Y%m%d'    # frequent 8-digit stamp: 20160114
alias dt8=dt
alias day=dt
alias dts='date +%s'       # seconds since epoch: 1452795263
alias dte=dts
alias dt10=dts
alias dtep='print "use this to parse: date -d @123..."'  # parse the epoch
alias dtp='date -d '       # parse
alias dttm='date --utc +%Y%m%d%H%M%S'  # datetime: 20160114192940
alias dt14=dttm
alias dt13=dtb
alias dto='date +%Y%j'     # ordinal date: 2016016 (16th day of year)
alias dt7=dto
# https://en.wikipedia.org/wiki/ISO_week_date
alias dtwk='date +W%V'     # week date: W02
alias dtyrwk='date +%GW%V' # year week date: 2016W02
# ISO 8601
alias dtw='date +%G-W%V-%' # year week: 2016-W02-4
alias dti='date -I'        # ISO-8601: 2016-01-14
alias dt-=dti
alias dti='date -u +%Y%m%dT%H%M%S'        # 20160111T173110Z
alias dti-='date -u +%Y-%m-%dT%H:%M:%SZ'  # 2016-01-11T17:31:10Z
alias dti0='date -u -Iseconds'            # 2016-01-11T17:31:10+0000
alias dto-='date +%Y-%j'                  # ordinal date: 2016-016
alias dtb="date-iso2bson.rb <<<\"'`dt-`'\""  # for mongo bson

(Note: this is using date from GNU Coreutils. Yours may be different if you’re not on Linux.)