Results 1 to 9 of 9

Thread: dateadd

  1. #1
    Join Date
    Jun 2008
    Location
    As. - Paraguay - GMT -4
    Posts
    95

    Thumbs up 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:
    Code:
    date '+%Y-%m-%d %H:%M:%S' --date=' +2 days'
    returning this output
    Code:
    2008-06-25 12:45:21
    or
    Code:
    date '+%Y-%m-%d %H:%M:%S' --date='2007-10-10 00:10:00 +1 days'
    output
    Code:
    2007-10-11 00:10:00

    even when I add a time to the current date
    Code:
    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

    Code:
    date '+%Y-%m-%d %H:%M:%S' --date='2007-10-10 00:10:00 +5 days'
    output
    Code:
    2007-10-11 00:10:00
    the same thing happens with a time add
    Code:
    date '+%Y-%m-%d %H:%M:%S' --date='2007-10-10 00:10:00 +5 minutes'
    output
    Code:
    2007-10-09 20:06:00
    what is wrong with this?

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,861

    Default Re: dateadd

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

    Look it these:
    Code:
    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 ).
    Henk van Velden

  3. #3
    Join Date
    Jun 2008
    Location
    As. - Paraguay - GMT -4
    Posts
    95

    Default Re: dateadd

    :s

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

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: dateadd

    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.

  5. #5
    Theo v. Werkhoven NNTP User

    Default Re: dateadd

    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"

  6. #6
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: dateadd

    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    42

    Default Re: dateadd

    This kind of works
    Code:
    ~> 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

  8. #8
    Join Date
    Jun 2008
    Posts
    42

    Default Re: dateadd

    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

  9. #9
    Join Date
    Jun 2008
    Location
    As. - Paraguay - GMT -4
    Posts
    95

    Default Re: dateadd

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •