Sign in to follow this  
Taha Ansari

Time and Date wrapper class

Recommended Posts

Greetings! After looking at different forums topics, I finally decided to post my query here: Thing is, I want to calculate total number of seconds that have elapsed since january 6, 1980. I've heard that Java offers this functionality some where, and certainly in case of BREW, I've used a function to determine the same [GETTIMESECONDS()]. Because it is some how my requirement to do same on windows mobile platform, I dont seem to find any function that does this - all I was able to find in MSDN is some TimetToFileTime( time_t t, LPFILETIME pft ) function, and that also returns total number of seconds elapsed since January 1, 1970, and not January 6, 1980, which is my requirement. While trying to google for some time wrapper classes, I havn't found any thing open source yet. MFC gives me CTime class along with arithmetic overloads, but its of no use here... Can any one guide me towards some easy solution before I find it in-evitable to implement my own function? Thanks in advance...

Share this post


Link to post
Share on other sites
Quote:
Original post by Taha Ansari

Because it is some how my requirement to do same on windows mobile platform, I dont seem to find any function that does this - all I was able to find in MSDN is some TimetToFileTime( time_t t, LPFILETIME pft ) function, and that also returns total number of seconds elapsed since January 1, 1970, and not January 6, 1980, which is my requirement.


I'm pretty sure the difference between those two dates is constant. Why not just add or subtract that constant to convert from one representation to another?

Share this post


Link to post
Share on other sites
Quote:
quote by Antheus
I'm pretty sure the difference between those two dates is constant. Why not just add or subtract that constant to convert from one representation to another?

Thats very correct - I seem to have lost that reference else I'd have posted the function's internal code as given by MSDN (and I'm too lazy to find that again since initial search didn't show up anything useful), but the constant is the problem. Naturally, this constant value will be different for Jan 6, 1980, so, how will I find it?

Quote:
quote by iMalc
Psuedocode (naturally):
a = DateDiff(Now, "January 1, 1970")
b = DateDiff("January 6, 1980", "January 1, 1970")
SecondsSince_Jan_6_1980 = a - b


That's the very first temptation I got and it doesn't look very complex either, until I had to consider the leap year factor - so I stopped here.

Share this post


Link to post
Share on other sites
write a java prog that constantly outputs the time since 1970, and also posts the current time

do the same on windows or something where it poops out 1980, and the current time

then find a matching current time and take the difference

Share this post


Link to post
Share on other sites
Thanks for providing me with possible solutions - I've been able to find the answer. A little closer look into MFC/ATL code, and I got clues how to find the time difference, and with that in hand I can find the exact number of seconds elapsed since Jan 6, 1980.

Thanks again!

Share this post


Link to post
Share on other sites
Quote:
Original post by Taha Ansari
Quote:
quote by iMalc
Psuedocode (naturally):
a = DateDiff(Now, "January 1, 1970")
b = DateDiff("January 6, 1980", "January 1, 1970")
SecondsSince_Jan_6_1980 = a - b


That's the very first temptation I got and it doesn't look very complex either, until I had to consider the leap year factor - so I stopped here.
But you don't have to consider any leap year factor or anything else. You only need something to convert the time into some arbitrary number of elapsed seconds. Sounds to me like TimetToFileTime will do it for you just fine, that would already take care of everything perfectly.

Share this post


Link to post
Share on other sites
Quote:
Quote by iMalc
But you don't have to consider any leap year factor or anything else. You only need something to convert the time into some arbitrary number of elapsed seconds. Sounds to me like TimetToFileTime will do it for you just fine, that would already take care of everything perfectly.


Agreed! And that is exactly the approach that has been adopted by ATL CTime and CTimeSpan classes, though, in one view, they appear to be hiding some thing in their implementation - they calculate time differences in their overloaded operators: they find elapsed seconds for time1, and time2, and subtract them. Result is in seconds, and they can do any basic manipulation with this data to extract total number of days, etc...

So, I also adopted similar approach and got results perfectly...

Thanks for sharing!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this