Solved

Issues with time.h

Posted on 2006-07-17
4
394 Views
Last Modified: 2012-05-05
Recently I posted a question regarding a problem Im having with time.h....

What I'm trying to to do is take 2 dates from user input, and do a difftime on them.

I've checked out the mktime, time & difftime on the link below

http://www.cplusplus.com/ref/ctime/difftime.html

I can get each of these functions working for one "time" but not 2. My function is below:

void ageCalculator()
      {
      time_t rawtime, rawtime2;
      struct tm * timeinfo;
      struct tm * timeinfo2;
      int year, month ,day;

      /* prompt user for date */
      printf ("Enter year: "); scanf ("%d",&year);
      printf ("Enter month: "); scanf ("%d",&month);
      printf ("Enter day: "); scanf ("%d",&day);

      /* get current timeinfo and modify it to user's choice */
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      timeinfo->tm_year = year - 1900;
      timeinfo->tm_mon = month - 1;
      timeinfo->tm_mday = day;

      mktime (timeinfo);

      
      int year2, month2 ,day2;

      /* prompt user for date */
      printf ("Enter year: "); scanf ("%d",&year2);
      printf ("Enter month: "); scanf ("%d",&month2);
      printf ("Enter day: "); scanf ("%d",&day2);

      /* get current timeinfo and modify it to user's choice */
      time ( &rawtime2 );
      timeinfo2 = localtime ( &rawtime2 );
      timeinfo2->tm_year = year - 1900;
      timeinfo2->tm_mon = month - 1;
      timeinfo2->tm_mday = day;

      mktime (timeinfo2);

      double seconds;
      seconds = difftime(mktime(timeinfo),mktime(timeinfo2));
      printf("Seconds = %f", seconds);
      }


I've tried to adapt it from the reference above.

Any thoughts?


0
Comment
Question by:Benjamin_Barrett
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17123904
Is it intentional that you are not using year2, month2 and day2 when calculationg the info for the 2nd time? IMO that should be

     int year2, month2 ,day2;

     /* prompt user for date */
     printf ("Enter year: "); scanf ("%d",&year2);
     printf ("Enter month: "); scanf ("%d",&month2);
     printf ("Enter day: "); scanf ("%d",&day2);

     /* get current timeinfo and modify it to user's choice */
     time ( &rawtime2 );
     timeinfo2 = localtime ( &rawtime2 );
     timeinfo2->tm_year = year2 - 1900; // <---!
     timeinfo2->tm_mon = month2 - 1;  // <---!
     timeinfo2->tm_mday = day2;  // <---!

     mktime (timeinfo2);

     double seconds;
     seconds = difftime(mktime(timeinfo),mktime(timeinfo2));
     printf("Seconds = %f", seconds);
0
 

Author Comment

by:Benjamin_Barrett
ID: 17124293
Ugh yeah, well spotted jkr.

Unfortunately it still doesnt work, the ouput is still
Seconds = 0.000000
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17124683
'localtime()' uses a single static buffer, so youll have to call 'mktime()' and save the value before calling 'localtime()' again. Try

void ageCalculator()
     {
     time_t rawtime, rawtime2;
     struct tm * timeinfo;
     struct tm * timeinfo2;
     int year, month ,day;
     time_t t1, t2;

     /* prompt user for date */
     printf ("Enter year: "); scanf ("%d",&year);
     printf ("Enter month: "); scanf ("%d",&month);
     printf ("Enter day: "); scanf ("%d",&day);

     /* get current timeinfo and modify it to user's choice */
     time ( &rawtime );
     timeinfo = localtime ( &rawtime );
     timeinfo->tm_year = year - 1900;
     timeinfo->tm_mon = month - 1;
     timeinfo->tm_mday = day;

     t1 = mktime (timeinfo);

     int year2, month2 ,day2;

     /* prompt user for date */
     printf ("Enter year: "); scanf ("%d",&year2);
     printf ("Enter month: "); scanf ("%d",&month2);
     printf ("Enter day: "); scanf ("%d",&day2);

     /* get current timeinfo and modify it to user's choice */
     time ( &rawtime2 );
     timeinfo2 = localtime ( &rawtime2 );
     timeinfo2->tm_year = year2 - 1900;
     timeinfo2->tm_mon = month2 - 1;
     timeinfo2->tm_mday = day2;

     t2 = mktime (timeinfo2);

     printf("%d vs. %d\n",t1, t2);

     double seconds;
     seconds = difftime(t1,t2);
     printf("Seconds = %f", seconds);
     }

That works at least for me ;o)
0
 

Author Comment

by:Benjamin_Barrett
ID: 17124816
You da man!!

Your explanantion re localtime() makes perfect sense too

Thanks heaps
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question