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
Solved

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

Posted on 2000-02-15
11
196 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
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().
0
 
LVL 32

Expert Comment

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

Author Comment

by:bleek
ID: 2522811
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 86

Expert Comment

by:jkr
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 ) );
   else
      perror( "mktime failed" );
}

?
0
 
LVL 32

Expert Comment

by:jhance
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.
0
 
LVL 3

Expert Comment

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

Author Comment

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

Expert Comment

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

0
 

Author Comment

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

Accepted Solution

by:
3rsrichard earned 100 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);
or
tlater += ( (long)7 * 24 * 60 * 60);


It could be your compiler doing the rounding.
0
 

Author Comment

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

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

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…
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 recursion in the C programming language.
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.

808 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