jsonbashdate

Get date from a ms since epoch using Bash


I have a json file which needs to be parsed . It has date in long format , sometimes negative long Eg: -201269509000 . If i use this formula t=$(date -d @-201269509000 +'%Y-%m-%d %H:%M:%S'); . This gives an answer like -4408-01-10 00:56:48 .How do i sort this out ? I am trying to do this via shell script.


Solution

  • The answer is that date expects numbers in epoch seconds, and your json output is likely in epoch ms (if you know that 4408 BCE is incorrect). Anubhava's comment is wrong, as it assumes your input is positive seconds since epoch.

    seconds=-201269509000
    t=$(date -d @${seconds%???} +'%Y-%m-%d %H:%M:%S')
    echo $t
    > 1963-08-16 04:48:11
    

    ${var%???} removes the last 3 characters from $var. This is an example of parameter expansion.