Getting minutes passed from two times
#1

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 TimeSecond TimeMinutes Passed
20:45 PM20:50 PM5
20:45 PM20:55 PM10
Thanks!
Reply
#2

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.
Reply
#3

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?

pawn Код:
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
}
Reply
#4

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.
Reply
#5

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.

pawn Код:
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

pawn Код:
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

pawn Код:
Minutes += Hours * 60;
return Minutes;
Hope this helps
Reply
#6

pawn Код:
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)