[Tutorial] How to: Unix timestamps
#41

You bet!
Reply
#42

https://sampforum.blast.hk/showthread.php?tid=497929 can anyone help me here
Reply
#43

Quote:
Originally Posted by __
View Post
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
  1. Introduction
  2. Contents of this tutorial (here!)
  3. What Unix timestamps are
  4. Why, when and how you should use them
  5. Some useful calculations
  6. 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.
the hyper link http://pastebin.ca/2058337
not work
Reply
#44

I've also used this timestamp feature for a temporary ban system and wrote a few things:
pawn Code:
// Convert Seconds to Days, Hours, Minutes and Seconds
StoDHMS(Time, &Days, &Hours, &Minutes, &Seconds)
{
    // Convert the given time in seconds to days, hours, minutes and seconds
    Days = Time / 86400;
    Time = Time - (Days * 86400);
    Hours = Time / 3600;
    Time = Time - (Hours * 3600);
    Minutes = Time / 60;
    Seconds = Time - (Minutes * 60);
}
You can supply this function with a given timestamp difference (to check how much time it still takes until the ban is automatically removed, and supply 4 variables in which the function returns it's calculations.
This function actually converts the amounts of seconds you supply into Days, Hours, Minutes and Seconds.

A small snippet from my current code show it in action:
pawn Code:
// Setup local variables
    new Msg[128], UnbanTime, RemainingBanTime, Days, Hours, Minutes, Seconds;

    // Check if this account is still banned
    UnbanTime = cache_get_field_content_int(0, "UnbanTime", SQL_db);
    if (UnbanTime > gettime())
    {
        // Get the remaining bantime (unbantime - current time)
        RemainingBanTime = UnbanTime - gettime();
        // Convert the remaining bantime to Days, Hours, Minutes and Seconds
        StoDHMS(RemainingBanTime, Days, Hours, Minutes, Seconds);
        // Kick the player
        format(Msg, sizeof(Msg), "{FF0000}Your account is still banned for %i days, %i hours, %i minutes and %i seconds", Days, Hours, Minutes, Seconds);
        Player_Kick(playerid, Msg);
        // Exit the function
        return 1;
    }
It reads the unbantime from the database (the timestamp when the ban will expire), substracts the current timestamp and the result is how long the ban remains in effect (in seconds).
The function converts it into Days, Hours, Minutes and Seconds and displays the remaining bantime to the player and kicks him.
Reply
#45

2018 still amazing
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)