Converting seconds into Days/Hours/Minutes
#1

Hello!
I was looking for a way to calculate player's time spent ingame (on server). I didn't found anything, so I started thinking... with no success lol
Actually, I made a system that saves every player second spent on server.

So I was able to do something like
pawn Код:
new pTimeOnline_Days = 0, pTimeOnline_Hours = 0, pTimeOnline_Minutes = 0, playerName[MAX_PLAYER_NAME], string[128];
format(string, sizeof(string), "users/%s.ini", playerName);

if(fexist(string)) new seconds = dini_Int(string, "Time");

pTimeOnline_Minutes = (seconds/60);
pTimeOnline_Hours = ((seconds/60)/60);
pTimeOnline_Days = (((seconds/60)/60)/24);

if(pTimeOnline_Days > 0)
{
    format(string, sizeof(string), "%i days, %i hours, %i minutes", pTimeOnline_Days, pTimeOnline_Hours, pTimeOnline_Minutes);
}
else if(!(pTimeOnline_Days > 0) && pTimeOnline_Hours > 0)
{
    format(string, sizeof(string), "%i hours, %i minutes", pTimeOnline_Hours, pTimeOnline_Minutes);
}
else if(!(pTimeOnline_Days > 0 && pTimeOnline_Hours) pTimeOnline_Minutes > 0)
{
    format(string, sizeof(string), "%i minutes", pTimeOnline_Minutes);
}
else format(string, sizeof(string), "Just Registred");

SendClientMessage(playerid, COLOR_WHITE, string);
Firstly I thought its working cuz it was saying my minutes spent on server. Then I changed Time in my configuration file to some number (lets say 200 000) (seconds).. and for example I got
Код:
2 days, 55 hours, 3333 minutes
Instead of
Код:
3 days, 7 hours and some seconds idk
I hope u know what I mean.
Reply
#2

unixtime forewer
pawn Код:
new globaltime[MAX_PLAYERS];

OnPlayerConnect(...)
{
SetPVarInt(playerid, "ENTERTIME", gettime());
...
}

OnPLayerDisconnect(...)
{
globaltime[playerid] += gettime() - GetPVarInt(playerid, "ENTERTIME");
...
}
this is only an idea on itself is implemented by.
Reply
#3

oh god, nobody on this forums doesn't read whole post.. well how the hell u think about getting server time if i asked how to seperate
days, hours, minutes that is stored in seconds inside player's configuration file!?
Reply
#4

i done this before where are you gonna be using it not sure if i wanna give it away yet took me ages
Reply
#5

pawn Код:
new pTimeOnline_Days = 0, pTimeOnline_Hours = 0, pTimeOnline_Minutes = 0, playerName[MAX_PLAYER_NAME], string[128];
format(string, sizeof(string), "users/%s.ini", playerName);
if(fexist(string)) new seconds = dini_Int(string, "Time");
pTimeOnline_Minutes = seconds/60;
while(pTimeOnline_Minutes > 59)
{
  pTimeOnline_Hours ++;
  pTimeOnline_Minutes = pTimeOnline_Minutes - 60;
}
while(pTimeOnline_Hours > 23)
{
  pTimeOnline_Days ++;
  pTimeOnline_Hours = pTimeOnline_Hours - 24;
}
if(pTimeOnline_Days > 0)
{
    format(string, sizeof(string), "%i days, %i hours, %i minutes", pTimeOnline_Days, pTimeOnline_Hours, pTimeOnline_Minutes);
}
else if(!(pTimeOnline_Days > 0) && pTimeOnline_Hours > 0)
{
    format(string, sizeof(string), "%i hours, %i minutes", pTimeOnline_Hours, pTimeOnline_Minutes);
}
else if(!(pTimeOnline_Days > 0 && pTimeOnline_Hours) pTimeOnline_Minutes > 0)
{
    format(string, sizeof(string), "%i minutes", pTimeOnline_Minutes);
}
else format(string, sizeof(string), "Just Registred");

SendClientMessage(playerid, COLOR_WHITE, string);
This should work.
Reply
#6

anyone can come in handy

Код:
ConvertSeconds(time)
{
	new string[128];
	if(time < 60)format(string, sizeof(string), "%d seconds", time);
	else if(time == 60)string = "1 minute";
	else if(time > 60 && time < 3600)
	{
		new Float: minutes;
		new seconds;
		minutes = time / 60;
		seconds = time % 60;
		format(string, sizeof(string), "%.0f minutes %d seconds", minutes, seconds);
	}
	else if(time == 3600)string = "1 hoiur";
	else if(time > 3600)
	{
		new Float: hours;
		new minutes_int;
		new Float: minutes;
		new seconds;
		hours = time / 3600;
		minutes_int = time % 3600;
		minutes = minutes_int / 60;
		seconds = minutes_int % 60;
		format(string, sizeof(string), "%.0f:%.0f:%d", hours, minutes, seconds);
	}
	return string;
}
use
Код:
format(string,sizeof(string),"SERVER: blahblahblah %s.",ConvertSeconds(TIME_TO_KICK));
PS days themselves split
Reply
#7

Quote:
Originally Posted by stepmex
anyone can come in handy

Код:
ConvertSeconds(time)
{
	new string[128];
	if(time < 60)format(string, sizeof(string), "%d seconds", time);
	else if(time == 60)string = "1 minute";
	else if(time > 60 && time < 3600)
	{
		new Float: minutes;
		new seconds;
		minutes = time / 60;
		seconds = time % 60;
		format(string, sizeof(string), "%.0f minutes %d seconds", minutes, seconds);
	}
	else if(time == 3600)string = "1 hoiur";
	else if(time > 3600)
	{
		new Float: hours;
		new minutes_int;
		new Float: minutes;
		new seconds;
		hours = time / 3600;
		minutes_int = time % 3600;
		minutes = minutes_int / 60;
		seconds = minutes_int % 60;
		format(string, sizeof(string), "%.0f:%.0f:%d", hours, minutes, seconds);
	}
	return string;
}
use
Код:
format(string,sizeof(string),"SERVER: blahblahblah %s.",ConvertSeconds(TIME_TO_KICK));
PS days themselves split
That won't work
Reply
#8

Hi

This public function work ... you can paste it "anywhere" in your script.

The Function:
pawn Код:
forward Sec2DayMinSec(InputSec, OutputString[], OutputStringLen);
public Sec2DayMinSec(InputSec, OutputString[], OutputStringLen)
{
    new
      Days,
      Hours,
      Mins,
      Secs;
     
    Days = floatround((InputSec / 86400), floatround_floor); // get Days
    Secs = (InputSec - (Days * 86400));// remove days from total seconds
    Hours = floatround((Secs / 3600), floatround_floor); // get Hours
    Secs = (Secs - (Hours * 3600)); // remove Hours from secs
    Mins = floatround((Secs / 60), floatround_floor);// get Mins
    Secs = (Secs - (Mins * 60));// remove Mins from secs, whats left is seconds

    format(OutputString, OutputStringLen,
        "%i days %i hours %i minutes and %i seconds",
      Days, Hours, Mins, Secs);
    return 1;
}
I tested the function using this in OnGameModeInit and/or OnFilterScriptInit
pawn Код:
new TestString[128];// string to store the output (128 is my fav number for that)
    Sec2DayMinSec(120, TestString, sizeof(TestString));// gettin the string for 120 seconds
    print(TestString);// printing the string
    Sec2DayMinSec(120654789, TestString, sizeof(TestString));// gettin the string for 120654789 seconds
    print(TestString);// printing...
Good Luck !
Reply
#9

Quote:
Originally Posted by [HiC
TheKiller ]That won't work
I do not spread what is not working
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)