Converting Unix timestamps

Sometimes, we need to convert Unix timestamps (seconds since January 1st, 1970) to human-readable dates. For example, we might transform 1539561600 to 2018-10-15 00:00 UTC.

There are multiple online services that do this, I like unixtimestamp.com.

Every now and then we need to batch-convert timestamps. The date command shipped on Linux distributions does this nicely:

date "+%c" --date=@1539561600

I recently ran into a similar problem when logfiles contained Unix timestamps instead of human-readable dates. Using date seemed a bit clumsy here. Fortunately, Superuser.com had a nice solution involving Vim. The following sequence converts the timestamp under the cursor and records a macro q to facilitate future conversions:

qq                             " start recording
"mciw                          " put time in register m and replace it…
<C-r>=strftime("%c", @m)<CR>   " …with localized datetime
<Esc>                          " exit insert mode
q                              " stop recording

Quick and convenient — and easily incorporated into a macro to convert timestamps across the entire file.


Vim notes

Scripts

  • UpdateModDate.vim is a vim script I have written for updating the date-stamp of these pages automatically every time I save the files from within vim.

It searches for lines marked with %DATE_TAG% and updates the date on these lines every time the file is saved. Example:

   Last Modified: Tue Jun  2 04:44:58 UTC 2004      // %DATE_TAG%

Bugs

  • I've hit a bug in vim related to html highlighting that would cause get really really slow. This has already been fixed though. The interested might look at the detailed description.