Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more


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

Posted on 2000-02-15
Medium Priority
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;
            st = localtime(&tnow);
            strftime(timestr, 64, "%d %b %y  %H%MZ", st);
How can I modify this to get next week's time?
Question by:bleek
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
LVL 32

Expert Comment

ID: 2522752
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().
LVL 32

Expert Comment

ID: 2522756
>>>(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....;-)

Author Comment

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

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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 86

Expert Comment

ID: 2522976
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 ) );
      perror( "mktime failed" );

LVL 32

Expert Comment

ID: 2523050
>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.

Expert Comment

ID: 2523103
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.

Author Comment

ID: 2523117
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.

Expert Comment

ID: 2523135
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."


Author Comment

ID: 2523138
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.

Accepted Solution

3rsrichard earned 400 total points
ID: 2523235
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);
tlater += ( (long)7 * 24 * 60 * 60);

It could be your compiler doing the rounding.

Author Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

647 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