How to: Unix timestamps -
__ - 13.05.2011
How to: Unix timestamps
Tutorial by __
Introduction
A lot of you may not understand how useful Unix timestamps actually are, which is why I am creating this tutorial.
Contents of this tutorial- Introduction
- Contents of this tutorial (here!)
- What Unix timestamps are
- Why, when and how you should use them
- Some useful calculations
- Decoding timestamps
What Unix timestamps are
Unix time (or 'POSIX time') is essentially a timescale, from January 1, 1970 to January 19, 2038 (if you're using 32-bit timestamps, see
here for more information).
Unix timestamps are integers (numbers) that basically state the amount of seconds it has been since the 1st of January 1970, these are very useful in programming and are used by loads of scripters/coders/programmers.
Despite the simplicity of Unix timestamps, some scripters still use more complicated and less efficient methods, for example - storing days, hours, minutes, seconds and years and trying to calculate with those as variables, instead of using a single Unix timestamp - which would generally speaking be a lot more efficient.
An example of a Unix timestamp is:
1305302089
Why, when and how you should use them
How you can use them
Scenario: Temporary VIP - A feature quite a few servers have, an admin retains the ability to give someone a special rank temporarily, after X the rank expires and they no longer have access to it.
The code for this scenario can be found
here. Note, the code wasn't tested - it was only compiled.
Unix timestamps are very effective in PAWN, using gettime() without any parameters (so literally just 'gettime();', with or without the semi colon) returns a Unix timestamp.
Why you should use them
What's the point in creating multiple variables that you don't really need? All you really need is 1 long integer.
When you should use them
Whenever you have something that isn't indefinite award a player, I've written a short list of some examples, though do note there are many more uses for Unix timestamps in PAWN/SA-MP:
- Temporary VIP
- Temporary houses
- Temporary businesses
- Temporary admin
- Temporary Job timeouts
- And many more...
Some useful calculations
Add these on to gettime() to calculate the appropriate times.
1 minute (60 seconds) is 60 * 1 = 60
2 minutes (120 seconds) is 60 * 2 = 120
1 hour (60 minutes) is 60 * 60 = 3600
2 hours (120 minutes) is 60 * 120 = 7200
1 day (24 hours) is 60 * 60 * 24 = 86400
2 days (48 hours) is 60 * 60 * 48 = 172800
1 week (7 days) is 60 * 60 * 24 * 7 = 604800
2 weeks (14 days) is 60 * 60 * 24 * 14 = 1209600
1 month (30 days) is 60 * 60 * 24 * 30 = 2592000
2 months (60 days) is 60 * 60 * 24 * 60 = 5184000
1 year (365 days) is 60 * 60 * 24 * 365 = 31536000
2 years (730 days) is 60 * 60 * 24 * 730 = 63072000
An example:
pawn Code:
new
iTime = gettime() + 5184000; // 2 months.
Decoding timestamps
In PHP and a few similar languages, you can decode unix timestamps to dates by using the date() function with your timestamp as an extra parameter, unfortunately - there are no native functions in PAWN that can do this.
Thanks to Blacklite, a function has been provided which you can use to compare two timestamps (for example, your timestamp that you saved and the current timestamp) to get the amount of time in between the timestamps. Note, the function has a 'compare' parameter which if empty gets the current timestamp anyway.
The code is available here, full credits belong to Blacklite for releasing the function in this topic and for creating it.
Theoretically (seeing as I haven't tested it), you should be able to use:
pawn Code:
new
iRandomTime = gettime()-86402; // Now minus 24 hours and 2 seconds, for the sake of this example
szMessage[64];
format(szMessage, sizeof(szMessage), "It has been: %s", timec(iRandomTime)); // Format our message, saying how long it has been since 'iRandomTime'
print(szMessage); // Print our string
You shouldn't always need to decode timestamps, but a great usage of doing so is to state how long something is for, or something of that nature.
Re: How to: Unix timestamps -
CrunkBankS - 13.05.2011
How to add to the current unixstamp 30 days?
Re: How to: Unix timestamps - [03]Garsino - 13.05.2011
Nice work. But maybe add a section for calculating how many minutes/hours/days/weeks etc. has passed since the unix timestamp was set?
Re: How to: Unix timestamps -
__ - 13.05.2011
Quote:
Originally Posted by CrunkBankS
How to add to the current unixstamp 30 days?
|
86400 * 30 = 2592000
Quote:
Originally Posted by [03]Garsino
Nice work. But maybe add a section for calculating how many minutes/hours/days/weeks etc. has passed since the unix timestamp was set?
|
Do you mean some calculations to help figure out how much to add on to a timestamp or what? I didn't quite understand your message if you meant something else.
Re: How to: Unix timestamps - [03]Garsino - 13.05.2011
I mean like (for example, take a temporaly ban system). You ban the player for 2 weeks, 3 days and 2 hours. How would you go around detecting how many weeks, days and hours that has passed since the unix timestamp was set (the ban date).
Re: How to: Unix timestamps -
__ - 13.05.2011
I've added a list of calculations now, so it should be a bit more clear, but for your predicament, if you had a command with the syntax of '/tempban [playerid] [years] [weeks] [days] [hours]' then you'd have to do something like this:
pawn Code:
CMD:tempban
(playerid, params
[]) { new iYears,
iMonths,
iFinalCalculation
= gettime(),
iHours,
iWeeks,
iTarget;
if(sscanf
(params,
"udddddds", iTarget, iYears, iMonths, iWeeks, iDays, iHours, iMinutes, szReason
)) return SendClientMessage
(playerid, COLOR,
"Syntax: /tempban [playerid] [years] [months] [weeks] [days] [hours] [minutes] [reason]");
iFinalCalculation
= gettime() + mktime
(iHours, iMinutes,
0, iDays, iMonths, iYears
);
// get mktime() from here: http://y-less.pastebin.ca/1000201 // iFinalCalculation retains the final unix timestamp you'll set them to be banned for. // Check when a player connects to see if they have a timestamp, and if they do, check if it exceeds gettime() // ...if it does, then unban/remove ban return 1;
}
Re: How to: Unix timestamps -
Zimon95 - 13.05.2011
Wow, I didn't knew that! Thank you very much, now I can create some temporary systems!
Re: How to: Unix timestamps -
__ - 14.05.2011
Released a filterscript using Unix timestamps for temporary banning:
http://forum.sa-mp.com/showthread.ph...22#post1205522
Re: How to: Unix timestamps -
Haydz - 16.05.2011
Thank you so much for the tut. Converted most of my script over to using unix. Hopefully alot more people will start using this!
Re: How to: Unix timestamps -
linuxthefish - 16.05.2011
Well done, Unix timestamps FTW!
Re: How to: Unix timestamps -
Swiftz - 16.05.2011
I have a doubt gettime functions gets server pc time our client pc time coz it can be easily manipulated if its based on client time
Re: How to: Unix timestamps -
Zimon95 - 16.05.2011
Quote:
Originally Posted by Swiftz
I have a doubt gettime functions gets server pc time our client pc time coz it can be easily manipulated if its based on client time
|
The function
gettime, if you pass parameters, returns the server time.
Re: How to: Unix timestamps -
__ - 17.05.2011
Quote:
Originally Posted by Swiftz
I have a doubt gettime functions gets server pc time our client pc time coz it can be easily manipulated if its based on client time
|
Swiftz, it's a server-side function. If it was going to get the clientside time, there would at least be a playerid parameter.
Quote:
Originally Posted by Zimon95
The function gettime, if you pass parameters, returns the server time.
|
Yup.
Re: How to: Unix timestamps -
Blacklite - 19.05.2011
Here are the 2 functions I wrote in PHP:
Code:
function diff($int1, $int2) {
if ($int1 > $int2) {
return $int1 - $int2;
}
return $int2 - $int1;
}
function timec($timestamp, $compare = null) {
if ($compare === null) {
$compare = time();
}
$diff = diff($timestamp, $compare);
if ($diff < 60) {
return '< 1 minute';
} else if ($diff < 3600) {
$num = floor($diff / 60).' minute';
} else if ($diff < 86400) {
$num = floor($diff / 60 / 60).' hour';
} else if ($diff < 2592000) {
$num = floor($diff / 60 / 60 / 24).' day';
} else if ($diff < 31536000) { // 31536000 = 1 year
$num = floor($diff / 60 / 60 / 24 / 30).' month';
} else {
return 'A very very long time';
}
if ($num > 1) {
return $num.'s';
}
return $num;
}
I have made an attempt at converting it to pawn (not tested but should work!):
pawn Code:
stock timec(timestamp, compare = -1) {
if (compare == -1) {
compare = gettime();
}
new
n,
// on the following line, I have removed the need for the diff() function.
// if you want to use the diff() function in pawn, replace the following with:
// Float:d = diff(timestamp, compare),
Float:d = (timestamp > compare) ? timestamp - compare : compare - timestamp,
returnstr[32];
if (d < 60) {
format(returnstr, sizeof(returnstr), '< 1 minute');
return returnstr;
} else if (d < 3600) { // 3600 = 1 hour
n = floatround(floatdiv(d, 60.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'minute');
} else if (d < 86400) { // 86400 = 1 day
n = floatround(floatdiv(d, 3600.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'hour');
} else if (d < 2592000) { // 2592000 = 1 month
n = floatround(floatdiv(d, 86400.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'day');
} else if (d < 31536000) { // 31536000 = 1 year
n = floatround(floatdiv(d, 2592000.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'month');
} else {
n = floatround(floatdiv(d, 31536000.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'year');
}
if (n == 1) {
format(returnstr, sizeof(returnstr), '1 %s', returnstr);
} else {
format(returnstr, sizeof(returnstr), '%d %ss', n, returnstr);
}
return returnstr;
}
Re: How to: Unix timestamps -
__ - 19.05.2011
Quote:
Originally Posted by Blacklite
Here are the 2 functions I wrote in PHP:
Code:
function diff($int1, $int2) {
if ($int1 > $int2) {
return $int1 - $int2;
}
return $int2 - $int1;
}
function timec($timestamp, $compare = null) {
if ($compare === null) {
$compare = time();
}
$diff = diff($timestamp, $compare);
if ($diff < 60) {
return '< 1 minute';
} else if ($diff < 3600) {
$num = floor($diff / 60).' minute';
} else if ($diff < 86400) {
$num = floor($diff / 60 / 60).' hour';
} else if ($diff < 2592000) {
$num = floor($diff / 60 / 60 / 24).' day';
} else if ($diff < 31536000) { // 31536000 = 1 year
$num = floor($diff / 60 / 60 / 24 / 30).' month';
} else {
return 'A very very long time';
}
if ($num > 1) {
return $num.'s';
}
return $num;
}
I have made an attempt at converting it to pawn (not tested but should work!):
pawn Code:
stock timec(timestamp, compare = -1) { if (compare == -1) { compare = gettime(); } new n, // on the following line, I have removed the need for the diff() function. // if you want to use the diff() function in pawn, replace the following with: // Float:d = diff(timestamp, compare), Float:d = (timestamp > compare) ? timestamp - compare : compare - timestamp, returnstr[32]; if (d < 60) { format(returnstr, sizeof(returnstr), '< 1 minute'); return returnstr; } else if (d < 3600) { // 3600 = 1 hour n = floatround(floatdiv(d, 60.0), floatround_floor); format(returnstr, sizeof(returnstr), 'minute'); } else if (d < 86400) { // 86400 = 1 day n = floatround(floatdiv(d, 3600.0), floatround_floor); format(returnstr, sizeof(returnstr), 'hour'); } else if (d < 2592000) { // 2592000 = 1 month n = floatround(floatdiv(d, 86400.0), floatround_floor); format(returnstr, sizeof(returnstr), 'day'); } else if (d < 31536000) { // 31536000 = 1 year n = floatround(floatdiv(d, 2592000.0), floatround_floor); format(returnstr, sizeof(returnstr), 'month'); } else { n = floatround(floatdiv(d, 31536000.0), floatround_floor); format(returnstr, sizeof(returnstr), 'year'); } if (n == 1) { format(returnstr, sizeof(returnstr), '1 %s', returnstr); } else { format(returnstr, sizeof(returnstr), '%d %ss', n, returnstr); } return returnstr; }
|
Brilliant, I'll try to test it later.
Re: How to: Unix timestamps -
BaubaS - 19.05.2011
Add a part of decoding unix.
Re: How to: Unix timestamps -
__ - 19.05.2011
Done, thanks for the idea BaubaS.
Re: How to: Unix timestamps -
BaubaS - 19.05.2011
Btw, for example when player buys a VIP Im setting time variable to 2592000(1month)
After I played some hours, I want to "decode" and know how much VIP I will be?
Re: How to: Unix timestamps -
Blacklite - 19.05.2011
Quote:
Originally Posted by BaubaS
Btw, for example when player buys a VIP Im setting time variable to 2592000(1month)
After I played some hours, I want to "decode" and know how much VIP I will be?
|
Try the function I posted earlier in this thread.
Re: How to: Unix timestamps -
Scenario - 14.06.2011
How could I go about decoding the time-stamp to give me an exact date? I would like to be able to tell people when they will be able to join the server again after being banned. All I can do right now is tell them how many years, months, days, etc. they have been banned for.