subtracting dates with specific format - shell

i have search for a solution to my shell date subtraction issue with no joy so here goes.
i have a date format like so %m%d%H%M%S which is "0102231203" and the second %Y%m%d%H%M%S, i can take the year off the second one and do a normal subtraction but when it is over a day it becomes an issue with the time being incorrect.
here is what i have tried so far
BTT=0102234500
TPP=0102233635 (after removing the year)
BT=date -d ${BTT}
TP=date -d ${TPP}
and
BT=date -d $BTT +%m%d%H%M%S
TP=date +%m%d%H%M%S -d ${TPP}
date: invalid date `0102234500'
date: invalid date `0102233635'
BT=date -d #${BTT} +%m%d%H%M%S
TP=date +%m%d%H%M%S -d #${TPP}
weird output
0329071355
0329072820
BT=date -d #${BTT}
TP=date -d #${TPP}
Thu Mar 29 07:13:55 BST 1973
Thu Mar 29 07:28:20 BST 1973
even changed it to add the year to both still
BTT=20130102234500
TPP=20130102233635
BT=date -d #${BTT}
TP=date -d #${TPP}
Fri Jul 19 08:53:55 GMT 639867
Fri Jul 19 09:08:20 GMT 639867
how do i resolve this issue.
tnx

The -d option of date accept human readable string so if you can have full length date you can do :
me#server:/tmp$ BTT=`date +"%Y-%m-%d %H:%M:%S"`
me#server:/tmp$ TPP=`date +"%Y-%m-%d %H:%M:%S"`
me#server:/tmp$ echo $((`date -d "$TPP" +%s`-`date -d "$BTT" +%s`))
3
With your datas :
me#server:/tmp$ BTT="2013-01-02 23:45:00"
me#server:/tmp$ TPP="2013-01-02 23:36:35"
me#server:/tmp$ echo $((`date -d "$BTT" +%s`-`date -d "$TPP" +%s`))
505
With the results in seconds.

Related

How to calculate date in the past from given date (also in past) using "date -d 'xx/xx/xxxx - xx' "

I have a input date:
01-01-2019 08:30:00
And I would like to back it in time by:
2hours 10minutes
So the new date should be like:
01-01-2019 06:20
I have tried commands:
(1)date -d "2019-01-01 08:30:00 + 1hour"
which return: Tue Jan 1 09:30:00 STD 2019 (ok, but seems to be lucky shot)
(2)date -d "2019-01-01 08:30:00 - 1hour"
which return: Tue Jan 1 11:30:00 STD 2019 that means +3hours (wrong)
(3)date -d "2019-01-01 08:30:00 + 3hours"
which return: Tue Jan 1 07:30:00 STD 2019 that means -1hours (wrong)
(4)date -d "2019-01-01 08:30:00 + 2minutes"
which return: Tue Jan 1 07:31:00 STD 2019 that means -59minutes (wrong)
You probably need to explicitly mention your timezone:
$ TZ=STD date -d '2019-01-01 08:30:00 STD + 1 hour'
Tue Jan 1 09:30:00 STD 2019
$ TZ=STD date -d '2019-01-01 08:30:00 STD - 3 hour'
Tue Jan 1 05:30:00 STD 2019
$ TZ=STD date -d '2019-01-01 08:30:00 STD + 2 minutes'
Tue Jan 1 08:32:00 STD 2019
This seems to alleviate dates confusion.
The is only one most reliable way of doing that:
date -d "#$(( $(date -d '2019-01-01 08:30:00' +%s) - ( 2 * 60 + 10 ) * 60 ))"
First convert to seconds since epoch.
Then subtract the time you want.
Then use date to format the output.

shell date "-n hours" differs with "n hours ago" in some situation

in shell, when I print this
date -d "2016-11-23 13:05 -1 hours " "+%Y-%m-%d %H:00:00"
I get 2016-11-23 23:00:00.Strange!
when I print this
date -d "2016-11-23 13:05 1 hours ago" "+%Y-%m-%d %H:00:00"
I get 2016-11-23 12:00:00.
Why they are different? What I think is that they are both 2016-11-23 12:00:00.
This is because the negative number is treated as an offset to your timezone, not to the 13:05. In my timezone, MET (one hour east of GMT), this is what I get:
$ date -d "2016-11-23 13:05 -1 hours " "+%Y-%m-%d %H:00:00"
2016-11-23 16:00:00
$ TZ=GMT date -d "2016-11-23 13:05 -1 hours " "+%Y-%m-%d %H:00:00"
2016-11-23 15:00:00
$ TZ=GMT-1 date -d "2016-11-23 13:05 -1 hours " "+%Y-%m-%d %H:00:00"
2016-11-23 16:00:00
$ TZ=GMT-1 date -d "2016-11-23 13:05 -2 hours " "+%Y-%m-%d %H:00:00"
2016-11-23 17:00:00
The timezone offset is usually specified as a four digit number, as in
Sun, 29 Feb 2004 16:21:42 -0800
but apparently date(1) is happy with a -1 as well.
From the man page:
DATE STRING
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb
2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may
contain items indicating calendar date, time of day, time zone, day of week, relative
time, relative date, and numbers. An empty string indicates the beginning of the day.
The date string format is more complex than is easily documented here but is fully
described in the info documentation.

Loop minutes and echo with bash

I want to iterate all the minutes in a month (the purpose will be to generate a CSV file).
But when I try this:
d="2016-09-01 00:00:00"
while [ "$d" != "2016-09-30 00:00:00" ]; do
echo $d
d=$(date --utc "+%Y-%m-%d %H:%M:00" -d "$d + 1 minute" )
done
Both the hour and minute are being incremented:
2016-09-01 00:00:00
2016-09-01 01:01:00
2016-09-01 02:02:00
2016-09-01 03:03:00
2016-09-01 04:04:00
2016-09-01 05:05:00
2016-09-01 06:06:00
2016-09-01 07:07:00
2016-09-01 08:08:00
What am I doing wrong and how to correctly loop minutes?
I would work with Unix timestamps instead.
d=$(date --utc +%s -d "2016-09-01 00:00:00")
end=$(date --utc +%s -d "2016-09-30 00:00:00")
while [ "$d" != "$end" ]; do
date --utc "+%Y-%m-%d %H:%M:00" -d "#$d"
d=$(( d + 60 ))
done
You can add timezone UTC after your date string variable $d to get the right output:
d="2016-09-01 00:00:00"
for ((i=0; i<=60; i++)); do
date --utc -d "$d UTC + $i minute"
done
Thu Sep 1 00:00:00 UTC 2016
Thu Sep 1 00:01:00 UTC 2016
Thu Sep 1 00:02:00 UTC 2016
Thu Sep 1 00:03:00 UTC 2016
Thu Sep 1 00:04:00 UTC 2016
Thu Sep 1 00:05:00 UTC 2016
...
...
Thu Sep 1 00:55:00 UTC 2016
Thu Sep 1 00:56:00 UTC 2016
Thu Sep 1 00:57:00 UTC 2016
Thu Sep 1 00:58:00 UTC 2016
Thu Sep 1 00:59:00 UTC 2016
Thu Sep 1 01:00:00 UTC 2016
Note use of UTC after $d.
Using + after a time component in the date string is used for ' time zone correction' not for doing what you want to do. Interestingly, inverting date and time works:
$ date "+%Y-%m-%d %H:%M:00" -d "21:31:00 2016-09-03 + 1 minute "
2016-09-03 21:32:00
while the other way around messes with timezones and offsets so the result might depend on your local configuration:
$ TZ=Europe/London date "+%Y-%m-%d %H:%M:00" -d "2016-09-03 21:32:00 + 1 minute "
2016-09-03 21:33:00
$ TZ=Europe/Brussels date "+%Y-%m-%d %H:%M:00" -d "2016-09-03 21:32:00 + 1 minute "
2016-09-03 22:33:00
$ TZ=Asia/Singapore date "+%Y-%m-%d %H:%M:00" -d "2016-09-03 21:32:00 + 1 minute "
2016-09-04 04:33:00

How to subtract 5 minute from date

I have this data:
`date +%Y-%m-%d`" 00:00:00"
that return 2015-10-08 00:00:00
I would like cancel 5 minute:
2015-10-07 23:55:00
Many thanks
You need to subtract 5 minutes from a known point in time:
$ date -d "00:00:00 today"
Thu Oct 8 00:00:00 EDT 2015
$ date -d "00:00:00 today -5 minutes"
Wed Oct 7 23:55:00 EDT 2015
You just need to add your format string.
There's more than one way to subtract a value from the current time, although this should match the format shown in your question:
date -d "-5 min" "+%Y-%m-%d %H:%M:%S"
Result:
2015-10-08 15:26:13

How to find date n days in the future from past date?

I want to find a date that is 57 –working– days after an arbitrary date using date or bash. For instance,
today is august 21st,
reference date is july 15th,
what days will be 57 working days after july 15th?
This should work to just get all days
date -d '7/15/14 +57 days'
To get number of work days (M..F) you can do something lazy like this
#!/bin/bash
days=0
for ((i=1;i>0;i++)) do
future=$(date -d "7/15/14 +$i days" '+%w')
((future!=0 && future!=6)) && ((days++)) # ignore sunday (0) and saturday (6)
((days==57)) && date -d "7/15/14 +$i days" && break
done
e.g.
> ./abovescript
> Thu Oct 2 00:00:00 CDT 2014
Weird solution:
day=21
mon=8
year=2014
days=4
curl -s "http://www.timeanddate.com/scripts/dateserver.php?mode=addweekdays&d1=$day&m1=$mon&y1=$year&type=add&ad=$days&atyp=0&ach=3" | sed -n 's|.*<h2>Result: \(.*\)</h2>.*|\1|p'
prints
Wednesday, August 27, 2014
the date after 4 working days from 2014.08.21
for the
day=15
mon=7
year=2014
days=57
prints
Friday, October 3, 2014

Resources