我想有一个时间,比如早上 6 点 45 分,并添加一个小时数,比如 1.45 小时,以产生另一个时间。所以我想在早上 6 点 45 分增加 1.45 小时以获得另一个时间。
是否有命令行实用程序?我已经做了一些谷歌搜索,并阅读了手册页date,但没有找到类似的东西。wcalc似乎不处理时间计算。
编辑:2015 年 3 月 6 日。这是我最终使用十进制小时的脚本。它可以使用一些错误检查来确保 HH:MM 使用 2 位数字表示小时。
#!/bin/bash
# Mar 6, 2015
# Add decimal hours to given time.
# Syntax: timeadd HH:MM HOURS
# There MUST be 2 digits for the hours in HH:MM.
# Times must be in military time.
# Ex: timeadd 05:51 4.51
# Ex: timeadd 14:12 2.05
echo " "
# If we have less than 2 parameters, show instructions and exit.
if [ $# -lt 2 ]
then
echo "Usage: timeadd HH:MM DECHOURS"
exit 1
fi
intime=$1
inhours=$2
# Below is arithmetic expansion $(())
# The bc calculator is standard on Ubuntu.
# Below rounds to the minute.
inminutes=$(echo "scale=0; ((($inhours * 60)*10)+5)/10" | bc)
echo "inminutes=$inminutes"
now=$(date -d "$intime today + $inminutes minutes" +'%H:%M')
echo "New time is $now"
Run Code Online (Sandbox Code Playgroud)
Rin*_*ind 33
命令行:
$ now=$(date -d "06:45 today + 105 minutes" +'%H:%M')
$ echo "$now"
08:30
Run Code Online (Sandbox Code Playgroud)
$now 将保留您指定的时间。
你可以在“和”之间放很多东西;像当前时间并添加 105。
$now=$(date -d "06:45 today + 2.5 hour" +'%H:%M')
date: invalid date `06:45 today + 2.5 hour'
$now=$(date -d "06:45 today + 2:30 hour" +'%H:%M')
date: invalid date `06:45 today + 2:30 hour'
$ now=$(date -d "06:45 today + 2 hour" +'%H:%M')
$ echo "$now"
08:45
Run Code Online (Sandbox Code Playgroud)
不允许使用小数...
来自评论:要获得 1.45 十进制小时的答案:
$ now=$(date -d "06:45 today + $((145 * 60 / 100)) minutes" +'%H:%M')
$ echo "$now:
8:12
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39427 次 |
| 最近记录: |