what's wrong with my program ?

Discussion in 'C Programming' started by sugaray, Jun 7, 2004.

  1. sugaray

    sugaray Guest

    hi, below is my program for doing exercise with library time functions,
    something is not right with it, and for the time being i couldn't figure
    out what's wrong and where, thanx for your help.

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

    double TimeDifference(struct tm *t1,struct tm *t2)
    {
    return difftime(mktime(t1),mktime(t2));
    }


    char *ConstructTime(struct tm *time,int month,int day,int year)
    {
    time->tm_mon=month-1;
    time->tm_mday=day;
    time->tm_year=year-1900;
    mktime(time);
    return asctime(time);
    }

    int main(int argc,char **argv)
    {
    struct tm now,t1,t2;
    char *tmstr;
    double diff;
    int month,day,year;

    scanf("%d%d%d",&month,&day,&year);
    tmstr=ConstructTime(&now,month,day,year);
    printf("%s",tmstr);
    ConstructTime(&t1,6,7,2004);
    ConstructTime(&t2,6,9,2004);
    diff=TimeDifference(&t1,&t2);
    printf("%f\n",diff);


    return 0;
    }
    sugaray, Jun 7, 2004
    #1
    1. Advertising

  2. sugaray

    T.M. Sommers Guest

    sugaray wrote:
    > hi, below is my program for doing exercise with library time functions,
    > something is not right with it, and for the time being i couldn't figure
    > out what's wrong and where, thanx for your help.
    >
    > #include <time.h>
    > #include <stdio.h>
    >
    > double TimeDifference(struct tm *t1,struct tm *t2)
    > {
    > return difftime(mktime(t1),mktime(t2));
    > }
    >
    >
    > char *ConstructTime(struct tm *time,int month,int day,int year)
    > {
    > time->tm_mon=month-1;
    > time->tm_mday=day;
    > time->tm_year=year-1900;


    At this point, all of the members of time except the 3 you just
    set contain garbage. You need to set them to some rational
    values before going on.

    > mktime(time);
    > return asctime(time);
    > }

    --
    Thomas M. Sommers -- -- AB2SB
    T.M. Sommers, Jun 7, 2004
    #2
    1. Advertising

  3. "sugaray" <> wrote in message news:...
    > hi, below is my program for doing exercise with library time functions,
    > something is not right with it, and for the time being i couldn't figure
    > out what's wrong and where, thanx for your help.
    >
    > #include <time.h>
    > #include <stdio.h>
    >
    > double TimeDifference(struct tm *t1,struct tm *t2)
    > {
    > return difftime(mktime(t1),mktime(t2));
    > }
    >
    >
    > char *ConstructTime(struct tm *time,int month,int day,int year)
    > {
    > time->tm_mon=month-1;
    > time->tm_mday=day;
    > time->tm_year=year-1900;
    > mktime(time);
    > return asctime(time);
    > }
    >
    > int main(int argc,char **argv)
    > {
    > struct tm now,t1,t2;
    > char *tmstr;
    > double diff;
    > int month,day,year;
    >


    /* adding these removed the misbehaviour */
    memset ( &now, 0, sizeof now );
    memset ( &t1, 0, sizeof t1 );
    memset ( &t2, 0, sizeof t2 );


    > scanf("%d%d%d",&month,&day,&year);
    > tmstr=ConstructTime(&now,month,day,year);
    > printf("%s",tmstr);
    > ConstructTime(&t1,6,7,2004);
    > ConstructTime(&t2,6,9,2004);
    > diff=TimeDifference(&t1,&t2);
    > printf("%f\n",diff);
    >
    >
    > return 0;
    > }
    Vijay Kumar R Zanvar, Jun 7, 2004
    #3
  4. sugaray

    Richard Bos Guest

    "Vijay Kumar R Zanvar" <> wrote:

    > "sugaray" <> wrote in message news:...
    > > char *ConstructTime(struct tm *time,int month,int day,int year)
    > > {
    > > time->tm_mon=month-1;
    > > time->tm_mday=day;
    > > time->tm_year=year-1900;
    > > mktime(time);
    > > return asctime(time);
    > > }
    > >
    > > int main(int argc,char **argv)
    > > {
    > > struct tm now,t1,t2;
    > > char *tmstr;
    > > double diff;
    > > int month,day,year;

    >
    > /* adding these removed the misbehaviour */
    > memset ( &now, 0, sizeof now );
    > memset ( &t1, 0, sizeof t1 );
    > memset ( &t2, 0, sizeof t2 );


    Not a good solution. First of all, the problem really is in
    ConstructTime(). With a name like that, one can expect it to construct a
    complete time struct, so it should do so. Second, if you use this
    solution, you need to use it for every single time you create, not just
    once in the function itself; forget one, and the problem comes back.
    Third, this sets the structs to all-bits-zero, not to sensible initial
    members; in particular, it does not set tm_isdst to something negative,
    which is what I would want it to be.
    Oh, and another small bug: it is never a good idea to use the same name
    for one of your structs as for a closely related Library function...
    A better solution is this:

    char *ConstructTime(struct tm *the_time, int month,int day,int year)
    {
    the_time->tm_mon=month-1;
    the_time->tm_mday=day;
    the_time->tm_year=year-1900;
    the_time->tm_sec=the_time->tm_min=the_time->tm_hour=0;
    the_time->tm_wday=the_time->tm_yday=0;
    the_time->tm_isdst=-1;
    mktime(the_time);
    return asctime(the_time);
    }

    Note also that asctime() returns a pointer to a static array, so if you
    want to use two of those strings, copy the first _string_, not the first
    pointer, before you get the second one. The second call will over-write
    the string written by the first.

    Richard
    Richard Bos, Jun 7, 2004
    #4
  5. sugaray

    Al Bowers Guest

    sugaray wrote:
    > hi, below is my program for doing exercise with library time functions,
    > something is not right with it, and for the time being i couldn't figure
    > out what's wrong and where, thanx for your help.
    >
    > #include <time.h>
    > #include <stdio.h>
    >
    > double TimeDifference(struct tm *t1,struct tm *t2)
    > {
    > return difftime(mktime(t1),mktime(t2));
    > }
    >


    Function mktime will return (time_t)-1 should it
    fail to convert a calendar time. Therefore the
    function arguments must be of the form that will
    ensure a conversion. Or, you must modify the function.

    >
    > char *ConstructTime(struct tm *time,int month,int day,int year)
    > {
    > time->tm_mon=month-1;
    > time->tm_mday=day;
    > time->tm_year=year-1900;
    > mktime(time);
    > return asctime(time);
    > }
    >


    I see two problems in the use of function ConstructTime.

    First you only set three members of the struct. All the
    other members are indeterminant.

    Second, if function mktime is unable to determine a
    calendar time, components are not set to represent the
    specified calendar time, nor their values forced
    to the normal ranges. Using this illegitmate struct
    as argument to function asctime may be unpleasant.

    A possible fix:

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

    double TimeDifference(struct tm *t1,struct tm *t2)
    {
    return difftime(mktime(t1),mktime(t2));
    }


    char *ConstructTime(struct tm *time,int month,int day,int year)
    {
    struct tm tmp = {0};

    *time = tmp;
    time->tm_mon=month-1;
    time->tm_mday=day;
    time->tm_year=year-1900;
    time->tm_isdst = -1;
    return mktime(time)==(time_t)-1?NULL:asctime(time);
    }

    int main(int argc,char **argv)
    {
    struct tm now,t1,t2;
    char *tmstr1, *tmstr2;
    double diff;
    int month,day,year;

    printf("Enter the date (MM DD YYYY?): ");
    fflush(stdout);
    scanf("%d%d%d",&month,&day,&year);
    tmstr1=ConstructTime(&now,month,day,year);
    if(tmstr1) printf("%s\n",tmstr1);
    tmstr1 = ConstructTime(&t1,6,7,2004);
    if(tmstr1) printf("Time 1: %s",tmstr1);
    tmstr2 = ConstructTime(&t2,6,8,2004);
    if(tmstr2) printf("Time 2: %s", tmstr2 );
    if(tmstr1 && tmstr2)
    {
    diff=TimeDifference(&t2,&t1);
    printf("Difference is %.f secconds\n",diff);
    }
    return 0;
    }

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
    Al Bowers, Jun 7, 2004
    #5
  6. sugaray wrote:

    > hi, below is my program for doing exercise with library time functions,
    > something is not right with it, and for the time being i couldn't figure
    > out what's wrong and where, thanx for your help.
    >
    > #include <time.h>
    > #include <stdio.h>
    >
    > double TimeDifference(struct tm *t1,struct tm *t2)
    > {
    > return difftime(mktime(t1),mktime(t2));
    > }
    >
    >
    > char *ConstructTime(struct tm *time,int month,int day,int year)
    > {
    > time->tm_mon=month-1;
    > time->tm_mday=day;
    > time->tm_year=year-1900;
    > mktime(time);
    > return asctime(time);
    > }


    You need to initialize the elements of *time (which should be spelt some
    way other that "time"). Here's one approach a modern C compiler would like:

    char *ConstructTime(struct tm *t, int month, int day, int year)
    {
    *t = (struct tm) {
    .tm_mon = month - 1,.tm_mday = day,.tm_year =
    year - 1900,.tm_isdst = -1};
    mktime(t);
    return asctime(t);
    }

    An approach that doesn't need the anonymous struct is to declare a local
    struct tm t_tmp, with the proper base values, and copy it: *t = t_tmp;
    Martin Ambuhl, Jun 7, 2004
    #6
  7. sugaray

    sugaray Guest

    Martin Ambuhl <> wrote in message news:<>...

    > char *ConstructTime(struct tm *t, int month, int day, int year)
    > {
    > *t = (struct tm) {
    > .tm_mon = month - 1,.tm_mday = day,.tm_year =>
    > year - 1900,.tm_isdst = -1};
    > mktime(t);
    > return asctime(t);
    > }


    > An approach that doesn't need the anonymous struct is to declare a local
    > struct tm t_tmp, with the proper base values, and copy it: *t = t_tmp;



    so, why use a temp variable rather set the values directly to *t ?
    sugaray, Jun 8, 2004
    #7
    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. VisionSet

    what's wrong in this program

    VisionSet, Sep 24, 2004, in forum: Java
    Replies:
    12
    Views:
    536
    Thomas G. Marshall
    Sep 28, 2004
  2. Replies:
    7
    Views:
    408
  3. Matthew
    Replies:
    7
    Views:
    654
    Priscilla Walmsley
    Jan 7, 2005
  4. jose luis fernandez diaz

    why this program is wrong on Tru64 OS ?

    jose luis fernandez diaz, Nov 25, 2003, in forum: C++
    Replies:
    2
    Views:
    410
    Stephan Br?nnimann
    Nov 26, 2003
  5. Replies:
    5
    Views:
    1,580
Loading...

Share This Page