Solved

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

Posted on 2000-02-15
11
193 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:bleek
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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().
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
>>>(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....;-)
0
 

Author Comment

by:bleek
Comment Utility
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.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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" );
}

?
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
>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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
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.
0
 

Author Comment

by:bleek
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
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."

0
 

Author Comment

by:bleek
Comment Utility
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.
0
 
LVL 3

Accepted Solution

by:
3rsrichard earned 100 total points
Comment Utility
Even if Quest is 16 bits, it should have at least a 32 bit value for time_t.
Otherwise the clock would roll over in less than a day.
Did you try something like

tlater += (long)( 7 * 24 * 60 * 60);
or
tlater += ( (long)7 * 24 * 60 * 60);


It could be your compiler doing the rounding.
0
 

Author Comment

by:bleek
Comment Utility
To3rsrichard &  all that tried to help, thank you very much.  
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

763 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

7 Experts available now in Live!

Get 1:1 Help Now