Solved

Issues with time.h

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 conditional statements in the C programming language.

820 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