Link to home
Start Free TrialLog in
Avatar of bleek
bleekFlag for United States of America

asked on

How do I modify results of function time to get next week's time?

I am trying to adjust the results of a call to time so that I can get the time for a week from now.  the code I'm using to get current time fot display is:

            time_t tnow;
            char timestr[64];
            struct tm *st;
            time(&tnow);
            st = localtime(&tnow);
            strftime(timestr, 64, "%d %b %y  %H%MZ", st);
How can I modify this to get next week's time?
Avatar of jhance
jhance

time() give you the number of seconds since 1/1/70.  Just add the number of seconds in a week (which is always 7 * 24 * 60 * 60) to that value before you pass it to localtime().
>>>(which is always 7 * 24 * 60 * 60)

Actually, I think I'll take that back.  I've been through some weeks that were a LOT longer....;-)
Avatar of bleek

ASKER

I already tried that by modifying the code to:
time_t tlater;
char timestr2[64];
struct tm *st2;
time(&tlater);
tlater += ( 7 * 24 * 60 * 60);
st2 = localtime(&tlater);
strftime(timestr2, 64, "%d %b %y  %H%MZ", st2);
strupr(timestr2);
weektime.SetFormatText(timestr2);

When I print it out, the time is only 5 hours later. (like you, I've worked weeks like that);

I'm working with a subset of "C" provided by the authoring package Quest.
Avatar of jkr
What about somthing like this:

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

void main( void )
{
   struct tm when;
   time_t now, result;
   int    days;

   time( &now );
   when = *localtime( &now );
   printf( "Current time is %s\n", asctime( &when ) );
   printf( "How many days to look ahead: " );
   scanf( "%d", &days );

   when.tm_mday = when.tm_mday + days;
   if( (result = mktime( &when )) != (time_t)-1 )
      printf( "In %d days the time will be %s\n",
              days, asctime( &when ) );
   else
      perror( "mktime failed" );
}

?
>I'm working with a subset of "C" >provided by the authoring package >Quest.

Then you're probably running into a 16/32 bit truncation issue.  7 * 24 * 60 * 60 is too large to fit in a 16 bit.  The total is 0x00093A80.  If you toss the upper 16 bits, you get 0x3A80 = 14976.  That's a bit over 4 hours.

Could this be what's happening?  I'm not familiar with the Quest package but you might read up on the 16 vs. 32 bit arithmetic.
MS has changed the way they store time
(at least thats what they claim in MSDN).
It appears that it is now supposed to be a 32 bit integer, and that the days are in the small end of the upper 16 bits.
So, you might try adding
7 * 65536 = 458752 to your current time_t.
Avatar of bleek

ASKER

To jhance:  Thank you for your last comment.  I bet that's what happened.

To jkr:  The problem with that is it doen't account for days greater that that allowed for a particular month and you get dates like Feb. 34.  I had already tried that, but I'd like to eliminate the need for the large case statement to catch day overflows,  month and year changes.  That is why I was trying to modify the output from time.  Thank you for the suggestion.
I decided that makes no sense even if it is what the helpfiles say.
They say elsewhere
"The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored."

Avatar of bleek

ASKER

To 3rsrichard:  TheQuest  package is 16 bits and I don't think they're using the new time function.  Their "C" compliler complains that 458752 is a long and won't run it.
ASKER CERTIFIED SOLUTION
Avatar of 3rsrichard
3rsrichard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bleek

ASKER

To3rsrichard &  all that tried to help, thank you very much.