Display a time span in days

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

I have a time saved in a time_t variable.

I'd like to be able to display the number of days since then but can't find
anything built-in that will help.

Failing that I'd like to display the date (only the date) that corresponds
to the saved time_t variable.


Thanks for any help
 
Frank said:
I have a time saved in a time_t variable.

I'd like to be able to display the number of days since then but can't find
anything built-in that will help.

I believe dividing the number by (60*60*24) might give you the number of days
(depends on what exactly you have in that variable). Be careful about how the
result is rounded, though.
Failing that I'd like to display the date (only the date) that corresponds to
the saved time_t variable.

--
"It is easy in the world to live after the world's opinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
Thanks

Actually I thought of that after I posted. That question was an after
thought - without too much thought.

The main reason for posting is that I wanted to display a saved date,
without the time, and it seems there must be an easier way that what I was
doing.



Thanks a lot
 
Frank said:
Thanks

Actually I thought of that after I posted. That question was an after
thought - without too much thought.

The main reason for posting is that I wanted to display a saved date,
without the time, and it seems there must be an easier way that what I was
doing.

First gmtime or localtime to get a struct tm, then strftime with a format
string of your choice.
 
I'll look at that. I did tried tm once but if I remember I got 2 for example
rather than March.
Maybe strftime was what I missed.

In your prior post you cautioned about rounding. Thanks for that. I got 0
when I expected 1 until I remember your caution.


thanks
 
Frank said:
I'll look at that. I did tried tm once but if I remember I got 2 for
example rather than March.
Maybe strftime was what I missed.

In your prior post you cautioned about rounding. Thanks for that. I got 0
when I expected 1 until I remember your caution.

Wasn't my post actually, but...

Define what you mean by "number of days since then". If the saved time is
11:59 PM on Feb 1, and current system time is 12:03 AM on Feb 2, the delay
is only 4 minutes, but midnight passed... so do you count that as a day?
Rounding won't really help you there.

If you mean calendar days, then you may want to convert your time_t to a tm
using localtime, zero the hour/minute/second fields, use mkgmtime to go back
to time_t, then difftime and divide by (24*60*60). In that way you will
count days elapsed since the beginning of the day in which the saved time
occurred.
 
Ben said:
If you mean calendar days, then you may want to convert your time_t
to a tm using localtime, zero the hour/minute/second fields, use
mkgmtime to go back to time_t, then difftime and divide by
(24*60*60). In that way you will count days elapsed since the
beginning of the day in which the saved time occurred.

or just do the division first, then the subtraction:

int num_days = (end_time_t / 86400) - (start_time_t/86400);

Will always give the difference in day number between two time_t values. If
you want the "rollover" from one day to the next to be at a time other than
midnight (e.g. to counts days in a different timezone than the one
represented in your time_t), then just subtract the appropriate number of
hours*3600 from each time_t before dividing.

-cd
 
Thanks for all the insight.

I'm showing the days since and also the prior date.

I got two extra characters displayed so I added the last line shown below.

Can you tell me what is going on?



Thanks again



struct tm pasttime;

localtime_s( &pasttime, &OldTime );

strftime( szTmpStr, 100, "%A %B %d, %Y\n", &pasttime );

szTmpStr[lstrlen(szTmpStr)-1] = 0;
 
Frank said:
Thanks for all the insight.

I'm showing the days since and also the prior date.

I got two extra characters displayed so I added the last line shown below.

That extra line removes the newline character you have in the format string.
Perhaps it's also going through newline translation and becoming a CR/LF
pair.

Try just removing the "\n" from the format string.
Can you tell me what is going on?



Thanks again



struct tm pasttime;

localtime_s( &pasttime, &OldTime );

strftime( szTmpStr, 100, "%A %B %d, %Y\n", &pasttime );

szTmpStr[lstrlen(szTmpStr)-1] = 0;
 
ouch!

thanks

Ben Voigt said:
Frank said:
Thanks for all the insight.

I'm showing the days since and also the prior date.

I got two extra characters displayed so I added the last line shown
below.

That extra line removes the newline character you have in the format
string. Perhaps it's also going through newline translation and becoming a
CR/LF pair.

Try just removing the "\n" from the format string.
Can you tell me what is going on?



Thanks again



struct tm pasttime;

localtime_s( &pasttime, &OldTime );

strftime( szTmpStr, 100, "%A %B %d, %Y\n", &pasttime );

szTmpStr[lstrlen(szTmpStr)-1] = 0;
 
Back
Top