Time conversion
#1

Hey, i've made a little function for an RP server i've developing, which converts minutes into days, hours & minutes. My problem is that when the minute mark reaches 60+ with the convert minute function, it just carries on, I was wondering if I could stop that and reset it to 0 without disrupting the days & hours process.

Sorry, if that was hard to understand.

Код:
stock ConvertTime(time) {
	new String[300];

	new day = time / 1440;
	new hour = time / 60;
	new minute = time;

	format(String, sizeof String, "{FF0000}%d {FFFFFF}days, {FF0000}%d {FFFFFF}hours and {FF0000}%d {FFFFFF}minutes", day, hour, minute);
    return String;
}
Reply
#2

I'm not sure. My Div Function is good, use it. It will return you how many times is b in a, without decimal places.

pawn Код:
stock Div(a,b)
{
    new count = 0;
    if(a < b) return 0;
    while((count*b) < a)
    {
        count++;
    }
    count--;
    return count;
}

stock ConvertTime(time)
{
    new String[300];

    new day = Div(time,24*60);
    new hour = Div(time-(24*60*days),60);
    new minute = time - (days*60*24)-(hour*60);

    format(String, sizeof String, "{FF0000}%d {FFFFFF}days, {FF0000}%d {FFFFFF}hours and {FF0000}%d {FFFFFF}minutes", day, hour, minute);
    return String;
}
Reply
#3

Is it accurate though? I've gone from having this 0 days, 1 hour & 83 minutes to 0 days, 1 hours & 32 minutes
Reply
#4

Which value you give in start? Take example value and then just track code and fix it. Mine is untested.
Reply
#5

Quote:
Originally Posted by [MG]Dimi
Посмотреть сообщение
I'm not sure. My Div Function is good, use it. It will return you how many times is b in a, without decimal places.
That is pretty much what the modulus operator does, which is native in Pawn. It returns the remainder of the division.

pawn Код:
new temp = time % (24 * 60);
new days = (time - temp) / (24 * 60);
new minutes = temp % 60;
new hours = (temp - minutes) / 60;
Say, 2000 minutes:
Код:
time 2000
temp = 2000 % 1440 = 560
days = (2000 - 560) / (24 * 60) = 1440 / 1440 = 1
minutes = 560 % 60 =  20
hours = (560 - 20) / 60 = 540 / 60 = 9
1 day, 9 hours, 20 minutes.

Do not change the order of the variables. That's important.
Reply
#6

Right, so 143 minutes would equal 0 days, 2 hours & 23 minutes right? Sorry, I actually don't understand all this, gonna have to learn.
Reply
#7

If you did those long divisions (or whatever you call them) far back in primary school then you should know what a remainder is.

Basically, if I was to divide 8 by 3 I would get a remainder of 2, because the highest whole number that can be divided by 3 is 6. (and 6 + 2 = 8)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)