csvkit icon indicating copy to clipboard operation
csvkit copied to clipboard

Convert date and time stamps from Epoch

Open ethman42 opened this issue 9 years ago • 5 comments

I would love to have a feature that would let me convert epoch timestamps to human-readable. Any thoughts on getting this working.

ethman42 avatar May 01 '17 19:05 ethman42

csvkit offers --date-format and --datetime-format for parsing dates/times, but apparently Python's strptime doesn't support %s (which in other languages allows the parsing of timestamps). So, we'll have to pursue a special case.

jpmckinney avatar May 07 '17 20:05 jpmckinney

No other interest expressed since 2017, and no easy solution for this feature, so closing.

jpmckinney avatar Feb 23 '19 18:02 jpmckinney

re-opening this, wondering if any python or other advancements make this more supportable?

tb582 avatar Aug 25 '21 16:08 tb582

Still nothing in Python: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

This feature would require new logic to use date.fromtimestamp, but it's not a simple fix like %s would have been.

jpmckinney avatar Aug 30 '21 17:08 jpmckinney

It's possible to kludge it with GNU Awk strftime, assuming you manually prepend headers (to avoid the header being converted to a datetime) eg:

Given input.csv of:

a,b,c,d
foo,bar,1674642543,baz
spam,eggs,1674645576,ham

the following can be used:

tail -n+2 input.csv |
  awk -F, '{ print $1 "," $2 "," strftime("%c",$3) "," $4 }' |
  sed -e "1i $(head -n1 input.csv)" |
  csvlook

where:

  • tail -n+2 input.csv strips the headers
  • awk -F, '{ print $1 "," $2 "," strftime("%c",$3) "," $4 }' transforms the timestamp
  • sed -e "1i $(head -n1 input.csv)" prepends the headers

resulting in:

| a    | b    | c                            | d   |
| ---- | ---- | ---------------------------- | --- |
| foo  | bar  | Wed 25 Jan 2023 10:29:03 GMT | baz |
| spam | eggs | Wed 25 Jan 2023 11:19:36 GMT | ham |

damonmcminn avatar Jan 25 '23 13:01 damonmcminn