Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash convert epoch to date, showing wrong time

Tags:

date

bash

epoch

How come date is converting to wrong time?

result=$(ls /path/to/file/File.*) #/path/to/file/File.1361234760790  currentIndexTime=${result##*.} echo "$currentIndexTime" #1361234760790  date -d@"$currentIndexTime" #Tue 24 Oct 45105 10:53:10 PM GMT 
like image 545
bobbyrne01 Avatar asked May 01 '13 01:05

bobbyrne01


People also ask

How do I convert epoch time to real time?

Convert from epoch to human-readable datemyString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch. =(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number).

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 (seconds in a day) floored and add 719163 (the days up to the year 1970) to pass to it. Awesome, this is as manual as it gets.

How do you convert date to epoch time in bash?

Use the build-in date command and instruct it to output the number of seconds since 1970-01-01 00:00:00 UTC. You can do this by passing a format string as parameter to the date command. The format string for UNIX epoch time is '%s'. To convert a specific date and time into UNIX epoch time, use the -d parameter.


2 Answers

This particular timestamp is in milliseconds since the epoch, not the standard seconds since the epoch. Divide by 1000:

$ date -d @1361234760.790 Mon Feb 18 17:46:00 MST 2013 
like image 197
andrewdotn Avatar answered Oct 11 '22 15:10

andrewdotn


For Mac OS X, it's date -r <timestamp_in_seconds_with_no_fractions>

$ date -r 1553024528 Tue Mar 19 12:42:08 PDT 2019 

or

$ date -r `expr 1553024527882 / 1000` Tue Mar 19 12:42:07 PDT 2019 

or

$ date -r $((1553024527882/1000)) Tue Mar 19 12:42:07 PDT 2019 
like image 36
Marcus Avatar answered Oct 11 '22 15:10

Marcus