web-dev-qa-db-ja.com

指定した時間にn時間を追加する方法は?

午前6時45分などの時間を設け、さらに時間を追加して1.45時間にしたいと思います。別の時間を取得するために、午前6時45分に1.45時間を追加したいと思います。

そのためのコマンドラインユーティリティはありますか?私はいくつかのグーグルを行ったが、dateのmanページを読んで、そのようなものを見つけていない。 wcalcは時間計算を処理していないようです。

編集:2015年3月6日。これは10進数の時間を使用することになったスクリプトです。エラーチェックを使用して、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"
13
Bulrush

コマンドライン:

$ now=$(date -d "06:45 today + 105 minutes" +'%H:%M')
$ echo "$now"
08:30

$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

小数は許可されません...


コメントから:小数点以下1.45時間の回答を得るには:

$ now=$(date -d "06:45 today + $((145 * 60 / 100)) minutes" +'%H:%M')
$ echo "$now:
8:12
23
Rinzwind