bash shell 脚本中的日期命令
在工作中,所有的配置文件都是新鲜生成的,并附加一个 会话号。该公司于2月16日
上市,86400
为秒 一天之内。 会话号是通过减去公司开始时间生成的 从 Seconds_Since_last_day 开始计算天数并添加几个零
这是与天数配置文件交互的关键。我明白了 - 但我不明白 了解
date -ud“$distance days ago 00:00:00”
。
是自 1970 年以来的秒数吗?
if $session; then
# return the session of the last day
seconds_since_day_one=`date -ud "Feb 16 2002" +"%s"`
seconds_since_last_day=`date -ud "$distance days ago 00:00:00" +"%s"`
days_between=`printf "%010d" $(( (seconds_since_last_day - seconds_since_day_one) / 86400 ))`
# Truncate on the left to 9 bytes to leave room
# to append the engine suffix for your environment
echo $days_between | awk '{l=length($1); print substr( $1, (l-8), l )}'
At work all the days config files are generated fresh and appended with a
session number. The company went public on Feb 16
, and the 86400
is seconds
in one day. The session number is generated by subtracting the company start
day from seconds_since_last_day and adding a few zero's
That is the key to interacting with the days config files. I get this - However I do not
understand the
date -ud "$distance days ago 00:00:00"
.
Is it the number of seconds since 1970?
if $session; then
# return the session of the last day
seconds_since_day_one=`date -ud "Feb 16 2002" +"%s"`
seconds_since_last_day=`date -ud "$distance days ago 00:00:00" +"%s"`
days_between=`printf "%010d" $(( (seconds_since_last_day - seconds_since_day_one) / 86400 ))`
# Truncate on the left to 9 bytes to leave room
# to append the engine suffix for your environment
echo $days_between | awk '{l=length($1); print substr( $1, (l-8), l )}'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
date -ud "$distance days ago 00:00:00"
本身只是以一种非常可读的格式打印一定天前的日期,但是当您添加 FORMAT 字符串来控制输出时+"%s"
确实意味着所谓的 Unix 时间(自 1970-01-01 00:00:00 UTC 以来的秒数)中的数字。如果变量
$distance
设置为数字,则显示几天前的日期,如果设置为0则表示今天,1表示昨天,2表示前天,依此类推。为了更好地理解这些格式和相关关键字,GNU coreutils 包(以及其他地方)中有相当好的文档。检查这些网址:
http://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html#Relative-items-in-date-strings< br>
http://www.gnu.org/software/coreutils /manual/html_node/Date-input-formats.html
http://www.gnu.org/software /coreutils/manual/html_node/date-in Vocation.html#date-inspiration
Unix 时间的维基百科解释:
http://en.wikipedia.org/wiki/Unix_time
date -ud "$distance days ago 00:00:00"
in itself just prints the date a certain amount of days ago in a quite readable format, but when you add the FORMAT string to control the output+"%s"
does indeed mean the number in so called Unix Time (number of seconds since 1970-01-01 00:00:00 UTC).If the variable
$distance
is set to a number it shows the date that number of days ago, if its set to 0 it means today, 1 it means yesterday, 2 the day before yesterday and so on. To better understand these formats and relative keywords there are rather good documentations in (amongst other places) the GNU coreutils package.Check these URLs:
http://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html#Relative-items-in-date-strings
http://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html
http://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html#date-invocation
Wikipedia explanation of Unix Time:
http://en.wikipedia.org/wiki/Unix_time
date
的选项 -d 提供用于获取日期的通用字符串。例如,
date -d Saturday
将打印昨天的日期,date -d 'yesterday 12:00 AM'
将打印昨天的日期,时间设置为 12:上午 00 点。因此,
date -d 6 days ago 00:00:00
将打印 6 天前的日期,时间设置为 00:00:00。我希望它能回答你的问题。格式 +"%s" 告诉
date
打印从 1970 年开始的秒数,而不是日期。The option -d to
date
provides a generic string to obtain the date.So, for example,
date -d yesterday
will print yesterday's date, anddate -d 'yesterday 12:00 AM'
will print yesterday's date with the time set to 12:00 AM.So,
date -d 6 days ago 00:00:00
will print the date from 6 days ago, with the time set to 00:00:00. I hope it answers your question.The format +"%s" tells
date
to print the number of seconds from 1970, instead the date.awk 中的 mktime 和 strftime 可以用来获取日期时间。
http://www.gnu.org/software/gawk/manual /html_node/Time-Functions.html
例如,strftime("%A",mktime("YYYY MM DD 00 00 00"))
应该给你这一天。
mktime and strftime in awk can be used to get the date of the time.
http://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html
For instance, strftime("%A",mktime("YYYY MM DD 00 00 00"))
should give you the day.