Skip to main content
GameDev.net gamedev.net
🔒 Locked

Total hours spent

Started by ursus Dec 9, 2006 at 7:07 AM 11 replies 1.8k views
Original Post
ursus
ursus
Hello All A simple question I believe. I need to calculate time spent on some activity. Let's say the activity started at 2 o'clock PM and finished at 4 o'clock PM. This case is easy: total_time = end_time start_time 4-2=2 That formula doesn't work for an example where the activity started at 10 o'clock AM and finished at 2 PM 2-10=-8 (rubish!) I remember this is some basic exercise we had at the university but I just cannot recall the solution. I'd greatly appreciate any hints. Many thanks
ApochPiQ
ApochPiQ
Most languages have a library function for this. More details about what tools you're working with and what libraries you have available would be very helpful.
Servant of the Lord
Servant of the Lord
Quote:
Original post by Anonymous Poster
use 24 hour time.

Anonymous posters cannot post hyperlinks. You can post URLs however. (If that is what you're complaining about)

Quote:
OP
That formula doesn't work for an example where the activity started at 10 o'clock AM and finished at 2 PM

2-10=-8 (rubish!)


24 hour time is indeed the right way to go.
14 - 10 = 4 hours.
ursus
ursus
Guys,

Thank you very much for your replies.

First of all, please accept my apologies for placing this post on this forum. It was supposed to be in Math and Physics section. I put it here by mistake. Moderators, would you be so kind to move it to the appropriate forum?

Secondly, using 24 hours clock will solve the problem to some extent only as we can have the case when the activity starts at 22:00 and finished at 2:00

Many thanks
Hylo
Hylo
You could try adding 24 to the finish time if it is less than the start time.

22:00 to 2:00
26 - 22 = 4 hours.


Andrew.
tetrisfrog
tetrisfrog
What language are you writing in? I'll assume C, but most languages have some sort of time interface that is the same. The easiest way to do it (accurate to 1 second), is to get the timestamp of when they started, and then the timestamp of when the finished. The timestamp is a standard UNIX timestamp measured in seconds since the epoch (start), which is usually since January 1, 1970 at 12:00 midnight. [http://en.wikipedia.org/wiki/Unix_time]

You would do something like this:
#include <time.h>    // make sure to include this...your_function() {   long begin_time;  // vars for our timestamps   long end_time;   long diff_time;   int hours_played; // vars for our play time   int mins_played;   int secs_played;   begin_time = time(NULL);   // play a game or do something useful here....   end_time = time(NULL);   diff_time = end_time - begin_time;  // this calculates the "time played" in                                       // seconds   // so to get this into something useful other than seconds...   // 1 hour = 3600 seconds, 1 minute = 60 seconds, etc.   hours_played = diff_time / 3600;  // note that this is *integer* division.                                     // that means no matter how close that                                     // we are to the next number in terms of                                     // rounding, it will always round down                                     // ( and to our advantage ^_~ )   diff_time -= hours_played * 3600; // subtract the hours since we already                                     // calculated them. this should give us                                     // < 1 hour left (eg. minutes&seconds)   mins_played = diff_time / 60;     // calculate the minutes   diff_time -= mins_played * 60;    // subtract... etc.   secs_played = diff_time;          // the remainder should be < 60, and                                     // is the number of seconds =]   printf("You've been playing for %i hours, %i minutes, and %i seconds!\n",          hours_played, mins_played, secs_played);}

This is the way i do it in C, PHP, and Perl at least, and should work the same way in any other language... most popular languages have the same basic time functions with the same syntax. ^_~
Hope this helps. Good luck.
ursus
ursus
Thank you all guys!

I think Andrew's suggestion hits the spot
SilencedViolence
SilencedViolence
You probably would want to tell us what form the "time" is in. Is it a specific date time thing as 10:57:31.00 AM March 31 2007 or is it a relative thing, as in 10:57:31.00 PM, +1 (where the +1 component may indicate in how many days, for example 1 (or +1 as i wrote it) is tomorrow).

If its the second case, if we label some arbitary start time as t1 with components x1 y1 z1 (like c1 = 11:33:44.00, y1 = AM, z1 = +3) and another time as t2 with components x2 y2 z2 you could calculate the difference using this kind of function:

dt = (x2 - x1) + (helper(y2) - helper(y1)) + (24 hours)*(z2-z1)

where helper is a function that returns (12 hours) if its input is "PM" and (0 hours) if its input is "AM" (you can use a boolean value for an imput, for example true can be PM and false AM or something). dt is the difference in time you are looking for... it can be in any format you want depending on how you define subtraction and addition (ill explain).

That should pretty much cover it. Of course the subtraction, addition, etc. here are not simple operations, since mathematically 11:33:44.00 - 10:57:31.00 is pretty meaningless, instead they are "overloaded" operators, basically you're redifining subtraction and addition so they know how to subtract and add times (for example the operations must know there are 60 seconds in a minute, 60 minutes in an hour, etc.)

Ok so heres a programming way to implement it (i havent written code in a while so this might be poorly written, and ive also forgotten how to add codeblocks so feel free to sue me):

struct exampleTime
{float milliseconds;
unsigned int seconds,minutes, hours;
//the three variables above comprise x1,x2,etc.
bool PM; //////this would be y1, y2, etc.
int days; //////this would be z1, z2, etc.
};

Then you define the helper function, or whatever yu want to call it. You can either make it accept a "exampleTime" type input or a "boolean" input. I'll implement the boolean version here (i dont remember how to write functions anymore, so this might be messed up:)

exampleTime helper(bool isitPM)
{
exampleTime temporary;
////basically you define a variable of type exampleTime that you're gonna return
//here you'd initialize the members of "temporary" to 0 or whatever (except for the hour). A far better way would be to write a constructor for the structure exampleTime or make it a class. Whatever.

if (isitPM)
temporary.hours = 12;
else
temporary.hours = 0;

return temporary;
//finally you return the "temporary" exampleTime structure you made.
)

There are far better ways to implement this... but this is the basic idea of how you'd go about doing this. After this you'd probably go on to overload the + and - operators, as well as * for the function i described to work. You can figure this out on your own

Or if you want to keep it simple you could juse use 24 hour time, in which case there would be no need for the y part of the equation or for a helper function.

Wow this answer was so elaborate... I hope I covered most things. Btw the code is supposed to be in C++... I hope I got it right.

Oh and I didnt notice tetrisfrog's reply while I was composing this... but I think I used a different idea for it. Whatever, hope I helped.
~ SilencedViolence

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.