Solved

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

Posted on 2000-02-15
11
194 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
 
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
Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Does Objective C commented code increase filesize in an installed iPhone app? 7 136
pgpool-II on Ubuntu 14.04... ARGH! 5 843
why "." vs "->" 23 119
How to learn Linux? 10 43
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…
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…
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 for-loops in the C programming language.

910 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

19 Experts available now in Live!

Get 1:1 Help Now