将今天的 HH:MM am/pm 转换为 Bash 中的纪元

Convert today's HH:MM am/pm to epoch in Bash

我有两个变量 $sunrise="7:23 am" 和 $sunset="6:10 pm"。我需要将它们都转换为今天的等效时间,如:

secSunrise=($date ... "$sunrise" ... +"%s")
secSunset=$(date ... "$sunset" ... +"%s")

到目前为止我唯一想到的就是用当前日期时间做这件事:

$ secNow=$(date +"%s")
$ echo $secNow
1488331535

如何对这个圆进行平方并将 HH:MM am/pm 12 小时格式的变量插入 date 命令?

您可以这样使用 GNU date

date --date="7:23 am today" +%s
# output => 1488352980

date --date="6:10 pm today" +%s
# output => 1488391800
  • %s 打印纪元

来自man date

The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully described in the info documentation.