简体   繁体   English

增加用户时间变量Bash

[英]Incrementing user time variable Bash

In the script I'm working on the user defines a variable labeled start time. 在我正在使用的脚本中,用户定义了一个标记为开始时间的变量。 I would like to increment and decrement that variable by ±3 hrs. 我想将该变量增加和减少±3小时。 The format is 00:00:00 (24hr) and I'm uncertain if there is a better way that doesn't involve string manipulation because if it is less than zero I can't pass -03:00:00 to the Python script that utilizes this variable. 格式为00:00:00(24hr),我不确定是否有更好的方法不涉及字符串操作,因为如果它小于零,则无法将-03:00:00传递给Python利用此变量的脚本。

#somescript.sh 00:01:35    
STARTTIME=$1
E=${STARTTIME%*:}
S=${STARTTIME##*:} && ((S=S+3)
echo "$S : $E"

Desired Result: 03:01:35 and 21:01:35 预期的结果:03:01:35和21:01:35

Reviewed: String Operators and Incrementing time (minutes and seconds) in bash / shell script prior to asking this question. 审阅:在问这个问题之前,bash / shell脚本中的 字符串运算符递增时间(分钟和秒) Any assistance would be appreciated. 任何援助将不胜感激。

starttime=$1
endtime=$(date -u -d "$starttime 3 hours" +'%H:%M:%S')
echo "${starttime} - $endtime"

...used as: ...用作:

$ ./add-time 01:01:35
01:01:35 - 04:01:35

If you need to go in the opposite direction, add the word "ago": 如果您需要朝相反的方向前进,请添加单词“ ago”:

endtime=$(date -u -d "$starttime 3 hours ago" +'%H:%M:%S')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM