Bash: subtracting 10 mins from a given time - bash

In a bash script, if I have a number that represents a time, in the form hhmmss (or hmmss), what is the best way of subtracting 10 minutes?
ie, 90000 -> 85000

This is a bit tricky. Date can do general manipulations, i.e. you can do:
date --date '-10 min'
Specifying hour-min-seconds (using UTC because otherwise it seems to assume PM):
date --date '11:45:30 UTC -10 min'
To split your date string, the only way I can think of is substring expansion:
a=114530
date --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"
And if you want to just get back hhmmss:
date +%H%M%S --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"

why not just use epoch time and then take 600 off of it?
$ echo "`date +%s` - 600"| bc; date
1284050588
Thu Sep 9 11:53:08 CDT 2010
$ date -d '1970-01-01 UTC 1284050588 seconds' +"%Y-%m-%d %T %z"
2010-09-09 11:43:08 -0500

Since you have a 5 or 6 digit number, you have to pad it before doing string manipulation:
$ t=90100
$ while [ ${#t} -lt 6 ]; do t=0$t; done
$ echo $t
090100
$ date +%H%M%S --utc -d"today ${t:0:2}:${t:2:2}:${t:4:2} UTC - 10 minutes"
085100
Note both --utc and UTC are required to make sure the system's timezone doesn't affect the results.
For math within bash (i.e. $(( and ((), leading zeros will cause the number to be interpreted as octal. However, your data is more string-like (with a special format) than number-like, anyway. I've used a while loop above because it sounds like you're treating it as a number and thus might get 100 for 12:01 am.

My version of bash doesn't support -d or --date as used above. However, assuming a correctly 0-padded input, this does work
$ input_time=130503 # meaning "1:05:03 PM"
# next line calculates epoch seconds for today's date at stated time
$ epoch_seconds=$(date -jf '%H%M%S' $input_time '+%s')
# the 600 matches the OP's "subtract 10 minutes" spec. Note: Still relative to "today"
$ calculated_seconds=$(( epoch_seconds - 600 )) # bc would work here but $((...)) is builtin
# +%H%M%S formats the result same as input, but you can do what you like here
$ echo $(date -r $calculated_seconds '+%H%M%S')
# output is 125503: Note that the hour rolled back as expected.

For MacOS users you can do the following:
$(date -v -10M +"%H:%M:%S")
Date time without a specific format:
$(date -v -10M)
For non-macOS users:
Date time without a specific format:
date --date '-10 min'

Related

bash: substract a day from a date stored in a variable [duplicate]

I have the following in a shell script. How can I subtract one hour while retaining the formatting?
DATE=`date "+%m/%d/%Y -%H:%M:%S"`
The following command works on recent versions of GNU date:
date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
date -v-60M "+%m/%d/%Y -%H:%M:%S"
DATE=`date -v-60M "+%m/%d/%Y -%H:%M:%S"`
If you have bash version 4.4+ you can use bash's internal date printing and arithmetics:
printf "current date: %(%m/%d/%Y -%H:%M:%S)T\n"
printf "date - 60min: %(%m/%d/%Y -%H:%M:%S)T\n" $(( $(printf "%(%s)T") - 60 * 60 ))
The $(printf "%(%s)T") prints the epoch seconds, the $(( epoch - 60*60 )) is bash-aritmetics - subtracting 1hour in seconds. Prints:
current date: 04/20/2017 -18:14:31
date - 60min: 04/20/2017 -17:14:31
if you need substract with timestamp :
timestamp=$(date +%s -d '1 hour ago');
This work on my Ubuntu 16.04 date:
date --date="#$(($(date +%s) - 3600))" "+%m/%d/%Y -%H:%M:%S"
And the date version is date (GNU coreutils) 8.25
$ date +%Y-%m-%d-%H
2019-04-09-20
$ date -v-1H +%Y-%m-%d-%H
2019-04-09-19
But in shell use as like date +%Y-%m-%d-%H, date -v-1H +%Y-%m-%d-%H
Convert to timestamp (a long integer), subtract the right number of milliseconds, reformat to the format you need.
Hard to give more details since you don't specify a programming language...
If you need change timezone before subtraction with new format too:
$(TZ=US/Eastern date -d '1 hour ago' '+%Y-%m-%d %H:%M')
Here another way to subtract 1 hour.
yesterdayDate=`date -d '2018-11-24 00:09 -1 hour' +'%Y-%m-%d %H:%M'`
echo $yesterdayDate
Output:
2018-11-23 23:09
I hope that it can help someone.
DATE=date -1H "+%m/%d/%Y -%H:%M:%S"

How to subtract two different date formats to get days in bash?

I am working on bash. I have to subtract current date from a given date to get number of days as a difference. The given date is in format m/d/yyyy so instead of 09/26/2015 it is 9/26/2015. So even if I try to convert both dates into same format and subtract it says invalid date format.
date1=$(date +"%F")
date2=$(date -d 11/2/2015 +"%F")
diff=$(date "--date=${date2} -${date1}" +%F)
echo $diff days remaining
This is what I had tried with some variations, but doesn't work. What am I doing wrong? Thanks in advance.
Try this:
let diff=(`date +%s -d 11/2/2015` - `date +%s`)/86400
echo $diff days remaining
there are two problems: converting the user-provided date into a normalized form and calculcating the difference in days.
normalizing date
how date interprets a date-string depends on the current locale.
Try to find a locale that uses your special formatting (%m/%d/%Y):
$ LC_TIME=en_US.UTF-8 date -d 1/2/2015
Fri Jan 2 00:00:00 CET 2015
calculating the difference
bash only can only do integer arithmetic, so convert your date first to some integer representation, do the subtraction and convert the representation to days (if needed).
$ LC_TIME=en_US.UTF-8 \
echo $(( ( $(date -d 11/2/2015 +%s) - $(date +%s)) / (3600*24) ))
32
This uses $(...) instead of ... to function substitution.
It also uses $(( ... )) for evalution of math expression instead of the bashism let x=(), so you can use it in POSIX-conformant shell-scripts (e.g. interpreted by /bin/dash)

Reading date with hour and incrementing it in shell script

I have a file with date '2015-06-01-12', how can I get it to increment the hour in shell script? The result I want is '2015-06-01-13'. If its the 23rd hour it has to move forward a date and get 00 as hour.
I was able to do it to date but have so far had not any luck with incrementing hours.
currDate=2015-06-02
nextDate=`date '+%Y-%m-%d' -d "$currDate+1 days"`
echo $nextDate
It is reasonably easy if you keep your date as an epoch (number of seconds since January 1, 1970):
$ currDate=$( date +%s -d "2015-06-02 23:00:00" )
$ echo $currDate
1433300400
$ date +%Y-%m-%d-%H -d #$currDate
2015-06-02-23
$ nextDate=$(( $currDate + 3600 )) #adding an hour's worth of seconds
$ date +%Y-%m-%d-%H -d #$nextDate
2015-06-03-00

Subtract 1 hour from date in UNIX shell script

I have the following in a shell script. How can I subtract one hour while retaining the formatting?
DATE=`date "+%m/%d/%Y -%H:%M:%S"`
The following command works on recent versions of GNU date:
date -d '1 hour ago' "+%m/%d/%Y -%H:%M:%S"
date -v-60M "+%m/%d/%Y -%H:%M:%S"
DATE=`date -v-60M "+%m/%d/%Y -%H:%M:%S"`
If you have bash version 4.4+ you can use bash's internal date printing and arithmetics:
printf "current date: %(%m/%d/%Y -%H:%M:%S)T\n"
printf "date - 60min: %(%m/%d/%Y -%H:%M:%S)T\n" $(( $(printf "%(%s)T") - 60 * 60 ))
The $(printf "%(%s)T") prints the epoch seconds, the $(( epoch - 60*60 )) is bash-aritmetics - subtracting 1hour in seconds. Prints:
current date: 04/20/2017 -18:14:31
date - 60min: 04/20/2017 -17:14:31
if you need substract with timestamp :
timestamp=$(date +%s -d '1 hour ago');
This work on my Ubuntu 16.04 date:
date --date="#$(($(date +%s) - 3600))" "+%m/%d/%Y -%H:%M:%S"
And the date version is date (GNU coreutils) 8.25
$ date +%Y-%m-%d-%H
2019-04-09-20
$ date -v-1H +%Y-%m-%d-%H
2019-04-09-19
But in shell use as like date +%Y-%m-%d-%H, date -v-1H +%Y-%m-%d-%H
Convert to timestamp (a long integer), subtract the right number of milliseconds, reformat to the format you need.
Hard to give more details since you don't specify a programming language...
If you need change timezone before subtraction with new format too:
$(TZ=US/Eastern date -d '1 hour ago' '+%Y-%m-%d %H:%M')
Here another way to subtract 1 hour.
yesterdayDate=`date -d '2018-11-24 00:09 -1 hour' +'%Y-%m-%d %H:%M'`
echo $yesterdayDate
Output:
2018-11-23 23:09
I hope that it can help someone.
DATE=date -1H "+%m/%d/%Y -%H:%M:%S"

Get hex time stamp from bash script

I would like to convert the current date and time into a hex time stamp, something like:
Tue Feb 2 10:27:46 GMT 2010 converted into 0x6d054a874449e
I would like to do this from a bash script, any idea how I might do that?
Thanks
J
printf '0x%x' $(date +%s)
Without knowing the unit or epoch for your hex timestamp, it's hard to say for sure (and I was slightly confused by your example of "Feb 2" which is not even close to the current date!).
date +%s will convert the current date into a time_t, the number of seconds since the usual Unix epoch (which is midnight on 1st Jan 1970).
printf "0x%x" some_number will convert a value from decimal to hex.
If you need to convert to a different epoch / unit, you will need to do some calculation. You can do arithmetic in bash using $(( expression )):
$ time_t=$(date +%s)
$ echo $(($time_t * 1000))
1284505668000
If you want to convert an arbitrary date (like your "Feb 2 ..." example), rather than the current one, and are happy to assume that you have the GNU version of date, then you can use the -d option along with the +%s output format to do the conversion:
$ date -d 'Tue Feb 2 10:27:46 GMT 2010' +%s
1265106466
An example of putting this all together:
$ time_t=$(date -d 'Tue Feb 2 10:27:46 GMT 2010' +%s)
$ time_t_ms=$(($time_t * 1000))
$ hexstamp=$(printf "0x%x" $time_t_ms)
$ echo $hexstamp
0x1268e38b4d0
Seconds since unix epoch, in hex:
echo "$(date +%s)"|xargs printf "0x%x"
0x59a8de5b
Milliseconds since the epoch:
echo "$(date +%s%N)/1000000"|bc|xargs printf "0x%x"
0x15e3ba702bb
Microseconds:
echo "$(date +%s%N)/1000"|bc|xargs printf "0x%x"
0x55818f6eea775
Nanoseconds:
echo "$(date +%s%N)"|xargs printf "0x%x"
0x14e0219022e3745c

Resources