SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 02/06/2012, 03:27 PM   #1
Jochemd
High-roller
 
Jochemd's Avatar
 
Join Date: Jul 2009
Location: The Netherlands
Posts: 2,220
Reputation: 844
Default TimestampToDate.inc - Convert a timestamp to a date!

TimestampToDate & DateToTimestamp


Hello,

Unix Timestamps for beginners
For those who are not familliar with Unix Timestamps, take a look at Wikipedia (press the link).

Introduction
Today, I'll present one of my new works: TimestampToDate. This library is able to convert your specified timestamp to a date.

- Timezone supported.
- Leap years supported.
- Easy to use


The syntaxes are as followed:

Explanation
pawn Code:
stock TimestampToDate(Timestamp, &year, &month, &day, &hour, &minute, &second, HourGMT, MinuteGMT = 0)

Notice the last two parameters. GMT is the timezone in which we live. It's taken as the standard timezone. You should only fill in MinuteGMT if you live in a timezone which has for example: 6.5.

The year, month, day, hour, minute and second parameters should be filled in like you do in getdate and gettime.

Since v2.0, this library contains a new stock: DateToTimestamp. I will explain this as well.
pawn Code:
stock DateToTimestamp(str[11])

You could compare this with strtotime/mktime in PHP. The only parameter is a string, which is the date that should be converted to a timestamp. It should be in the following format: 06.04.2013. The "." here is the splitter. It can be changed to any character you wish (as long as you don't use numeric characters, it will probably work). It should be changed by redefining "#SPLITTER" after including this library.

Download
v2.0 --> http://pastebin.com/7LQB5Ki1
Example --> http://pastebin.com/HP9F63a2


- Open the link and press RAW.
- Copy all text and save it right here: "server_dir/pawno/includes" under filename: TimestampToDate.inc.
- Add "#include <TimestampToDate>" in the script you'd like to use it (gamemode?).


Do not redistribute this without my permission. Link people to this topic instead of direct download. It will be appreciated!
__________________

Last edited by Jochemd; 06/04/2013 at 04:09 PM.
Jochemd is offline   Reply With Quote
Old 02/06/2012, 04:33 PM   #2
Jochemd
High-roller
 
Jochemd's Avatar
 
Join Date: Jul 2009
Location: The Netherlands
Posts: 2,220
Reputation: 844
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

You can choose the GMT in which you live. Leap years are hard to make indeed, but debugging showed it works (for me) and it was hard to think about. Just the tests I tried showed it works, but of course it might be a bit buggy.

Code:
[13:41:00] tmp = 2, timestamp: 1307101260
[13:41:00] tmp = 1, timestamp: 1275565260
[13:41:00] LEAP YEAR - timestamp: 1243942860
[13:41:00] tmp = 4, timestamp: 1212406860
[13:41:00] tmp = 3, timestamp: 1180870860
[13:41:00] tmp = 2, timestamp: 1149334860
[13:41:00] tmp = 1, timestamp: 1117798860
[13:41:00] LEAP YEAR - timestamp: 1086176460
[13:41:00] tmp = 4, timestamp: 1054640460
[13:41:00] tmp = 3, timestamp: 1023104460
[13:41:00] tmp = 2, timestamp: 991568460
[13:41:00] tmp = 1, timestamp: 960032460
[13:41:00] LEAP YEAR - timestamp: 928410060
[13:41:00] tmp = 4, timestamp: 896874060
[13:41:00] tmp = 3, timestamp: 865338060
[13:41:00] tmp = 2, timestamp: 833802060
[13:41:00] tmp = 1, timestamp: 802266060
[13:41:00] LEAP YEAR - timestamp: 770643660
[13:41:00] tmp = 4, timestamp: 739107660
[13:41:00] tmp = 3, timestamp: 707571660
[13:41:00] tmp = 2, timestamp: 676035660
[13:41:00] tmp = 1, timestamp: 644499660
[13:41:00] LEAP YEAR - timestamp: 612877260
[13:41:00] tmp = 4, timestamp: 581341260
[13:41:00] tmp = 3, timestamp: 549805260
[13:41:00] tmp = 2, timestamp: 518269260
[13:41:00] tmp = 1, timestamp: 486733260
[13:41:00] LEAP YEAR - timestamp: 455110860
[13:41:00] tmp = 4, timestamp: 423574860
[13:41:00] tmp = 3, timestamp: 392038860
[13:41:00] tmp = 2, timestamp: 360502860
[13:41:00] tmp = 1, timestamp: 328966860
[13:41:00] LEAP YEAR - timestamp: 297344460
[13:41:00] tmp = 4, timestamp: 265808460
[13:41:00] tmp = 3, timestamp: 234272460
[13:41:00] tmp = 2, timestamp: 202736460
[13:41:00] tmp = 1, timestamp: 171200460
[13:41:00] LEAP YEAR - timestamp: 139578060
[13:41:00] tmp = 4, timestamp: 108042060
[13:41:00] tmp = 3, timestamp: 76506060
[13:41:00] tmp = 2, timestamp: 44970060
[13:41:00] tmp = 1, timestamp: 13434060
But thanks for looking. I can always do something with what you say ^^
__________________
Jochemd is offline   Reply With Quote
Old 02/06/2012, 08:41 PM   #3
Jochemd
High-roller
 
Jochemd's Avatar
 
Join Date: Jul 2009
Location: The Netherlands
Posts: 2,220
Reputation: 844
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

Update (v1.1)
Removed the while loop and replaced it with an never-ending for loop. It will break after a few times though. All the years (so also the leap years) are in the loop now.
__________________
Jochemd is offline   Reply With Quote
Old 03/06/2012, 12:50 AM   #4
Kyle
High-roller
 
Join Date: Mar 2009
Location: United Kingdom
Posts: 2,262
Reputation: 330
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

Quote:
Originally Posted by ****** View Post
Yeah, I realised that 2000 WAS a leap year because it was a multiple of 400, when other multiples of 100 are not, and any of the years where that is a factor are beyond the range of 32 bit integers. You realise you can do this with vastly less loops right? You don't need to loop over every year, just work out the number of leap years in between and do a huge multiply.
What ever happened to you giving constructive criticism?
Kyle is offline   Reply With Quote
Old 03/06/2012, 01:12 AM   #5
IceCube!
High-roller
 
IceCube!'s Avatar
 
Join Date: Aug 2011
Location: Grove Street, Home; Atleast it was before I fucked everything up. Motto: Carpe Diem, Bitch!
Posts: 1,777
Reputation: 230
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

Are you F***ing kidding me -.-, I couldn't find anything like this a month ago therefore I made it now I goto this section and the first thing I see is this flashing in front my eyes.

OT: Great Release its better than stamping values so should be good for a lot of people, mainly idots who are crap at maths.
__________________

Image by Phyzic
Projects (28/01/2017)
Skype: Hewlett_T
None.I'm dead, honest!
IceCube! is offline   Reply With Quote
Old 03/06/2012, 09:35 AM   #6
Jochemd
High-roller
 
Jochemd's Avatar
 
Join Date: Jul 2009
Location: The Netherlands
Posts: 2,220
Reputation: 844
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

It's good to have some critism so I can improve it.

Quote:
Originally Posted by IceCube! View Post
OT: Great Release its better than stamping values so should be good for a lot of people, mainly idots who are crap at maths.
@ IceCube: Glad you like it! In fact, timestamps are really useful! Have you ever thought about creating time-mutes, timebans, timefreeze, .... ? Get the timestamp, count the seconds on there and run a 10-second timer or so. Timestamps is something which too many people have not discovered.

Quote:
Originally Posted by ****** View Post
It could maybe have been slightly more constructive by giving a full formula, but it was just a quick reply. I had previously thought that the leap year calculation was wrong, and while technically it is, that reply was mostly to state that I realised that the calculation is correct for any period of time that this code will be used in.
Unsure if you mean this - but I tried to run this code when I set my windows-time to 2099, and the server crashed (too big integer?)
__________________
Jochemd is offline   Reply With Quote
Old 03/06/2012, 11:16 AM   #7
Niko_boy
High-roller
 
Niko_boy's Avatar
 
Join Date: Aug 2010
Location: Somewhere i belong
Posts: 1,338
Reputation: 138
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

Amazing work with this one
__________________
nope[IMG]http://*******/1r0SOkH_[/IMG]
•••[CLOSED]LCS•Freeroam•DM•Stunts•••AutoArena [0.3z][No SkinShot][sixtytiger.com]Want a decent Attack Defend Gamemode?
N/A176.31.229.148:7830Get This! Attack-Defend(v2.3.1)
Niko_boy is offline   Reply With Quote
Old 03/06/2012, 11:21 AM   #8
Jochemd
High-roller
 
Jochemd's Avatar
 
Join Date: Jul 2009
Location: The Netherlands
Posts: 2,220
Reputation: 844
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

Quote:
Originally Posted by Niko_boy View Post
Amazing work with this one
Thanks, I am writing an example at this stage so you guys can see an example function.
__________________
Jochemd is offline   Reply With Quote
Old 03/06/2012, 11:43 AM   #9
Jochemd
High-roller
 
Jochemd's Avatar
 
Join Date: Jul 2009
Location: The Netherlands
Posts: 2,220
Reputation: 844
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

Quote:
Originally Posted by ****** View Post
Sort of, yes. It will fail on years like 2101, but since their timestamps are outside the range of 32bit numbers, it doesn't matter.
We must be working with 1024 bit systems then.
__________________
Jochemd is offline   Reply With Quote
Old 03/06/2012, 01:49 PM   #10
legodude
Gangsta
 
legodude's Avatar
 
Join Date: Sep 2009
Location: 127.0.0.1 duhh
Posts: 601
Reputation: 783
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

is there any way we can use 64, or even 128 bit integers in samp?
__________________
legodude is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert a timestamp to countdown ThomasTailor93 Scripting Help 7 22/04/2012 03:39 PM
Unix timestamp to date/time function? Whizion Scripting Help 4 03/02/2012 08:01 AM
Unix timestamp for date format CyNiC Scripting Help 0 18/01/2012 01:33 PM
Is it possible to convert gettime() into Date and Time? Outbreak Help Archive 8 28/11/2009 12:32 AM


All times are GMT. The time now is 09:14 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.