Unix time to date linux

How to convert timestamps to dates in Bash?

I need a shell command or script that converts a Unix timestamp to a date. The input can come either from the first parameter or from stdin, allowing for the following usage patterns:

18 Answers 18

On systems with GNU Coreutils >= 5.3.0, e.g. Linux you can use:

@mehaase — the man page notes that the date string is too complex to be documented in the manpage, so it is described in info: info ‘(coreutils) date invocation’

info ‘Date input formats’ gets you straight to date ‘s formatting info node, with the pertinent Robert Grudin quote, and then a menu of format specifiers.

And it’s confusing because it’s not working in that way on most linux systems, because: freddy@hades ~ % date —help | grep — -r -r, —reference=FILE display the last modification time of FILE

while the comments on this answer are true, these are not rafa’s fault, and they don’t diminish his answer.

This version is similar to chiborg’s answer, but it eliminates the need for the external tty and cat . It uses date , but could just as easily use gawk . You can change the shebang and replace the double square brackets with single ones and this will also run in sh .

#!/bin/bash LANG=C if [[ -z "$1" ]] then if [[ -p /dev/stdin ]] # input from a pipe then read -r p else echo "No timestamp given." >&2 exit fi else p=$1 fi date -d "@$p" +%c 

@Bruno, @ghostdog74: On my system, gawk is (very roughly) 15% faster than date in a timed for loop consisting only of gawk ‘BEGIN < print strftime("%c", 1256571985); >‘ or date -d ‘@1256571985’ +%c with output redirected to /dev/null .

date is marginally (5%) faster than gawk for me (mac osx 10.9.2, date 8.23, gawk 4.1.1), but the real advantage of (g)awk is to accept a pipe of a column of many timestamps (see my answer below), which makes the script e.g. 250x as fast for 1000 timestamps.

Note that my answer meets the OP’s requirements as stated in the question, but the now accepted answer doesn’t.

You can use GNU date, for example,

$ sec=1267619929 $ date -d "UTC 1970-01-01 $sec secs" 

On macOS, you can run brew install coreutils and then use the gdate command, e.g. gdate —date @1660859222 which yields Thu Aug 18 14:47:02 PDT 2022 .

You can get formatted date from timestamp like this

I use this cross-platform one-liner:

date -d @1267619929 2>/dev/null || date -r 1267619929 

It should work both in macOS and modern versions of popular Linux distributions.

Since Bash 4.2 you can use printf ‘s %(datefmt)T format:

$ printf '%(%c)T\n' 1267619929 Wed 03 Mar 2010 01:38:49 PM CET 

That’s nice, because it’s a shell builtin. The format for datefmt is a string accepted by strftime(3) (see man 3 strftime ). Here %c is:

%c The preferred date and time representation for the current locale.

Now if you want a script that accepts an argument and, if none is provided, reads stdin, you can proceed as:

#!/bin/bash if (($#)); then printf '%(%c)T\n' "$@" else while read -r line; do printf '%(%c)T\n' "$line" done fi 

You can use this simple awk script:

$ echo '1098181096' | ./a.awk Tue 19 Oct 2004 03:18:16 AM PDT $ 

This doesn’t fit the first usage — sometimes I don’t want to echo the TS and use a parameter instead.

Читайте также:  Change default routing linux

On some old busybox version, date -s @ doesn’t work, and awk still does! An example without a stdin would be helpful too.

I use this when converting log files or monitoring them:

In OSX, or BSD, there’s an equivalent -r flag which apparently takes a unix timestamp. Here’s an example that runs date four times: once for the first date, to show what it is; one for the conversion to unix timestamp with %s , and finally, one which, with -r , converts what %s provides back to a string.

$ date; date +%s; date -r `date +%s` Tue Oct 24 16:27:42 CDT 2017 1508880462 Tue Oct 24 16:27:42 CDT 2017 

At least, seems to work on my machine.

$ uname -a Darwin XXX-XXXXXXXX 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64 

Источник

How to display Unix time in the timestamp format?

srand without a value uses the current timestamp with these Awk implementations:

The following will convert Date Time to Unix time on Unix-like environment.

# Current UNIXTIME unixtime() < datetime2unixtime "$(date -u +'%Y-%m-%d %H:%M:%S')" ># From DateTime(%Y-%m-%d %H:%M:%S)to UNIXTIME datetime2unixtime() < set -- "$" "$" set -- "$" "$" "$" "$" set -- "$1" "$" "$" "$3" "$" "$" set -- "$1" "$" "$" "$" "$" "$" [ "$2" -lt 3 ] && set -- $(( $1-1 )) $(( $2+12 )) "$3" "$4" "$5" "$6" set -- $(( (365*$1)+($1/4)-($1/100)+($1/400) )) "$2" "$3" "$4" "$5" "$6" set -- "$1" $(( (306*($2+1)/10)-428 )) "$3" "$4" "$5" "$6" set -- $(( ($1+$2+$3-719163)*86400+$4*3600+$5*60+$6 )) echo "$1" > # From UNIXTIME to DateTime format(%Y-%m-%d %H:%M:%S) unixtime2datetime() < set -- $(( $1%86400 )) $(( $1/86400+719468 )) 146097 36524 1461 set -- "$1" "$2" $(( $2-(($2+2+3*$2/$3)/$5)+($2-$2/$3)/$4-(($2+1)/$3) )) set -- "$1" "$2" $(( $3/365 )) set -- "$@" $(( $2-( (365*$3)+($3/4)-($3/100)+($3/400) ) )) set -- "$@" $(( ($4-($4+20)/50)/30 )) set -- "$@" $(( 12*$3+$5+2 )) set -- "$1" $(( $6/12 )) $(( $6%12+1 )) $(( $4-(30*$5+3*($5+4)/5-2)+1 )) set -- "$2" "$3" "$4" $(( $1/3600 )) $(( $1%3600 )) set -- "$1" "$2" "$3" "$4" $(( $5/60 )) $(( $5%60 )) printf "%04d-%02d-%02d %02d:%02d:%02d\n" "$@" ># Examples unixtime # => Current UNIXTIME date +%s # Linux command datetime2unixtime "2020-07-01 09:03:13" # => 1593594193 date -u +%s --date "2020-07-01 09:03:13" # Linux command unixtime2datetime "1593594193" # => 2020-07-01 09:03:13 date -u --date @1593594193 +"%Y-%m-%d %H:%M:%S" # Linux command 

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How can I get a formatted date for a UNIX timestamp from the command line

I have a UNIX timestamp and I’d like to get a formatted date (like the output of date ) corresponding to that timestamp. My attempts so far:

$ date +%s 1282367908 $ date -d 1282367908 date: invalid date `1282367908' $ date -d +1282367908 date: invalid date `+1282367908' $ date +%s -d +1282367908 date: invalid date `+1282367908' 
$ TZ=UTC somecommand 1282368345 Sat Aug 21 05:25:45 UTC 2010 

8 Answers 8

$ date -r 1282368345 Sat Aug 21 07:25:45 CEST 2010 $ date -r 1282368345 +%Y-%m-%d 2010-08-21 

with GNU core tools (you have to dig through the info file for that):

$ date -d @1282368345 Sat Aug 21 07:25:45 CEST 2010 $ date -d @1282368345 --rfc-3339=date 2010-08-21 

With either, add the -u (standard) option, or pass a TZ=UTC0 environment variable to have the UTC date ( TZ=UTC0 defines a timezone called UTC with offset 0 from UTC while the behaviour for TZ=UTC (with no offset) is unspecified (though on most systems would refer to a system-defined timezone also called UTC with offset 0 from UTC)).

Читайте также:  X96 mini прошивка linux

This is giving me a slightly different date: date -r 1447264553943 results in Wed Dec 28 01:39:03 BRST 47831 , when it’s actually 11/11/2015, 3:55:53 PM GMT-2:00 .

@hop ops, should have figured with the year being 47831, but that number was so high I missed it. Thanks!

After some googling, I found way to do it with the date command only:

$ date --date "Jan 1, 1970 00:00:00 +0000 + 1282367908 seconds" Sat Aug 21 09:18:28 MSD 2010 

another great example for how google makes you stupid. does nobody ever read the documentation anymore?

@hop, support for date -d @xxx was not added until coreutils 5.3 in 2005. That’s how you had to do it before that.

@StéphaneChazelas: a) this answer is from 2010 and b) why does this excuse googling before reading the documentation? If you answer from your own authority, one can excuse making errors like this, but if you have to look it up anyway, look it up right.

This perl one-liner will do it:

$ perl -e 'print scalar localtime $ARGV[0]' 1282367908 Sat Aug 21 09:18:28 2010 
$ zmodload zsh/datetime $ TZ=UTC0 strftime %c 1282368345 Sat 21 Aug 2010 05:25:45 UTC 
$ TZ=UTC0 printf '%(%c)T\n' '#1282368345' Sat Aug 21 05:25:45 2010 
$ TZ=UTC0 printf '%(%c)T\n' 1282368345 Sat 21 Aug 2010 05:25:45 UTC 

In all those, replace %c with the strftime() format you want.

Answer to an old question, but I think this might be an improvement if anyone else searches for this. This Bash function works on both Linux and OS X. I have not tested on any other BSD systems. Pass the epoch time as an argument to this function and it will print the RFC-3339 time format.

epochtorfc3339 () < RFC3339_FORMAT="+%FT%T. %z" EPOCH=$(echo $@ | sed -n "s/.*\(8\\).*/\1/p"); if date --version >/dev/null 2>/dev/null; then # Linux date $ -d "1970-01-01 UTC $ seconds" #date -d @$ --rfc-3339=seconds else # OS X/BSD date -r $ $ fi > 

Another neat example of the rich heritage of modern Unix. This is indeed possible under most BSD variants:

$ TZ=UTC date -r 1282368345 Sat Aug 21 05:25:45 UTC 2010 

(BTW your example seems to be off by one second)

Doesn’t work on Linux. hop’s answer seems to have the correct Linux answer. Thanks for pointing that out, I suspected something like that would happen, I’ve made it 45 seconds now.

For Unix-like environment, the following will work.

# Current UNIXTIME unixtime() < datetime2unixtime "$(date -u +'%Y-%m-%d %H:%M:%S')" ># From DateTime(%Y-%m-%d %H:%M:%S)to UNIXTIME datetime2unixtime() < set -- "$" "$" set -- "$" "$" "$" "$" set -- "$1" "$" "$" "$3" "$" "$" set -- "$1" "$" "$" "$" "$" "$" [ "$2" -lt 3 ] && set -- $(( $1-1 )) $(( $2+12 )) "$3" "$4" "$5" "$6" set -- $(( (365*$1)+($1/4)-($1/100)+($1/400) )) "$2" "$3" "$4" "$5" "$6" set -- "$1" $(( (306*($2+1)/10)-428 )) "$3" "$4" "$5" "$6" set -- $(( ($1+$2+$3-719163)*86400+$4*3600+$5*60+$6 )) echo "$1" > # From UNIXTIME to DateTime format(%Y-%m-%d %H:%M:%S) unixtime2datetime() < set -- $(( $1%86400 )) $(( $1/86400+719468 )) 146097 36524 1461 set -- "$1" "$2" $(( $2-(($2+2+3*$2/$3)/$5)+($2-$2/$3)/$4-(($2+1)/$3) )) set -- "$1" "$2" $(( $3/365 )) set -- "$@" $(( $2-( (365*$3)+($3/4)-($3/100)+($3/400) ) )) set -- "$@" $(( ($4-($4+20)/50)/30 )) set -- "$@" $(( 12*$3+$5+2 )) set -- "$1" $(( $6/12 )) $(( $6%12+1 )) $(( $4-(30*$5+3*($5+4)/5-2)+1 )) set -- "$2" "$3" "$4" $(( $1/3600 )) $(( $1%3600 )) set -- "$1" "$2" "$3" "$4" $(( $5/60 )) $(( $5%60 )) printf "%04d-%02d-%02d %02d:%02d:%02d\n" "$@" ># Examples unixtime # => Current UNIXTIME date +%s # Linux command datetime2unixtime "2020-07-01 09:03:13" # => 1593594193 date -u +%s --date "2020-07-01 09:03:13" # Linux command unixtime2datetime "1593594193" # => 2020-07-01 09:03:13 date -u --date @1593594193 +"%Y-%m-%d %H:%M:%S" # Linux command 

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Disk info linux console

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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.

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.

Источник

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