Time and Date wrapper class

Started by
6 comments, last by Taha Ansari 15 years, 5 months ago
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...
Advertisement
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?

Psuedocode (naturally):
a = DateDiff(Now, "January 1, 1970")
b = DateDiff("January 6, 1980", "January 1, 1970")
SecondsSince_Jan_6_1980 = a - b
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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.
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
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!
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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!

This topic is closed to new replies.

Advertisement