Calculate the date after subtracting nmbr of days form a date

Discussion in 'C Programming' started by Laery, Feb 24, 2005.

  1. Laery

    Laery Guest

    Hi,

    I'm currently adding a new module to an old borland C3.1 application
    (dos).
    And I need to calculate a date by subtracting the number of days from
    a given date.

    I know I could use an array of days in the months and go back by
    leaping when 1 is reached. (keeping the 29the of feb and january/year
    in mind).
    But is there no function in Borland C3.1 which will do this for me?

    I couldn't find one in the help?

    Regards
    Laery
    Laery, Feb 24, 2005
    #1
    1. Advertising

  2. Laery

    John Guest

    (Laery) wrote in
    news::

    > I'm currently adding a new module to an old borland C3.1 application
    > (dos).
    > And I need to calculate a date by subtracting the number of days from
    > a given date.


    [...]

    > I couldn't find one in the help?


    Look-up difftime and localtime... They're functions that have been in the
    Borland libraries at least since v1.5 and should help you do what you're
    trying to do.


    John
    John, Feb 24, 2005
    #2
    1. Advertising

  3. Laery

    infobahn Guest

    Laery wrote:
    >
    > Hi,
    >
    > I'm currently adding a new module to an old borland C3.1 application
    > (dos).
    > And I need to calculate a date by subtracting the number of days from
    > a given date.
    >


    Load the date into a struct tm, taking care to ensure that:

    (a) all fields you don't set are cleared to 0, eg with:
    struct tm date = (0); /* for now, just trust me on this */
    (b) the year field contains the full year less 1900 (so, for 2005,
    it would be set to 105).
    (c) the month field is in the range 0 to 11.
    (d) all other relevant fields are set correctly.

    Now subtract the number of days you want, and then pass the
    struct's address to mktime(), catching the result in a time_t
    object.

    If the result is not (time_t)-1 (which would indicate failure to
    convert the date as you require), it can be passed to localtime()
    or gmtime(), which both return pointers to struct tm from which
    you can extract the date information you require.
    infobahn, Feb 24, 2005
    #3
  4. Laery

    Richard Bos Guest

    John <> wrote:

    > (Laery) wrote in
    > news::
    >
    > > I'm currently adding a new module to an old borland C3.1 application (dos).
    > > And I need to calculate a date by subtracting the number of days from
    > > a given date.

    >
    > > I couldn't find one in the help?

    >
    > Look-up difftime and localtime... They're functions that have been in the
    > Borland libraries at least since v1.5 and should help you do what you're
    > trying to do.


    No, they won't. difftime() gives the difference, in seconds, between two
    time_t's; it doesn't allow you to change an existing time_t. localtime()
    converts from time_t to struct tm, but doesn't do any other
    computations.
    Using mktime() is the right solution. If BC3.1 doesn't have it yet
    (i.e., if it's pre-ISO), you _may_ be able to get away with subtracting
    days*24*3600 from a time_t, but do note that this relies on time_t being
    a straight number of seconds since the epoch, which it isn't required to
    be. Doing so would make your code unportable, but if it's already
    Borland-specific, that may not be a problem. Adding a comment explaining
    the hack would be a good idea, even so.

    Richard
    Richard Bos, Feb 24, 2005
    #4
  5. Laery

    Stan Milam Guest

    Laery wrote:
    > Hi,
    >
    > I'm currently adding a new module to an old borland C3.1 application
    > (dos).
    > And I need to calculate a date by subtracting the number of days from
    > a given date.
    >
    > I know I could use an array of days in the months and go back by
    > leaping when 1 is reached. (keeping the 29the of feb and january/year
    > in mind).
    > But is there no function in Borland C3.1 which will do this for me?
    >
    > I couldn't find one in the help?
    >
    > Regards
    > Laery


    I have a really robust date library which was published in the C User's
    Journal a few years ago. Drop me a line and I will email it to you
    (damn, I've got to get that web page up!). I also used it to write a
    Unix shell utility called ADU (a date utility) that allows you to do
    date math in the Unix shell. If there were a decent shell for Windows
    you could use it there too.
    Stan Milam, Feb 24, 2005
    #5
  6. Laery

    Jack Klein Guest

    On Thu, 24 Feb 2005 11:04:15 +0000 (UTC), infobahn
    <> wrote in comp.lang.c:

    > Laery wrote:
    > >
    > > Hi,
    > >
    > > I'm currently adding a new module to an old borland C3.1 application
    > > (dos).
    > > And I need to calculate a date by subtracting the number of days from
    > > a given date.
    > >

    >
    > Load the date into a struct tm, taking care to ensure that:
    >
    > (a) all fields you don't set are cleared to 0, eg with:
    > struct tm date = (0); /* for now, just trust me on this */


    ITYM struct tm date = { 0 };

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 25, 2005
    #6
  7. Laery

    Randy Howard Guest

    In article <>,
    says...
    > Laery wrote:
    > >
    > > Hi,
    > >
    > > I'm currently adding a new module to an old borland C3.1 application
    > > (dos).
    > > And I need to calculate a date by subtracting the number of days from
    > > a given date.
    > >

    >
    > Load the date into a struct tm, taking care to ensure that:
    >
    > (a) all fields you don't set are cleared to 0, eg with:
    > struct tm date = (0); /* for now, just trust me on this */


    It would be more trustworthy with braces instead of parens. :)

    --
    Randy Howard (2reply remove FOOBAR)
    "Making it hard to do stupid things often makes it hard
    to do smart ones too." -- Andrew Koenig
    Randy Howard, Feb 25, 2005
    #7
  8. Laery

    infobahn Guest

    Jack Klein wrote:
    >
    > On Thu, 24 Feb 2005 11:04:15 +0000 (UTC), infobahn
    > <> wrote in comp.lang.c:
    >
    > > (a) all fields you don't set are cleared to 0, eg with:
    > > struct tm date = (0); /* for now, just trust me on this */

    >
    > ITYM struct tm date = { 0 };


    I do indeed. I was clearly tempting fate a little too much with that
    comment.
    infobahn, Feb 25, 2005
    #8
  9. Laery

    infobahn Guest

    Randy Howard wrote:
    >
    > In article <>,
    > says...
    > > Laery wrote:
    > > >
    > > > Hi,
    > > >
    > > > I'm currently adding a new module to an old borland C3.1 application
    > > > (dos).
    > > > And I need to calculate a date by subtracting the number of days from
    > > > a given date.
    > > >

    > >
    > > Load the date into a struct tm, taking care to ensure that:
    > >
    > > (a) all fields you don't set are cleared to 0, eg with:
    > > struct tm date = (0); /* for now, just trust me on this */

    >
    > It would be more trustworthy with braces instead of parens. :)


    <sigh>
    Some days, it just doesn't pay to fire up your newsreader. :-$
    </sigh>
    infobahn, Feb 25, 2005
    #9
  10. Laery

    dragoncoder Guest

    Hello Stan

    If you could mail me a copy of the date library you are talking about,
    that will be very kind of you. Just mail it to me at

    ptiwaryATmahindrabtDOTcom

    Thanks in advance.
    dragoncoder, Feb 25, 2005
    #10
  11. Laery

    Randy Howard Guest

    In article <>,
    says...
    > <sigh>
    > Some days, it just doesn't pay to fire up your newsreader. :-$
    > </sigh>


    Unfortunately, I know exactly what you mean.

    --
    Randy Howard (2reply remove FOOBAR)
    "Making it hard to do stupid things often makes it hard
    to do smart ones too." -- Andrew Koenig
    Randy Howard, Feb 25, 2005
    #11
  12. Laery

    Laery Guest

    Hi,

    Thanks for all the responses.

    Regards
    Laery
    (Laery) wrote in message news:<>...
    > Hi,
    >
    > I'm currently adding a new module to an old borland C3.1 application
    > (dos).
    > And I need to calculate a date by subtracting the number of days from
    > a given date.
    >
    > I know I could use an array of days in the months and go back by
    > leaping when 1 is reached. (keeping the 29the of feb and january/year
    > in mind).
    > But is there no function in Borland C3.1 which will do this for me?
    >
    > I couldn't find one in the help?
    >
    > Regards
    > Laery
    Laery, Feb 25, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry
    Replies:
    6
    Views:
    16,154
    Brian Henry
    Nov 19, 2003
  2. =?Utf-8?B?TWFubnkgQ2hvaGFu?=

    subtracting days from date

    =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Nov 8, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    27,444
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Nov 9, 2004
  3. David Stockwell
    Replies:
    2
    Views:
    686
    Anna Martelli Ravenscroft
    Sep 20, 2004
  4. Gerrit
    Replies:
    0
    Views:
    377
    Gerrit
    Sep 20, 2004
  5. joe shaboo

    calculate date 4 days ago

    joe shaboo, May 4, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    285
    Jim Cochrane
    May 5, 2004
Loading...

Share This Page