Get the difference between two dates in terms of days

Discussion in 'C Programming' started by gops, Feb 10, 2004.

  1. gops

    gops Guest

    Hi,

    Can anyone send me a specific example of getting the differncce
    between two dates in terms of no of days using a C library function.

    I have tried using difftime, but I am not successfull so far. Pls can
    any one help me out.

    Regards
    Gopal
     
    gops, Feb 10, 2004
    #1
    1. Advertising

  2. gops wrote:

    > Hi,
    >
    > Can anyone send me a specific example of getting the differncce
    > between two dates in terms of no of days using a C library function.
    >
    > I have tried using difftime, but I am not successfull so far. Pls can
    > any one help me out.


    This is just a quick hack. I have actually checked that it basically works,
    but it's not hugely elegant code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>

    void stotm(struct tm *p, const char *s)
    {
    int i;
    struct tm blank = {0};
    *p = blank;

    for(i = 0; i < 4; i++)
    {
    p->tm_year *= 10;
    p->tm_year += *s++ - '0';
    }
    p->tm_year -= 1900;
    for(i = 0; i < 2; i++)
    {
    p->tm_mon *= 10;
    p->tm_mon += *s++ - '0';
    }
    --p->tm_mon;
    p->tm_mday = 10 * (*s++ - '0');
    p->tm_mday += *s++ - '0';
    }

    void calcdiff(const char *old, const char *new)
    {
    struct tm t1 = {0};
    struct tm t2 = {0};
    time_t tt1 = {0};
    time_t tt2 = {0};

    stotm(&t1, old);
    stotm(&t2, new);
    tt1 = mktime(&t1);
    tt2 = mktime(&t2);
    if(tt1 != -1 && tt2 != -1)
    {
    long day = 0;
    double d = difftime(tt1, tt2);
    if(d < 0) d *= -1.0;
    day = d / 86400;
    printf("%ld day%s\n", day, day == 1 ? "" : "s");
    }
    else
    {
    fprintf(stderr, "bad date format: %ld %ld\n", (long)tt1, (long)tt2);
    }
    }

    int main(int argc, char **argv)
    {
    if(argc < 3 || strlen(argv[1]) < 8 || strlen(argv[2]) < 8)
    {
    fprintf(stderr, "Learn.\n");
    }
    else
    {
    if(strcmp(argv[1], argv[2]) < 0)
    {
    calcdiff(argv[1], argv[2]);
    }
    else
    {
    calcdiff(argv[2], argv[1]);
    }
    }
    return 0;
    }



    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Feb 10, 2004
    #2
    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,192
    Brian Henry
    Nov 19, 2003
  2. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    881
    Rob Schieber
    Sep 30, 2005
  3. =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?=

    difference (in days) of two dates

    =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?=, Aug 23, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    337
    Hans Kesting
    Aug 23, 2007
  4. David
    Replies:
    2
    Views:
    119
    Kyle Peterson
    Jun 29, 2006
  5. kirke

    list of days between two days

    kirke, Oct 19, 2006, in forum: Javascript
    Replies:
    7
    Views:
    144
    Julian Turner
    Oct 20, 2006
Loading...

Share This Page