在Bash中将日期时间字符串转换为历元

时间:2021-01-18 16:02:51

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时间戳或纪元?

5 个解决方案

#1


60  

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.

你要找的是日期——日期='06/12/2012 07:21:22' + ' %s '。请记住,这假定您正在使用GNU coreutils,因为date和%s格式字符串都是GNU扩展。POSIX没有指定这两种方法中的任何一种,因此即使在与POSIX兼容的系统上,也没有可移植的方法来进行这种转换。

Consult the appropriate manual page for other versions of 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——日期和-d选项预期日期为US或ISO8601格式,即mm/dd/ yyyyyy或yyyyyy -mm-dd,而不是在英国、欧盟或任何其他格式。

#2


21  

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"

#3


1  

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}

#4


0  

Convert date time string to UNIX timestamp in bash command

As Daniel Kamil Kozar correctly answered, date is theright tool.

正如Daniel Kamil Kozar正确的回答,日期是最合适的工具。

I just want to add a particular way of using them, mostly if you havemany date to convert:

我只是想添加一种特殊的使用方法,如果你有任何日期可以转换:

Instead of running 1 fork by date to convert, run date just 1 time and do all convertion with same process (this could become a lot quicker)!:

不要按日期运行1个fork来转换,运行日期仅为1次,并使用相同的进程进行所有的转换(这可能会变得更快)!

date -f - +%s <<eofApr 17  2014May 21  2012Mar  8 00:07Feb 11 00:09eof1397685600133755120015204640201518304140

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:

(5月回答类似:

declare -- start1="1518549549"declare -- start2="1520183716"declare -- dirchg="1520601919"declare -- userdate="1397685600"

Using long running subprocess

We just need one fifo:

我们只需要一个先进先出:

mkfifo /tmp/myDateFifoexec 7> >(exec stdbuf -o0 /bin/date -f - +%s >/tmp/myDateFifo)exec 8</tmp/myDateFiform /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 $$ >&7read -u 8 start1read -u 8 start2LANG=C date -r . >&7read -u 8 dirchgread -p "Some date: " userdateecho >&7 $userdateread -u 8 userdate

We could buid a little function:

我们可以建立一个函数:

mydate() {    local var=$1;    shift;    echo >&7 $@    read -u 8 $var}mydate start1 $(LANG=C ps ho lstart 1)echo $start1

Or use my newConnector function

You may find them in different version on GitHub, or on my site.

你可以在GitHub或我的网站上找到不同的版本。

wget https://raw.githubusercontent.com/F-Hauri/Connector-bash/master/shell_connector.bash. shell_connector.bash newConnector /bin/date '-f - +%s' @0 0myDate "2018-1-1 12:00" testecho $test1514804400

#5


0  

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用户进行调整):

Sample script:

示例脚本:

#!/bin/bashorig="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:01epoch 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 "${2}" +"%s")            ;;        human)            # epoch to human (eg. 1524916201 to "Apr 28 07:50:01")            echo $(date -d "@${2}" +"%b %d %H:%M:%S")            ;;    esac}

#1


60  

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.

你要找的是日期——日期='06/12/2012 07:21:22' + ' %s '。请记住,这假定您正在使用GNU coreutils,因为date和%s格式字符串都是GNU扩展。POSIX没有指定这两种方法中的任何一种,因此即使在与POSIX兼容的系统上,也没有可移植的方法来进行这种转换。

Consult the appropriate manual page for other versions of 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——日期和-d选项预期日期为US或ISO8601格式,即mm/dd/ yyyyyy或yyyyyy -mm-dd,而不是在英国、欧盟或任何其他格式。

#2


21  

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"

#3


1  

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}

#4


0  

Convert date time string to UNIX timestamp in bash command

As Daniel Kamil Kozar correctly answered, date is theright tool.

正如Daniel Kamil Kozar正确的回答,日期是最合适的工具。

I just want to add a particular way of using them, mostly if you havemany date to convert:

我只是想添加一种特殊的使用方法,如果你有任何日期可以转换:

Instead of running 1 fork by date to convert, run date just 1 time and do all convertion with same process (this could become a lot quicker)!:

不要按日期运行1个fork来转换,运行日期仅为1次,并使用相同的进程进行所有的转换(这可能会变得更快)!

date -f - +%s <<eofApr 17  2014May 21  2012Mar  8 00:07Feb 11 00:09eof1397685600133755120015204640201518304140

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:

(5月回答类似:

declare -- start1="1518549549"declare -- start2="1520183716"declare -- dirchg="1520601919"declare -- userdate="1397685600"

Using long running subprocess

We just need one fifo:

我们只需要一个先进先出:

mkfifo /tmp/myDateFifoexec 7> >(exec stdbuf -o0 /bin/date -f - +%s >/tmp/myDateFifo)exec 8</tmp/myDateFiform /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 $$ >&7read -u 8 start1read -u 8 start2LANG=C date -r . >&7read -u 8 dirchgread -p "Some date: " userdateecho >&7 $userdateread -u 8 userdate

We could buid a little function:

我们可以建立一个函数:

mydate() {    local var=$1;    shift;    echo >&7 $@    read -u 8 $var}mydate start1 $(LANG=C ps ho lstart 1)echo $start1

Or use my newConnector function

You may find them in different version on GitHub, or on my site.

你可以在GitHub或我的网站上找到不同的版本。

wget https://raw.githubusercontent.com/F-Hauri/Connector-bash/master/shell_connector.bash. shell_connector.bash newConnector /bin/date '-f - +%s' @0 0myDate "2018-1-1 12:00" testecho $test1514804400

#5


0  

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用户进行调整):

Sample script:

示例脚本:

#!/bin/bashorig="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:01epoch 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 "${2}" +"%s")            ;;        human)            # epoch to human (eg. 1524916201 to "Apr 28 07:50:01")            echo $(date -d "@${2}" +"%b %d %H:%M:%S")            ;;    esac}