Converting seconds to hours, minutes and seconds
#1

Could someone please provide me with the method to convert seconds to hours, minutes and seconds?
Reply
#2

pawn Code:
public secs2hms(secs,&hours,&minutes,&seconds) {
if (secs<0) return false;
minutes = secs / 60; seconds = secs % 60;
hours = minutes / 60; minutes = minutes % 60;
return 1;
}
pawn Code:
new h,m,s;
secs2hms(4126623,h,m,s);
printf("4126623 secs :  %d:%d:%d",h,m,s);
Reply
#3

Edit:Yep..i told you i failed at math.
Reply
#4

pawn Code:
new seconds = SOME_VALUE, minutes, hours, days;
while(seconds > 59)
{
    seconds -= 60;
    minutes++;
}
while(minutes > 59)
{
    minutes -= 60;
    hours++;
}
while(hours > 23)
{
    hours -= 24;
    days++;
}
Reply
#5

http://forum.sa-mp.com/showpost.php?...&postcount=973
Reply
#6

Can you provide an example on how to use it? It's not very clear. I attempted it, but got invalid argument errors. How does the function know how many seconds to convert from, there's no argument for that?!
Reply
#7

Quote:
Originally Posted by MP2
View Post
Can you provide an example on how to use it? It's not very clear. I attempted it, but got invalid argument errors. How does the function know how many seconds to convert from, there's no argument for that?!
Oh Dear God, please make a "******" button on everybody's head...

https://sampwiki.blast.hk/wiki/Scripting_Basics#Variables

BTW, even if you suck at math you need to know certain things before attempting scripting for SA-MP, as it requires some knowledge to even start!

1 second = 1000 miliseconds
1 minut = 60 seconds = 60,000 miliseconds
1 hour = 60 minutes = 3600 seconds = 3,600,000 miliseconds
Reply
#8

I know how to use variables; I'm not a noob. I just didn't understand how to pass the seconds that I have to the function. I worked it out now, you have to store the seconds in a variable then pass that as the first argument.

EDIT: FFS it doesn't fucking work.

pawn Code:
format(ps, sizeof(ps), "In-game [AFK for %s]", ConvertTime(secs_away));
It says 'AFK for 116 seconds'...
Reply
#9

pawn Code:
stock secs2hms(secs,&hours,&minutes,&seconds) {if (secs<0) return false;minutes = secs / 60; seconds = secs % 60;hours = minutes / 60; minutes = minutes % 60;return 1;}
Notic, I changed public to stock.. public is not needed here at all. You don't need to call it ouside the script.

now notice the &hours, & ... , the & sign means you pass a variable by reference, so the function will actually SET that variable, it's like you provide more than 1 output variable, if no & (however eg arrays are exceptions, eg strcopy) it means it's input [again, arrays are exceptions, I won't go deeper into it],
Now we want to know how many hours, mins, seconds , 3661 seconds is,
we start making our variables:
pawn Code:
new MyKnownSeconds = 3661;
new Seconds;
new Minutes;
new Hours;
now we execute our function, not at global level because that will cause errors, we execute it locally (thus inside another function/public)
pawn Code:
public OnFilterScriptInit()
{
new MyKnownSeconds = 3661;
new Seconds;
new Minutes;
new Hours;
secs2hms(/*input*/ MyKnownSeconds, /*output*/Hours,Minutes,Seconds);//Assign Hours,Minutes,Seconds the correct values, you can replace MyKnownSeconds by an number also, eg 3661
printf("H: %d M: %d S: %d",Hours,Minutes,Seconds);
return 1;
}
this will print:
Code:
H: 1 M: 1 S: 1
If you need more lemme know..
Reply
#10

But in Kyosaur's function, all the arguments are &. There is no input. Do you see what I'm saying..? I want to use his, because it automatically formats the string. But I just don't get how I pass the seconds to the function if they're all &.

It could be:

x second
x seconds
x minute and x second
x minute and x seconds
x minutes and x second
x minutes and x seconds
x hour x minute x second
x hour x minute x seconds
x hour x minutes x second
x hour x minutes x seconds
etc...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)