PDA

View Full Version : Getting minutes passed from two times


Luis-
10/05/2013, 12:25 AM
Hey! I was planning to add something regarding uptime in my server, but I have no idea how to make it seeing as I have no understanding of the math functions.

Basically i'd like a stock function (doesn't matter if it's not!) that gets the amount of minutes passed from one time and another time, here's an example;



First Time
Second Time
Minutes Passed


20:45 PM
20:50 PM
5
[tr]
20:45 PM
20:55 PM
10



Thanks!

Luis-
10/05/2013, 12:34 AM
Luckily I am using timestamps for getting the servers uptime! Would I divide both times or something? Wish I listened in my math classes now.

Knappen
10/05/2013, 12:44 AM
If you're thinking about uptime for your server, couldn't you just create a timer and a global variable? And then save it to a file or the database?

Something like this?


new ServerUpTime;
// new ServerUpTimeID;
public OnGameModeInit()
{
// Get the last ID if you want to log every time the server has been running
ServerUpTime = 0;
SetTimer("UpdateServerUptime", 300000, true);
return 1;
}
stock UpdateServerTime(/*ID*/);
{
ServerUpTime += 5;
// Save it, and assign it with a ID if you want to log it for everytime the server runs
}

Luis-
10/05/2013, 12:47 AM
I've already done that, in fact that was probably one of the first things I did when I started coding the script. If you read the post, you'd of known I wasn't asking for that.

Knappen
10/05/2013, 01:03 AM
Okay, sorry about that then!

How about this then?

If you start the server at 19:00, and it runs till 20:55, it would be something like this

20 - 19 = 1 hour

55 - 00 = 55 minutes

So it's basically 20:55 - 19:00, but both hours and minutes divided into 2.


stock CalculateUpTime()
{
new TimeString[2][6], ResultString[2], ResultString2[2], FinalResult[6];
/* Load the string, say the server started at 16:43 and ran until 19:48 */
split(TimeString[1], ResultString, ':'); // 16:43
split(TimeString[2], ResultString2, ':');
Hours = ResultString2[1] - ResultString[1];
Minutes = ResultString2[2] - ResultString[2];
format(FinalResult, sizeof(FinalResult), "%d:%d", Hours, Minutes);
return FinalResult;
}


or you could pass the time on from another function


stock CalculateUpTime(TimeString[], TimeString2[])
{
new ResultString[2], ResultString2[2], FinalResult[6];
/* Load the string, say the server started at 16:43 and ran until 19:48 */
split(TimeString, ResultString, ':'); // 16:43
split(TimeString2, ResultString2, ':');
Hours = ResultString2[1] - ResultString[1];
Minutes = ResultString2[2] - ResultString[2];
format(FinalResult, sizeof(FinalResult), "%d:%d", Hours, Minutes);
return FinalResult;
}


How about this? Not tested, but you get the basic idea, right?

EDIT: Remade them to stocks, only stocks can return strings. Sorry about that.

Also, if you want it in mintues only, you could just finish like this


Minutes += Hours * 60;
return Minutes;


Hope this helps

iggy1
10/05/2013, 07:11 AM
new iStartTime=0;
iStartTime = gettime();

//now to get time passed since the above was called...

new iTimePassed = ( gettime()-iStartTime );//will return seconds passed since iStartTime was assigned "gettime"

Obviously divide 'iTimePassed' by 60 to get minutes passed.

Beats using timers just for counting time.