Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2007

Tip: Looking for answers? Try searching our database.

Display a time span in days

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank - 17 Jan 2007 08:08 GMT
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
Sourcerer - 17 Jan 2007 08:18 GMT
>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.

Signature

"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/

Frank - 17 Jan 2007 13:36 GMT
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

>>I have a time saved in a time_t variable.
>>
[quoted text clipped - 7 lines]
>> Failing that I'd like to display the date (only the date) that
>> corresponds to the saved time_t variable.
Ben Voigt - 17 Jan 2007 14:25 GMT
> Thanks
>
[quoted text clipped - 4 lines]
> 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.

> Thanks a lot
>
[quoted text clipped - 9 lines]
>>> Failing that I'd like to display the date (only the date) that
>>> corresponds to the saved time_t variable.
Frank - 17 Jan 2007 14:35 GMT
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

>> Thanks
>>
[quoted text clipped - 21 lines]
>>>> Failing that I'd like to display the date (only the date) that
>>>> corresponds to the saved time_t variable.
Ben Voigt - 17 Jan 2007 14:52 GMT
> 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.

> thanks
>
[quoted text clipped - 23 lines]
>>>>> Failing that I'd like to display the date (only the date) that
>>>>> corresponds to the saved time_t variable.
Carl Daniel [VC++ MVP] - 17 Jan 2007 15:28 GMT
> 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
Frank - 17 Jan 2007 16:54 GMT
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;
Ben Voigt - 17 Jan 2007 17:13 GMT
> 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?
>
[quoted text clipped - 7 lines]
>
> szTmpStr[lstrlen(szTmpStr)-1] = 0;
Frank - 17 Jan 2007 17:34 GMT
ouch!

thanks

>> Thanks for all the insight.
>>
[quoted text clipped - 20 lines]
>>
>> szTmpStr[lstrlen(szTmpStr)-1] = 0;

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.