[Tutorial] How to: Unix timestamps
#1

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.
Reply


Messages In This Thread
How to: Unix timestamps - by __ - 13.05.2011, 15:22
Re: How to: Unix timestamps - by CrunkBankS - 13.05.2011, 16:58
Re: How to: Unix timestamps - by [03]Garsino - 13.05.2011, 17:11
Re: How to: Unix timestamps - by __ - 13.05.2011, 17:39
Re: How to: Unix timestamps - by [03]Garsino - 13.05.2011, 17:43
Re: How to: Unix timestamps - by __ - 13.05.2011, 17:58
Re: How to: Unix timestamps - by Zimon95 - 13.05.2011, 18:34
Re: How to: Unix timestamps - by __ - 14.05.2011, 10:10
Re: How to: Unix timestamps - by Haydz - 16.05.2011, 10:52
Re: How to: Unix timestamps - by linuxthefish - 16.05.2011, 11:40
Re: How to: Unix timestamps - by Swiftz - 16.05.2011, 15:47
Re: How to: Unix timestamps - by Zimon95 - 16.05.2011, 17:57
Re: How to: Unix timestamps - by __ - 17.05.2011, 08:39
Re: How to: Unix timestamps - by Blacklite - 19.05.2011, 00:23
Re: How to: Unix timestamps - by __ - 19.05.2011, 10:04
Re: How to: Unix timestamps - by BaubaS - 19.05.2011, 14:30
Re: How to: Unix timestamps - by __ - 19.05.2011, 14:42
Re: How to: Unix timestamps - by BaubaS - 19.05.2011, 16:38
Re: How to: Unix timestamps - by Blacklite - 19.05.2011, 22:00
Re: How to: Unix timestamps - by Scenario - 14.06.2011, 19:30
Re: How to: Unix timestamps - by Anzipane! - 14.06.2011, 20:39
Re: How to: Unix timestamps - by Blacklite - 14.06.2011, 21:14
Re: How to: Unix timestamps - by jot16 - 14.06.2011, 21:17
Re: How to: Unix timestamps - by Jack_Rocker - 14.06.2011, 21:20
Re: How to: Unix timestamps - by Iphone1234g - 15.06.2011, 09:41
Re: How to: Unix timestamps - by __ - 17.06.2011, 23:08
Re: How to: Unix timestamps - by Steamator - 20.06.2011, 14:45
Re: How to: Unix timestamps - by ||123|| - 14.07.2011, 07:26
Re: How to: Unix timestamps - by cessil - 14.07.2011, 12:50
Re: How to: Unix timestamps - by __ - 15.07.2011, 05:39
Re: How to: Unix timestamps - by Jantjuh - 16.07.2011, 19:49
Re: How to: Unix timestamps - by BrUn3S - 18.10.2011, 18:59
Re: How to: Unix timestamps - by Scenario - 17.12.2011, 00:28
Re: How to: Unix timestamps - by Rokzlive - 07.01.2012, 20:02
Re: How to: Unix timestamps - by Scenario - 07.01.2012, 20:07
Re: How to: Unix timestamps - by Rokzlive - 07.01.2012, 20:14
Re: How to: Unix timestamps - by Calgon - 23.03.2012, 10:42
Re: How to: Unix timestamps - by __ - 15.06.2012, 07:19
Re: How to: Unix timestamps - by Tanush123 - 22.05.2013, 21:03
Re: How to: Unix timestamps - by trukker1998 - 19.02.2014, 11:35
Re: How to: Unix timestamps - by Scenario - 19.02.2014, 11:53
Re: How to: Unix timestamps - by AroseKhanNiazi - 01.03.2014, 11:38
Re: How to: Unix timestamps - by _Application_ - 20.01.2015, 11:20
Re: How to: Unix timestamps - by PowerPC603 - 20.01.2015, 13:37
Re: How to: Unix timestamps - by AdamCooper - 07.10.2018, 00:45

Forum Jump:


Users browsing this thread: 2 Guest(s)