在 Bash 中将日期时间字符串转换为纪元
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10990949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert date time string to epoch in Bash
提问by Edward D.
The date time string is in the following format: 06/12/2012 07:21:22. How can I convert it to UNIX timestamp or epoch?
日期时间字符串采用以下格式:06/12/2012 07:21:22。如何将其转换为 UNIX 时间戳或纪元?
回答by Daniel Kamil Kozar
What you're looking for is date --date='06/12/2012 07:21:22' +"%s"
. Keep in mind that this assumes you're using GNU coreutils, as both --date
and the %s
format string are GNU extensions. POSIX doesn't specify either of those, so there is no portable way of making such conversion even on POSIX compliant systems.
你要找的是date --date='06/12/2012 07:21:22' +"%s"
. 请记住,这假设您使用的是 GNU coreutils,因为两者--date
和%s
格式字符串都是 GNU 扩展。POSIX 没有指定其中任何一个,因此即使在 POSIX 兼容系统上也没有进行此类转换的可移植方式。
Consult the appropriate manual page for other versions of date
.
有关其他版本的date
.
Note: bash --date
and -d
option expects the date in US or ISO8601 format, i.e. mm/dd/yyyy
or yyyy-mm-dd
, not in UK, EU, or any other format.
注意:bash--date
和-d
option 需要美国或 ISO8601 格式的日期,即mm/dd/yyyy
或yyyy-mm-dd
,而不是英国、欧盟或任何其他格式。
回答by Abdul Rehman Janjua
For Linux Run this command
对于 Linux 运行此命令
date -d '06/12/2012 07:21:22' +"%s"
For mac OSX run this command
对于 mac OSX 运行此命令
date -j -u -f "%a %b %d %T %Z %Y" "Tue Sep 28 19:35:15 EDT 2010" "+%s"
回答by Mike Q
A lot of these answers overly complicated and also missing how to use variables. This is how you would do it more simply on standard Linux system (as previously mentioned the date command would have to be adjusted for Mac Users) :
很多这些答案都过于复杂,而且还缺少如何使用变量。这是在标准 Linux 系统上更简单的方法(如前所述,必须为 Mac 用户调整 date 命令):
Sample script:
示例脚本:
#!/bin/bash
orig="Apr 28 07:50:01"
epoch=$(date -d "${orig}" +"%s")
epoch_to_date=$(date -d @$epoch +%Y%m%d_%H%M%S)
echo "RESULTS:"
echo "original = $orig"
echo "epoch conv = $epoch"
echo "epoch to human readable time stamp = $epoch_to_date"
Results in :
结果是 :
RESULTS:
original = Apr 28 07:50:01
epoch conv = 1524916201
epoch to human readable time stamp = 20180428_075001
Or as a function :
或者作为一个函数:
# -- Converts from human to epoch or epoch to human, specifically "Apr 28 07:50:01" human.
# typeset now=$(date +"%s")
# typeset now_human_date=$(convert_cron_time "human" "$now")
function convert_cron_time() {
case "${1,,}" in
epoch)
# human to epoch (eg. "Apr 28 07:50:01" to 1524916201)
echo $(date -d "" +"%s")
;;
human)
# epoch to human (eg. 1524916201 to "Apr 28 07:50:01")
echo $(date -d "@" +"%b %d %H:%M:%S")
;;
esac
}
回答by shgurbanov
get_curr_date () {
# get unix time
DATE=$(date +%s)
echo "DATE_CURR : "$DATE
}
conv_utime_hread () {
# convert unix time to human readable format
DATE_HREAD=$(date -d @$DATE +%Y%m%d_%H%M%S)
echo "DATE_HREAD : "$DATE_HREAD
}
回答by F. Hauri
Efficient solution using date
as background dedicated process
date
用作后台专用进程的高效解决方案
In order to make this kind of translation a lot quicker...
为了使这种翻译更快...
Intro
介绍
In this post, you will find
在这篇文章中,你会发现
- a Quick Demo, following this,
- some Explanations,
- a Functionuseable for many Un*xtools (
bc
,rot13
,sed
...).
- 一个快速演示,在此之后,
- 一些解释,
- 可用于许多Un*x工具 ( , , ...)的函数。
bc
rot13
sed
Quick Demo
快速演示
fifo=$HOME/.fifoDate-$$
mkfifo $fifo
exec 5> >(exec stdbuf -o0 date -f - +%s >$fifo 2>&1)
echo now 1>&5
exec 6< $fifo
rm $fifo
read -t 1 -u 6 now
echo $now
This must output current UNIXTIME. From there, you could compare
这必须输出当前的UNIXTIME。从那里,你可以比较
time for i in {1..5000};do echo >&5 "now" ; read -t 1 -u6 ans;done
real 0m0.298s
user 0m0.132s
sys 0m0.096s
and:
和:
time for i in {1..5000};do ans=$(date +%s -d "now");done
real 0m6.826s
user 0m0.256s
sys 0m1.364s
From more than 6 seconds to less than a half second!!(on my host).
从超过6秒到不到半秒!!(在我的主机上)。
You could check echo $ans
, replace "now"
by "2019-25-12 20:10:00"
and so on...
你可以检查echo $ans
,更换"now"
的"2019-25-12 20:10:00"
等等...
Optionaly, you could, once requirement of date subprocessended:
或者,一旦日期子流程的要求结束,您可以:
exec 5>&- ; exec 6<&-
Original post (detailed explanation)
原帖(详细解释)
Instead of running 1 forkby date to convert, run date
just 1 time and do all convertion with same process (this could become a lot quicker)!:
不要按日期运行 1 个fork进行转换,date
只需运行1 次并使用相同的过程完成所有转换(这可能会变得更快)!:
date -f - +%s <<eof
Apr 17 2014
May 21 2012
Mar 8 00:07
Feb 11 00:09
eof
1397685600
1337551200
1520464020
1518304140
Sample:
样本:
start1=$(LANG=C ps ho lstart 1)
start2=$(LANG=C ps ho lstart $$)
dirchg=$(LANG=C date -r .)
read -p "A date: " userdate
{ read start1 ; read start2 ; read dirchg ; read userdate ;} < <(
date -f - +%s <<<"$start1"$'\n'"$start2"$'\n'"$dirchg"$'\n'"$userdate" )
Then now have a look:
那么现在来看看:
declare -p start1 start2 dirchg userdate
(may answer something like:
declare -- start1="1518549549" declare -- start2="1520183716" declare -- dirchg="1520601919" declare -- userdate="1397685600"
(可能会回答类似:
declare -- start1="1518549549" declare -- start2="1520183716" declare -- dirchg="1520601919" declare -- userdate="1397685600"
This was done in one execution!
这是一次执行完成的!
Using long runningsubprocess
使用长时间运行的子进程
We just need one fifo:
我们只需要一个fifo:
mkfifo /tmp/myDateFifo
exec 7> >(exec stdbuf -o0 /bin/date -f - +%s >/tmp/myDateFifo)
exec 8</tmp/myDateFifo
rm /tmp/myDateFifo
(Note: As process is running and all descriptors are opened, we could safely remove fifo's filesystem entry.)
(注意:当进程正在运行并且所有描述符都打开时,我们可以安全地删除 fifo 的文件系统条目。)
Then now:
那么现在:
LANG=C ps ho lstart 1 $$ >&7
read -u 8 start1
read -u 8 start2
LANG=C date -r . >&7
read -u 8 dirchg
read -p "Some date: " userdate
echo >&7 $userdate
read -u 8 userdate
We could buid a little function:
我们可以构建一个小函数:
mydate() {
local var=;
shift;
echo >&7 $@
read -u 8 $var
}
mydate start1 $(LANG=C ps ho lstart 1)
echo $start1
Or use mynewConnector
function
或者使用我的newConnector
功能
With functionsfor connectingMySQL/MariaDB
, PostgreSQL
and SQLite
...
随着功能的连接MySQL/MariaDB
,PostgreSQL
并SQLite
...
You may find them in different version on GitHub, or on my site: downloador show.
您可以在GitHub或我的网站上找到不同版本的它们:下载或显示。
wget https://raw.githubusercontent.com/F-Hauri/Connector-bash/master/shell_connector.bash
. shell_connector.bash
newConnector /bin/date '-f - +%s' @0 0
myDate "2018-1-1 12:00" test
echo $test
1514804400
Nota:On GitHub, functions and test are separated files. On my sitetest are run simply if this script is not sourced.
注意:在GitHub 上,函数和测试是分开的文件。在我的网站的测试,如果该脚本不只是运行的来源。
# Exit here if script is sourced
[ "datetime="06/12/2012 07:21:22"
" = "$BASH_SOURCE" ] || { true;return 0;}
回答by Tajni
Just be sure what timezone you want to use.
只要确定你想使用什么时区。
date -d "$datetime" +"%s" #depends on local timezone, my output = "1339456882"
Most popular use takes machine timezone.
最流行的使用需要机器时区。
date -u -d "$datetime" +"%s" #general output = "1339485682"
But in case you intentionally want to pass UTC datetime and you want proper timezone you need to add -u
flag. Otherwise you convert it from your local timezone.
但是,如果您故意想要传递 UTC 日期时间并且想要正确的时区,则需要添加-u
标志。否则,您将其从本地时区转换。