PDA

View Full Version : value to clock format?


Lirbo
08/07/2016, 05:46 PM
Let's say I've a number with the value of 600, how I can make a string that would show it as 10:00? (600 seconds = 10 minutes)

Dusan01
08/07/2016, 05:47 PM
here u go a function: stock ConvertTime(vreme)
{
new minuti, sekunde, string[128];
if(vreme > 59)
{
minuti = floatround(vreme/60);
sekunde = floatround(vreme - minuti*60);
if(sekunde > 9) format(string, sizeof(string), "%d:%d", minuti, sekunde);
else format(string, sizeof(string), "%d:0%d", minuti, sekunde);
}
else
{
sekunde = floatround(vreme);
if(sekunde > 9) format(string, sizeof(string), "0:%d", sekunde);
else format(string, sizeof(string), "0:0%d", sekunde);
}
return string;
}

AbyssMorgan
08/07/2016, 05:49 PM
https://github.com/AbyssMorgan/Time-Conversion/blob/master/timestamp.inc

Use my macros SecToTime or SecToTimeMini

Konstantinos
08/07/2016, 05:53 PM
here u go a function: stock ConvertTime(vreme)
{
new minuti, sekunde, string[128];
if(vreme > 59)
{
minuti = floatround(vreme/60);
sekunde = floatround(vreme - minuti*60);
if(sekunde > 9) format(string, sizeof(string), "%d:%d", minuti, sekunde);
else format(string, sizeof(string), "%d:0%d", minuti, sekunde);
}
else
{
sekunde = floatround(vreme);
if(sekunde > 9) format(string, sizeof(string), "0:%d", sekunde);
else format(string, sizeof(string), "0:0%d", sekunde);
}
return string;
}

You can use "%0xi" or "%0xd" whereas x is the number of zeros will be added if the digits are less.


ConvertTime(seconds)
{
new string[10];
format(string, sizeof string, "%02i:%02i", (seconds / 60) % 60, seconds % 60);
return string;
}

AbyssMorgan
08/07/2016, 05:54 PM
here u go a function: stock ConvertTime(vreme)
{
new minuti, sekunde, string[128];
if(vreme > 59)
{
minuti = floatround(vreme/60);
sekunde = floatround(vreme - minuti*60);
if(sekunde > 9) format(string, sizeof(string), "%d:%d", minuti, sekunde);
else format(string, sizeof(string), "%d:0%d", minuti, sekunde);
}
else
{
sekunde = floatround(vreme);
if(sekunde > 9) format(string, sizeof(string), "0:%d", sekunde);
else format(string, sizeof(string), "0:0%d", sekunde);
}
return string;
}

First, learn how to use format ~.~
https://wiki.sa-mp.com/wiki/Format

Dayrion
08/07/2016, 06:11 PM
Mine:

stock SecToMin(value)
{
new sec = value%60,
min = Euclideandiv(value, 60),
str[30];
format(str, sizeof(str), "%02i:%02i", min, sec);
return str;
}

stock Euclideandiv(dividende, diviseur, &reste = 0)
{
new
result = 0;

for(; dividende >= diviseur; result++)
dividende -= diviseur;

reste = dividende;

return result;
}

AbyssMorgan
08/07/2016, 06:16 PM
Macros is fast !

Lirbo
08/07/2016, 06:42 PM
Thanks a lot everybody :) +REP (sorry Kostantinos I can't rep u for some reason XD, probably cuz I gave reps only to u lately <3)