29.02.2016, 14:15
ConvertTimeToString
I made this function quite a while ago, mostly because all the functions available to me didn't suit my OCD as a grammar freak. It basically converts units as small as seconds, into units as large as weeks. Useful in situations such as ban duration.pawn Код:
stock ConvertTimeToString(weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0)
{
while(seconds >= 60) minutes++, seconds -= 60;
while(minutes >= 60) hours++, minutes -= 60;
while(hours >= 24) days++, hours -= 24;
while(days >= 7) weeks++, days -= 7;
new string[70], fstr[20];
if(weeks) { format(fstr, sizeof(fstr), (weeks == 1) ? ("%d week") : ("%d weeks"), weeks); if(days || hours || minutes || seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, 0); }
if(days) { format(fstr, sizeof(fstr), (days == 1) ? ("%d day") : ("%d days"), days); if(hours || minutes || seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, strlen(string)); }
if(hours) { format(fstr, sizeof(fstr), (hours == 1) ? ("%d hour") : ("%d hours"), hours); if(minutes || seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, strlen(string)); }
if(minutes) { format(fstr, sizeof(fstr), (minutes == 1) ? ("%d minute") : ("%d minutes"), minutes); if(seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, strlen(string)); }
if(seconds) { format(fstr, sizeof(fstr), (seconds == 1) ? ("%d second") : ("%d seconds"), seconds), strins(string, fstr, strlen(string)); }
return string;
}
If you wanted 5 days, 49 hours, 17 minutes and 14 seconds:
pawn Код:
printf("%s", ConvertTimeToString(.days = 5, .hours = 49, .minutes = 17, .seconds = 14));
//Outcome: 1 week, 1 hour, 17 minutes, 14 seconds
pawn Код:
printf("%s", ConvertTimeToString(.days = 3, .hours = 3, .minutes = 18));
//Outcome: 3 days, 3 hours, 18 minutes
When a player logs in:
pawn Код:
new string[100];
format(string, sizeof(string), "You last logged in: %s ago", ConvertTimeToString(.seconds = (gettime() - LastLoggedIn[playerid])));
SendClientMessage(playerid, -1, string);
LastLoggedIn[playerid] = gettime();
--
Honestly, I am just posting this here because I always have trouble finding it when I want to look at it for references, but maybe someone finds this helpful...