Bash:根据输入(如“YYYYMMDDHH”)计算时间差(以小时为单位)
我有两个形式的日期:yyyymmddhh
,并想计算这两个日期之间的差异(小时)。例如,
start_date=1996010100
end_date=1996010122
该日期为两个日期:1996-01-01 00:00:00和1996-01-01 22:00:00。我想使用日期
来计算小时的差异,结果应为22小时。我尝试了
START=$(date -d "$start_date" +"%s")
END=$(date -d "$end_date" +"%s")
HOURS=$(bc -l <<< "($END - $START) / 3600")
,但失败了... 那我该怎么做呢?谢谢!
I have two dates in forms like: YYYYMMDDHH
and want to calculate the differences (in hours) between these two dates. For example
start_date=1996010100
end_date=1996010122
which stands for two dates: 1996-01-01 00:00:00 and 1996-01-01 22:00:00. I want to use date
to calculate the difference in hours, the result shall be 22 hours. I tried with
START=$(date -d "$start_date" +"%s")
END=$(date -d "$end_date" +"%s")
HOURS=$(bc -l <<< "($END - $START) / 3600")
but it failed...
So how can I do this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
出于绩效原因,我们要限制需要调用的子过程呼叫的数量:
bash
子字符串功能将输入转换为可用的日期/时间字符串bash
数学替换bc
调用bash
substring功能将输入分解为可用的日期/时间格式,例如:应用于
$ {end_date}
并使用<代码> bash 数学:这为我们提供了2个子过程调用(
$(date ...)
)。虽然其他语言/工具(awk
,perl
等)可能会加快速度,如果您需要将结果存储在bash 变量,然后您正在考虑至少需要1个子过程调用(即,
hours = $(awk/perl/??? ...)
)。如果性能真的很重要(例如,需要执行此1000次的时间),请看一下 So Ansher 使用FIFO,背景
date
过程和IO重定向...是的,编码更多,更令人费解,但对于大量操作而言也更快。For performance reasons we want to limit the number of sub-process calls we need to invoke:
bash
substring functionality to convert inputs into usable date/time stringsbash
math to replacebc
callbash
substring functionality to break the inputs into a usable date/time format, eg:Applying to
${end_date}
and usingbash
math:This leaves us with 2 sub-process calls (
$(date ...)
). While other languages/tools (awk
,perl
, etc) can likely speed this up a bit, if you need to store the result in abash
variable then you're looking at needing at least 1 sub-process call (ie,hours=$(awk/perl/??? ...)
).If performance is really important (eg, needing to perform this 1000's of times) take a look at this SO answer that uses a fifo, background
date
process and io redirection ... yeah, a bit more coding and a bit more convoluted but also a bit faster for large volumes of operations.busybox date
可以解决这个问题busybox date
can do the trick因此,如果它代表它,请将其转换为该形式。
和结束一样。
So convert it to that form if it stands for it.
and the same with end.
最简单的方法是使用此命令安装“dateutils”
运行这些命令以获取秒数差异:
输出:
下一步:只需除以 86400 即可获取天数或类似的小时和分钟:)
the most simple way is to install "dateutils" using this command
Run these commands to get the difference in seconds:
output:
next step: Simply divide by 86400 to get the number of days or similarly for hours and minutes :)
如果您可以使用像 Python 这样功能更齐全的脚本语言,它将提供更愉快和更容易理解的日期解析体验,并且可能是默认安装的(
datetime
也是一个标准Python 库)使用 shell 变量进行结构化
结构化以从
stdin
读取日期和格式代码应与此处提供的 Python 3 和 Python 2.7 格式代码一起使用
(1989 C 标准)
https://docs.python.org/ 3/library/datetime.html#strftime-and-strptime-format-codes
If it's possible for you to use a more fully-featured scripting language like Python, it'll provide a much more pleasant and understandable date parsing experience, and is probably installed by default (
datetime
is also a standard Python library)Structured with shell vars
Structured to read dates and format code from
stdin
Should work with both Python 3 and Python 2.7
format codes available here (1989 C standard)
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes