我正在寻找一种方法来在 Debian Linux 中以时间戳的形式显示我的时间,例如
1279628325
我找不到任何使用该date
命令执行该操作的选项。有什么想法吗?
答案1
你可以这样做
date +%s
更多可能性,请参阅
man date
答案2
我最喜欢的方式:
perl -e 'print time'
答案3
srand
没有值时使用当前时间戳和以下 Awk 实现:
- 呆呆地
- awk --posix
- mawk 1.3.3
- 瑙克
因此你可以使用 Awk:
awk 'BEGIN {srand(); print srand()}'
或者 awk丝绒图书馆:
velour -n 'print t_now()'
答案4
以下将在类 Unix 环境中将日期时间转换为 Unix 时间。
# 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+$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