I want to make a dateadd with the date command but this only works with the current date, not with a customized date.
Everything goes fine when I tray to a add single days to a date:
date '+%Y-%m-%d %H:%M:%S' --date=' +2 days'
returning this output
2008-06-25 12:45:21
or
date '+%Y-%m-%d %H:%M:%S' --date='2007-10-10 00:10:00 +1 days'
output
2007-10-11 00:10:00
even when I add a time to the current date
date '+%Y-%m-%d %H:%M:%S' --date=' +1 minutes'
BUT
if I add a day greatest than 1 (one) to a date that is not the current one (today), does not work
date '+%Y-%m-%d %H:%M:%S' --date='2007-10-10 00:10:00 +5 days'
output
2007-10-11 00:10:00
the same thing happens with a time add
date '+%Y-%m-%d %H:%M:%S' --date='2007-10-10 00:10:00 +5 minutes'
As I read info date, the semantics you want are simply not supported. In your examples it’s treating the +N as some weird timezone. Relative dates are only relative to current time, not to a time you specify. You’ll have to do it some other way.
The carbonbased lifeform ken yap inspired
opensuse.org.help.programming-scripting with:
>
> As I read info date, the semantics you want are simply not supported. In
> your examples it’s treating the +N as some weird timezone. Relative
> dates are only relative to current time, not to a time you specify.
> You’ll have to do it some other way.
!/usr/bin/python
from datetime import *
Now = datetime.now()
Interval = timedelta(seconds=0, minutes=0, hours=0, days=2)
DateAdd = Now + Interval
>>> print Now
2008-06-24 00:54:28.091262
>>> print Interval
2 days, 0:00:00
>>> print DateAdd
2008-06-26 00:54:28.091262
Theo
theo at van-werkhoven.nl ICQ:277217131 SuSE Linux linuxcounter.org: 99872 Jabber:muadib at jabber.xs4all.nl AMD XP3000+ 1024MB
“ik heb niets tegen Microsoft, ik heb iets tegen
de uitwassen van Microsoft”
And if you really want to do it all in shell and using date, you could output the first operand as seconds past epoch (%s formatter), add the required number of seconds to it, using expr or bash $(()), then reconvert to your required format using the @secsfromepoch input format.
Just to add an example to previous post
~> date -r 1337466.asx +’%F %H:%M:%S’
2008-06-24 09:59:54
~> x=$(date -r 1337466.asx +’%F %H:%M:%S’); date --date=’+ 5 days + 1 hour ‘"$x" +’%F %H:%M:%S’
2008-06-29 10:59:54