dateadd

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'

output


2007-10-09 20:06:00

what is wrong with this?

What is wromg with this? IMHO it is simply wrong!

Look it these:

henk@boven:~> date '+%Y-%m-%d %H:%M:%S' --date='2007-10-10 00:10:00 +5 minutes'
2007-10-10 02:06:00
henk@boven:~> TZ=UTC0 date '+%Y-%m-%d %H:%M:%S' --date='2007-10-10 00:10:00 +5 minutes'
2007-10-10 00:06:00
henk@boven:~>

Doing this in timezone UTC0 as advised somewhere in info date .

When you think this is broken, I agree (does not help you very much :mad: ).

:s

Is there a way to do the same in C or C++?

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.

This kind of works


~> bash --version
GNU bash, version 3.2.25(1)-release (i586-suse-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
~> date --version
date (GNU coreutils) 6.9
Copyright (C) 2007 Free Software Foundation, Inc.
...
~> date --date='tomorrow + 5 minutes 2004-02-28 10:00:00' +'%F %H:%M:%S'
2004-02-29 10:05:00
~> date --date='+ 1 day + 5 minutes 2004-02-28 10:00:00' +'%F %H:%M:%S'
2004-02-29 10:05:00
~> date --date='+ 2 days + 5 minutes 2004-02-28 10:00:00' +'%F %H:%M:%S'
2004-03-01 10:05:00
~> date --date='+ 11 days + 55 minutes + 2 seconds 2004-02-28 10:00:00' +'%F %H:%M:%S'
2004-03-10 10:55:02
~> date --date='- 2 weeks - 5 minutes - 59 seconds 2004-02-28 10:00:00' +'%F %H:%M:%S'
2004-02-14 09:54:01

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

Thank you very much to all of you :slight_smile:
now it works!