SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 03/06/2012, 12:49 PM   #11
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 legodude View Post
is there any way we can use 64, or even 128 bit integers in samp?
Probably not.
__________________
Jochemd is offline   Reply With Quote
Old 03/06/2012, 12:57 PM   #12
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!

so if im understanding correctly, it is possible, but hacky, so youd need some kind of native code implementation to do it?
__________________
legodude is offline   Reply With Quote
Old 04/06/2012, 10:18 PM   #13
Kyle
High-roller
 
Join Date: Mar 2009
Location: United Kingdom
Posts: 2,278
Reputation: 330
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

Quote:
Originally Posted by ****** View Post
Yes, someone wrote a library for 64 bit integers, though you need to be careful as they're not brilliant if not done right. It may be better to write a plugin.
Why would you even consider using 64 bit ints? It's not like we will be playing sa-mp in even 30 years time, ever mind 100.
Kyle is offline   Reply With Quote
Old 05/06/2012, 05:46 AM   #14
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 3,802
Reputation: 1160
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

Lol, I'd be attempting to make my own SA-MP in thirty years

OT

Should function well enough till SA-MP lives. Never-the-less, well done.
__________________
Join the best Cops And Robbers in SA-MP, today. svr.sfcnr.com:7777

Lorenc_ is offline   Reply With Quote
Old 05/06/2012, 10:12 AM   #15
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!

Thank you!

Oh btw, leap seconds are not counted in timestamps.
__________________
Jochemd is offline   Reply With Quote
Old 05/06/2012, 01:13 PM   #16
mick88
Big Clucker
 
mick88's Avatar
 
Join Date: Jan 2010
Location: Ireland
Posts: 170
Reputation: 32
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

Hi Jochemd, I think it's great that you decided to make this useful include.

I had a quick look at your code and I think it doesn't support negative timestamp. I made something like this the other day in C as a part of my programming assignment and I wanted to port it to PAWN, but seeing as you are already working on it, I may as well share my C code here. It supports negative timestamps, but doesn't regard timezones, but it's easy to implement.

Code:
bool is_leap_year(int year)
{
	/*
		This function checks if given year is a leap year: 
		it must be divisible by 4 and not divisible by 100 unless it's also divisible by 400
		- according to wikipedia
	*/
	return ((year % 4) == 0 && (((year % 100) != 0) || ((year % 400) == 0)));
}

int get_num_days(int year)
{
	/*
		Simple function, just returns number of days in given year: either 365 or 366 (leap year)
	*/
	return (is_leap_year(year)?366:365);
}

void timestamp_to_date(int timestamp, int *y, int *m, int *d, int *h, int *min, int *s)
{
	/*
		This is a highly sophisticated function that I created. It turns timestamp into ymd and hms.
		
	*/
	int days = timestamp / (60*60*24), //converting timestamp to days by dividing it by number of seconds in a day
		month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //defining number of days in a month
	/*
		Initialising starting date
	*/	

	if (timestamp >= 0)
	{
		/*
			Getting time of the day is very simple, So I do it first.
			Just take the ramainder from dividing by number of seconds in a day
		*/
		*s = timestamp % (60*60*24);

		*min = *s / 60;		//convert seconds to minutes...
		*s = *s % 60;		//...and keep the remainder

		*h = *min / 60;		//Same goes for hours...
		*min = *min % 60;	//...and minutes
	
		/*
			Now the ugly part: 'day' stores number of days since 1st January 1970.
			Somehow this has to be converted into a date.
			It's tricky, since some years have 366 days, some have 365.
		*/

		*d = 1;
		*m = 1;
		*y = 1970;

		int year_days;	//this variable stores number of days in current year, so I don't have to call the function twice in each iteration (efficiency)
		while (days > (year_days = get_num_days(*y)))
		{
			/*
				And loop through all years, substracting number of days passed each year...
				.. until number of days left is less than number of days in that year
			*/
			days -= year_days;
			 (*y)++;
		}

		/*
			Now tha I know what year it is, time to ensure that number of days in february is correct for the current year:
		*/
		if (is_leap_year(*y)) month_days[1] = 29;

		while (days > month_days[(*m)-1])
		{
			/*
				Looking for the month the same way I was looking for year
			*/
			days -=  month_days[(*m)-1];
			(*m)++;
		}
		// Remaining number of days is obviously the day of the month.
		*d += days;
	}
	else //same thing but in reverse
	{
		int year_days;
		days = -days;

		*s = -((timestamp) % (60*60*24))-1;

		*min = *s / 60;
		*s = *s % 60;

		*h = *min / 60;
		*min = *min % 60;
		
		*s = 59 - (*s);
		*min = 59-(*min);
		*h = 23-(*h);
		
		*d = 31;
		*m = 12;
		*y = 1969;

		while (days > (year_days = get_num_days(*y)))
		{
			days -= year_days;
			(*y)--;
		}

		if (is_leap_year(*y)) month_days[1] = 29;

		while (days > month_days[(*m)-1])
		{
			days -=  month_days[(*m)-1];
			(*m)--;
		}
		*d =  month_days[(*m)-1] - days;
	}
}
mick88 is offline   Reply With Quote
Old 07/06/2012, 09:08 AM   #17
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!

Negative timestamps? It indeed doesn't.
__________________
Jochemd is offline   Reply With Quote
Old 07/06/2012, 08:23 PM   #18
Hanger
Big Clucker
 
Join Date: Apr 2012
Location: UK
Posts: 161
Reputation: 32
Default Re: TimestampToDate.inc - Convert a timestamp to a date!

would not this be easier using php code via http function ?
__________________
Hanger is offline   Reply With Quote
Old 13/06/2012, 10:19 AM   #19
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 Hanger View Post
would not this be easier using php code via http function ?
Maybe. Why would you use PHP code - this works and barely takes 1 milisecond.
__________________
Jochemd is offline   Reply With Quote
Old 06/10/2012, 09:03 AM   #20
s3rserii
Big Clucker
 
Join Date: Aug 2011
Posts: 52
Reputation: 0
Default AW: TimestampToDate.inc - Convert a timestamp to a date!

can u reupload the Example ?
s3rserii 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 02:39 PM
Unix timestamp to date/time function? Whizion Scripting Help 4 03/02/2012 07:01 AM
Unix timestamp for date format CyNiC Scripting Help 0 18/01/2012 12:33 PM
Is it possible to convert gettime() into Date and Time? Outbreak Help Archive 8 27/11/2009 11:32 PM


All times are GMT. The time now is 07:55 AM.


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