Linux milliseconds to date

Convert Unix timestamp to a date string

Is there a quick, one-liner way to convert a Unix timestamp to a date from the Unix command line? date might work, except it’s rather awkward to specify each element (month, day, year, hour, etc.), and I can’t figure out how to get it to work properly. It seems like there might be an easier way — am I missing something?

12 Answers 12

With date from GNU coreutils you can do:

# date -d @0 Wed Dec 31 19:00:00 EST 1969 

Alternatively, use strftime() . It’s not available directly from the shell, but you can access it via gawk. The %c specifier displays the timestamp in a locale-dependent manner.

# echo 0 | gawk '' Wed 31 Dec 1969 07:00:00 PM EST 

Make sure to eliminate the milliseconds from the timestamp (that is only 10 digits instead of 13), otherwise you’ll get invalid results (this is true for macOS’ date).

I thought this was not working, until I realised I had a timestamp in millis. Chop off the last 3 digits, and you get the accurate time

The «Convert Unix Timestamp» link is broken. Archived version: web.archive.org/web/20200930143943/https://www.antonolsen.com/… . I would edit the post with the new URL, but the edit queue is full right now.

date -d @1278999698 +’%Y-%m-%d %H:%M:%S’ Where the number behind @ is the number in seconds

This solution works with versions of date which do not support date -d @ . It does not require AWK or other commands. A Unix timestamp is the number of seconds since Jan 1, 1970, UTC so it is important to specify UTC in the input.

date -d '1970-01-01 1357004952 sec UTC' Mon Dec 31 17:49:12 PST 2012 

If you are on a Mac, then use:

Command for getting epoch:

Specifying UTC flag is a lot simpler than that . gnu-date -u -d’@1357004952′ ; bsd-date -u -r 1357004952 :: Tue Jan 1 01:49:12 UTC 2013 Tue Jan 1 01:49:12 UTC 2013

You may have misread the answer. My answer is for cases when you do not have support for date -d @ and when you want to display the output in your local time zone.

Читайте также:  Find in linux vim

As @TomMcKenzie says in a comment to another answer, date -r 123456789 is arguably a more common (i.e. more widely implemented) simple solution for times given as seconds since the Unix Epoch, but unfortunately there’s no universal guaranteed portable solution.

The -d option on many types of systems means something entirely different than GNU Date’s —date extension. Sadly GNU Date doesn’t interpret -r the same as these other implementations. So unfortunately you have to know which version of date you’re using, and many older Unix date commands don’t support either option.

Even worse, POSIX date recognizes neither -d nor -r and provides no standard way in any command at all (that I know of) to format a Unix time from the command line (since POSIX Awk also lacks strftime() ). (You can’t use touch -t and ls because the former does not accept a time given as seconds since the Unix Epoch.)

Note though The One True Awk available direct from Brian Kernighan does now have the strftime() function built-in as well as a systime() function to return the current time in seconds since the Unix Epoch), so perhaps the Awk solution is the most portable.

Источник

Shell linux milliseconds time to date

Rounding in times is only relevant when you look at time differences and not at absolute times. Solution 1: You may use this : is done to convert milli-sec value to second value before calling command to convert into format.

I want this command in windows with cygwin installed, so any Linux command will work for me.

date +%H:%M:%S:%N will give you the current time with nano seconds, you could then chop off however many digits or rearrange the time to how you wish to have it.

date —help can give you some other configuration options

If you want to get milliseconds instead of nanoseconds, you may simply use %3N to truncate the nanoseconds to the 3 most significant digits:

$ date +"%Y-%m-%d %H:%M:%S,%3N" 2014-01-08 16:00:12,746 
$ date +"%F %T,%3N" 2014-01-08 16:00:12,746 

testet with »GNU bash, Version 4.2.25(1)-release (i686-pc-linux-gnu)«

Also tested successfully in my cygwin installation.

But be aware, that %N may not implemented depending on your target system or bash version. Tested on an embedded system »GNU bash, version 4.2.37(2)- release (arm -buildroot-linux-gnueabi)« there was no %N :

date +"%F %T,%N" 2014-01-08 16:44:47,%N 

Here is the command where you can print the json-like ISO format current time :

Читайте также:  Linux монитор сетевых подключений

Linux comand get milliseconds Code Example, Returns the number of seconds + current nanoseconds. date +%s%N.

Rounding of the millisecond part in Linux datetime

So I have the date format like this : 2019-10-19 23:55:42.797 and I want the millisecond part to be round of into the second so the output should look something like this: 2019-10-19 23:55:43

but it’s giving me output like 2019-10-19 23:55:42

How should I do this in Linux bash shell?

This can be done in a single awk like this:

s='2020-12-31 23:59:59.501' awk -F. 'gsub(/[-:]/, " ", $1) < dt = mktime($1) if ($2 >= 500) dt++ print strftime("%F %X", dt) >'  

The behaviour you observe is as expected. The format specifiers represent the actual quantity without rounding. Imagine you would include rounding and you have the time "2019-10-19 23:55:42.797" but you are not interested in seconds and set the format to "%F %H:%M", do you want to see "2019-10-19 23:55" or "2019-10-19 23:56", and even further. Imagine you have the time "2020-12-31 23:59:59.501" with format "%F %T", do you want it to show "2021-01-01 00:00:00" or "2020-12-31 23:59:59". While we all want 2020 to finish as soon as possible, the latter still remains the correct time representation.

Rounding in times is only relevant when you look at time differences and not at absolute times. Hence, I strongly recommend not to implement any rounding and just use the output that date provides you.

However, if, for whatever reason you actually need to round the time to the nearest second, then you can do this:

epoch_ms=$(date -d "2019-10-19 23:55:42.797" "+%s%3N") epoch=$(( (epoch_ms + 500)/1000 )) date -d "@$epoch" "%F %T" 
date -d "@$(( ( $(date -d "2019-10-19 23:55:42.797" "+%s%3N") + 500 )/1000 ))" "+%F %T" 

Millisecond time in a shell script, This will give you how many miliseconds have passed since the beginning of the Unix epoch : date +%s%3N. You have to reduce the seconds*1000

How to convert column with millisecond timestamp to date in a file using bash?

I am trying to convert all values from the first column of a file that came in milliseconds timestamp to a date format %Y-%m-%d %H:%M:%S . But the output is Di 26. Mai 15:04:00 CEST 2020000 . I am using this command:

cat throughput-vs-latency-40K-16.csv | sed 's/^/echo "/; s/\(1\\)/`date -d @\1`/; s/$/"/' | bash 
"Time","pre_aggregate[0]-IN","pre_aggregate[10]-IN", 1590491460000. 1590491475000. 1590491490000,0,0,0,0,0,0,0,0, 1590491505000,290.51666666666665,290.53333333333336, 1590491535000,1027.15,1027.15,1028.85,1028.8666666666666, 1590491550000,1394.1166666666666,1394.15,1394.15,1394.1333333333334, 1590491565000,1475.5333333333333,1473.3666666666666, 

I would like to have the output bellow (please consider only the format and dismiss the correct time conversion):

"Time","pre_aggregate[0]-IN","pre_aggregate[10]-IN", "2020-05-07 08:05:45",0,0,0,0,0,0,0,0, "2020-05-07 08:06:45",290.51666666666665,290.53333333333336, "2020-05-07 08:07:45",1027.15,1027.15,1028.85,1028.8666666666666, "2020-05-07 08:05:45",1394.1166666666666,1394.15,1394.15,1394.1333333333334, "2020-05-07 08:08:45",1475.5333333333333,1473.3666666666666, 

What am I missing in the sed command to have a date in this format 2020-05-07 08:09:45 ?

awk 'BEGIN < FS=OFS="," >NR > 1 < n = $1/1000 cmd = "date -d @" n " +\"%Y-%m-%d %T\"" $1 = "\"" ( (cmd | getline out) >0 ? out : $1 ) "\"" close(cmd) > 1' file 
"Time","pre_aggregate[0]-IN","pre_aggregate[10]-IN", "2020-05-26 07:11:00". "2020-05-26 07:11:15". "2020-05-26 07:11:30",0,0,0,0,0,0,0,0, "2020-05-26 07:11:45",290.51666666666665,290.53333333333336, "2020-05-26 07:12:15",1027.15,1027.15,1028.85,1028.8666666666666, "2020-05-26 07:12:30",1394.1166666666666,1394.15,1394.15,1394.1333333333334, "2020-05-26 07:12:45",1475.5333333333333,1473.3666666666666, 

n=$1/1000 is done to convert milli-sec value to second value before calling date command to convert into Y-m-d H:M:S format.

With awk and strftime , replace first field by formatted time:

What am I missing in the sed command to have a date in this format 2020-05-07 08:09:45?

It's impossible to do it in sed . It is "theoretically "possible" to do arithmetic in sed , but the resulting script to handle very big values like 1590491490000 would be very, very, very long. sed can be used for simple regex replacement , sed is not able to "compute" or "convert" the values. Use other tools for such jobs.

sed doesn't understand backticks ` as a command substitution like shell does. Backticks are taken literally in sed . There is a GNU extension to execute the content of replacement string by adding a flag e on the end of s command. Because division by 1000 is equal of removing 3 last digits, in GNU sed you could do:

sed '1!s/^\(2*\)8\,/date -d@\1 +\\""%Y-%m-%d %T\\"",/e' # ^ execute the expression # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ get's executed # ^^^^^^^^^^ effectively divides by 1000 # ^^ execute for all except first line 

Using awk with strftime will be magnitudes faster.

Could you please try following in pure awk .

awk ' BEGIN < FS=OFS="," >FNR==1 < print next > < $1 = strftime("%D %T.000",substr($1,1,10)) gsub("/","-",$1) >1 ' Input_file 

2nd solution: Adding one more solution which will have " to start and end of date column.

awk ' BEGIN < FS=OFS="," >FNR==1 < print next > < $1 = strftime("\"%D %T.000\"",substr($1,1,10)) gsub("/","-",$1) >1 ' Input_file 

How to convert any date format in milliseonds in linux/centos/rhel, This can be converted in milliseconds with command: date -d "2022-06-23T08:28:23Z" +"%s%N". Any other format of date can also be converted

Источник

Оцените статью
Adblock
detector