41

I'm looking for a way to display my time in Debian Linux as a timestamp, e.g.

1279628325

I can't find any options to do that with the date command. Any ideas?

Shekhar
  • 5,139
user32433
  • 467

4 Answers4

75

You can do this with

date +%s

For more possibilities, see

man date
3

My favorite way:

perl -e 'print time'
Zombo
  • 1
1

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

  • gawk
  • gawk --posix
  • mawk 1.3.3
  • nawk

so you can use Awk:

awk 'BEGIN {srand(); print srand()}'

Or the awk Velour library:

velour -n 'print t_now()'
Zombo
  • 1
1

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 -- "${1%% }" "${1## }" set -- "${1%%-}" "${1#-}" "${2%%:}" "${2#:}" set -- "$1" "${2%%-}" "${2#-}" "$3" "${4%%:}" "${4#:}" set -- "$1" "${2#0}" "${3#0}" "${4#0}" "${5#0}" "${6#0}" [ "$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+$43600+$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

https://tech.io/snippet/a3dWEQY

shinokada
  • 2,715