Solved

Issues with time.h

Posted on 2006-07-17
4
387 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now