Jump to content
• ### What is your GameDev Story?

• Advertisement

# Time and Date wrapper class

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## 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

##### Share on other sites
Advertisement
Quote:
 Original post by Taha AnsariBecause 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

##### Share on other sites
Psuedocode (naturally):
a = DateDiff(Now, "January 1, 1970")
b = DateDiff("January 6, 1980", "January 1, 1970")
SecondsSince_Jan_6_1980 = a - b

#### Share this post

##### Share on other sites
Quote:
 quote by AntheusI'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 iMalcPsuedocode (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

##### 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

##### 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

##### Share on other sites
Quote:
Original post by Taha Ansari
Quote:
 quote by iMalcPsuedocode (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

##### Share on other sites
Quote:
 Quote by iMalcBut 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

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 28
• 16
• 10
• 10
• 11
• Advertisement
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015573
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!