在 UNIX 时间戳 Shell/Bash 中使用时区转换日期 [英] Converting date with timezone in UNIX timestamp Shell/Bash
问题描述
我需要将格式为yyyy/mm/dd hh:mm:ss TZ"的字符串日期转换为 UNIX 时间(TZ = 时区).
I need to convert a date from string in the format "yyyy/mm/dd hh:mm:ss TZ" to UNIX time (TZ = Timezone).
到目前为止我所做的是使用
What I have done so far is to convert a date in the format "yyyy/mm/dd hh:mm:ss" without a timezone to timestamp by using
dateYMD="2019/2/28 12:23:11.46"
newt=$(date -d "${dateYMD}" +"%s")
echo ${newt}
我有以下结果.
1551349391
我的努力是找到如何将时区和日期/时间转换为时间戳(unix 时间).例如,我需要 4 个与 dateYMD 具有相同日期/时间但位于 4 个不同时区的变量,以便它们的时间戳不同.
My struggle is to find how both timezone and date/time can be converted to timestamp (unix time) . For example I need 4 variables with the same date/time as dateYMD but in 4 different timezones so that their timestamps would be different.
这是我尝试过的最新版本
Here is the latest I have tried
dateYMD="2017/09/09 08:58:09"
timez=$(TZ=Australia/Sydney date -d @$(date +%s -d "${dateYMD}"))
unixTimez=$( date --date "${timez}" +"%s" )
echo ${unixTimez}
这向我展示了以下错误
date: invalid date ‘чт фев 28 21:23:11 AEDT 2019’
推荐答案
您不需要两次调用 date
.只需将 TZ
设置为该变量所需的时区即可调用一次.
You don't need to call date
twice. Just call it once with TZ
set to the timezone you want for that variable.
timesydney=$(TZ=Australia/Sydney date -d "$dateYMD" +%s)
timenyc=$(TZ=US/Eastern date -d "$dateYMD" +%s)
这篇关于在 UNIX 时间戳 Shell/Bash 中使用时区转换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!