Need funtion for Time
#1

i need a function that convert seconds into other style

EXAMPLE:

61 seconds into 1:1

EDIT: nvm i found it :/
Reply
#2

pawn Код:
forward Float:SecondsToMinutes(seconds);
public Float:SecondsToMinutes(seconds)
    return ( seconds / 60 );
Like this?
Reply
#3

pawn Код:
forward Float:SecondsToMinutes(seconds);
public Float:SecondsToMinutes(seconds)
    return ( seconds / 60 );
   
//under your command/public/stock
    new minutes, diff;
    minutes = SecondsToMinutes(seconds);
    diff = seconds*60;
    seconds = diff - minutes
    new string[128];
    format(string, 128, "Those are %i minutes, and %i seconds.",minutes,seconds);
//:D
Reply
#4

no not like this!

i want like convert

Like this

pawn Код:
new TimeLeft = 500;

stock ConvertSecondsIntoMinute(seconds)
{
   //Codes here
   format(string,128,"%d:%d",minute,seconds);
   return string;
}

new string[128];
format(string,128,"%s",ConvertSecondsIntoMinute(TimeLeft));
TextDrawSetString(tTimeLeft,string);
if you get this :3
Reply
#5

Simple enough:
pawn Код:
ConvertSecondsToMinutes(seconds)
{
    new
        minutes,
        output[ 8 ]; //8 cells. This allows the maximum output to be XXXX:XX - I don't think you will exceed 9999 minutes and 59 seconds.

    while( seconds > 60 )
    {
        minutes ++;
        seconds -= 60;
    }
   
    format( output, sizeof( output ), "%02i:%02i", minutes, seconds );
    return output;
}
For example the input
pawn Код:
print( ConvertSecondsToMinutes( 323 ));
would return
Код:
05:23
Reply
#6

I wouldn't go with the "while" loop. It's uber slow. Don't know if it's a side-efect of using a for-loop with while. But I benchmarked your code and my own simplest version without loops and results were shocking.
Код:
[20:22:13] Time taken to execute My SecToMS: 1066ms
[20:22:45] Time taken to execute LarzI's SecToMS: 31218ms
The code:
pawn Код:
#define FILTERSCRIPT
#include <a_samp>

public OnFilterScriptInit() {
    new f_Tick, l_Tick;
    f_Tick = GetTickCount();
    for (new d; d < 1000000; d++)
        SecondsToMinutesSeconds(54684);
    l_Tick = GetTickCount() - f_Tick;
    printf("Time taken to execute my SecToMS: %dms", l_Tick);
    f_Tick=0; l_Tick=0;
   
    f_Tick = GetTickCount();
    for (new d; d < 1000000; d++)
        ConvertSecondsToMinutes(54684);
    l_Tick = GetTickCount() - f_Tick;
    printf("Time taken to execute LarzI's SecToMS: %dms", l_Tick);
    f_Tick=0; l_Tick=0;
    return 1;
}

stock SecondsToMinutesSeconds(seconds)
{
    new mins, sec;
    mins = seconds/60; // Divide by 60 to get minutes
    sec = seconds%60; // Modulo 60 gets the remainder of a division, meaning the left-over seconds.
    new string[12];
    format(string, 12, "%d:%02d", mins, sec);
    return string;
}

stock ConvertSecondsToMinutes(seconds)
{
    new
        minutes,
        output[ 8 ]; //8 cells. This allows the maximum output to be XXXX:XX - I don't think you will exceed 9999 minutes and 59 seconds.

    while( seconds > 60 )
    {
        minutes ++;
        seconds -= 60;
    }
   
    format( output, sizeof( output ), "%02i:%02i", minutes, seconds );
    return output;
}
Reply
#7

Quote:
Originally Posted by Virtual1ty
Посмотреть сообщение
I wouldn't go with the "while" loop. It's uber slow. Don't know if it's a side-efect of using a for-loop with while. But I benchmarked your code and my own simplest version without loops and results were shocking.
Код:
[20:22:13] Time taken to execute My SecToMS: 1066ms
[20:22:45] Time taken to execute LarzI's SecToMS: 31218ms
The code:
pawn Код:
//code
Ah yes, I thought about using modulo after posting my code, but I couldn't be arsed to edit.
Reply
#8

Haha! Wanted to use modulo too but for some reason my brain stopped working, thank you Virtual1ty! You just refreshed it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)