shell 将日期时间字符串转换为Bash中的纪元

kx1ctssn  于 2023-01-31  发布在  Shell
关注(0)|答案(6)|浏览(125)

日期时间字符串的格式如下:06/12/2012 07:21:22.如何将其转换为UNIX时间戳或纪元?

wztqucjr

wztqucjr1#

你要找的是date --date='06/12/2012 07:21:22' +"%s"。记住,这里假设你使用的是GNU coreutils,因为--date%s字符串都是GNU扩展。POSIX没有指定这两种格式,所以即使在POSIX兼容的系统上也没有可移植的方法来进行这样的转换。
有关date的其他版本,请参阅相应的手册页。
注意:bash --date-d选项需要US或ISO8601格式的日期,即mm/dd/yyyyyyyy-mm-dd,而不是UK、EU或任何其他格式。

lyr7nygr

lyr7nygr2#

对于Linux,运行以下命令:

date -d '06/12/2012 07:21:22' +"%s"

对于macOS,请运行以下命令:

date -jf "%Y-%m-%d %H:%M:%S" "1970-01-01 00:00:00" +%s
b0zn9rqh

b0zn9rqh3#

很多这样的答案过于复杂,也缺少如何使用变量。这是你在标准Linux系统上更简单的方法(如前所述,对于Mac用户,日期命令必须进行调整):
示例脚本:

#!/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:
original = Apr 28 07:50:01
epoch conv = 1524916201 
epoch to human readable time stamp = 20180428_075001

或作为函数:

# -- 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
}
h5qlskok

h5qlskok4#

只要确定您要使用的时区。

datetime="06/12/2012 07:21:22"

最流行的用法是使用机器时区。

date -d "$datetime" +"%s" #depends on local timezone, my output = "1339456882"

但是如果你想传递UTC日期时间,并且你想要正确的时区,你需要添加-u标志,否则你需要从你的本地时区转换它。

date -u -d "$datetime" +"%s" #general output = "1339485682"
vawmfj5a

vawmfj5a5#

在bash中将 * date time string * 转换为 * epoch * 的有效方法

避免 * 无用的重复 * 分叉,为了使这个翻译快得多...
我们可以运行date -f - +%s作为后台进程,而不是为每个翻译运行一个fork ...

简介

常用语法:

epochDate=$(date -d "$InputDate" +%s)

工作很好,但如果重复运行会变重!
在这篇文章中,你会发现

  • 一个 * 快速演示 *,然后,
  • 一些 * 解释 *,
  • a * 功能 * 可用于许多 * Un * x * 工具(bcrot13sed ...)。

快速演示

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

它必须输出当前的 * 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

以及:

time for i in {1..5000};do ans=$(date +%s -d "now");done 
real    0m6.826s
user    0m0.256s
sys     0m1.364s
    • 从超过6秒到不到半秒!!**(在我的主机上)。

您可以选中echo $ans,将"now"替换为"2019-25-12 20:10:00"等等...
可选地,在 * date subprocess * 的要求结束后,您可以:

exec 5>&- ; exec 6<&-

原帖(详细说明)

与其运行1 * fork * by date来转换,不如只运行date一次,然后使用相同的过程进行所有转换(这样可以更快)!:

date -f - +%s <<eof
Apr 17  2014
May 21  2012
Mar  8 00:07
Feb 11 00:09
eof
1397685600
1337551200
1520464020
1518304140

样品:

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" )

那现在看看:

declare -p start1 start2 dirchg userdate

(may回答类似这样的问题:

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

这是一次处决!

使用 * 长时间运行 * 子进程

我们只需要一个 * fifo *:

mkfifo /tmp/myDateFifo
exec 7> >(exec stdbuf -o0 /bin/date -f - +%s >/tmp/myDateFifo)
exec 8</tmp/myDateFifo
rm /tmp/myDateFifo

(Note:当进程运行并且所有描述符都打开时,我们可以安全地删除fifo的文件系统条目。
那么现在:

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

我们可以建立一个小函数:

mydate() {
    local var=$1;
    shift;
    echo >&7 $@
    read -u 8 $var
}

mydate start1 $(LANG=C ps ho lstart 1)
echo $start1

或使用 * my * newConnector函数

具有 * 连接**MySQL/MariaDBPostgreSQL * 和 * SQLite * 的 * 功能 *...
您可以在GitHub或我的网站上找到它们的不同版本:downloadshow中的任意一个。

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
    • Note:**在 * GitHub * 上,函数和测试是分开的文件。在 * my site * 上,如果这个脚本不是 * sourced *,测试就会运行。
# Exit here if script is sourced
[ "$0" = "$BASH_SOURCE" ] || { true;return 0;}
nwlqm0z1

nwlqm0z16#

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
}

相关问题