[Include] TimestampToDate.inc - Convert a timestamp to a date!
#16

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.

Код:
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;
	}
}
Reply


Messages In This Thread
TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 02.06.2012, 14:27
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 02.06.2012, 15:33
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 02.06.2012, 19:41
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Kyle - 02.06.2012, 23:50
Re: TimestampToDate.inc - Convert a timestamp to a date! - by IceCube! - 03.06.2012, 00:12
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 03.06.2012, 08:35
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Niko_boy - 03.06.2012, 10:16
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 03.06.2012, 10:21
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 03.06.2012, 10:43
Re: TimestampToDate.inc - Convert a timestamp to a date! - by legodude - 03.06.2012, 12:49
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 03.06.2012, 12:49
Re: TimestampToDate.inc - Convert a timestamp to a date! - by legodude - 03.06.2012, 12:57
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Kyle - 04.06.2012, 22:18
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Lorenc_ - 05.06.2012, 05:46
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 05.06.2012, 10:12
Re: TimestampToDate.inc - Convert a timestamp to a date! - by mick88 - 05.06.2012, 13:13
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 07.06.2012, 09:08
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Hanger - 07.06.2012, 20:23
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 13.06.2012, 10:19
AW: TimestampToDate.inc - Convert a timestamp to a date! - by s3rserii - 06.10.2012, 09:03
Re: AW: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 07.10.2012, 07:04
AW: TimestampToDate.inc - Convert a timestamp to a date! - by s3rserii - 07.10.2012, 09:21
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Ballu Miaa - 03.12.2012, 11:47
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Ballu Miaa - 03.12.2012, 16:13
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 12.12.2012, 19:06
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Ballu Miaa - 13.12.2012, 02:08
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 06.04.2013, 14:52
Re: TimestampToDate.inc - Convert a timestamp to a date! - by shaPP - 12.02.2014, 03:52
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Drake1994 - 13.02.2014, 23:02
Re: TimestampToDate.inc - Convert a timestamp to a date! - by shaPP - 14.02.2014, 03:35
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 30.03.2014, 12:22
Respuesta: TimestampToDate.inc - Convert a timestamp to a date! - by MugiwaraNoLuffy - 31.03.2014, 05:21
Re: Respuesta: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 31.03.2014, 13:00
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Zio - 06.04.2014, 23:15
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 08.04.2014, 15:02
Respuesta: Re: Respuesta: TimestampToDate.inc - Convert a timestamp to a date! - by MugiwaraNoLuffy - 09.04.2014, 17:30
Respuesta: Re: TimestampToDate.inc - Convert a timestamp to a date! - by MugiwaraNoLuffy - 10.04.2014, 03:37
Re: TimestampToDate.inc - Convert a timestamp to a date! - by sammp - 30.06.2014, 21:54
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 30.06.2014, 22:11
Re: TimestampToDate.inc - Convert a timestamp to a date! - by PapaSmerf - 24.07.2014, 19:01
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 25.07.2014, 09:25
Re: TimestampToDate.inc - Convert a timestamp to a date! - by PapaSmerf - 25.07.2014, 11:51
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 25.07.2014, 12:46
Re: TimestampToDate.inc - Convert a timestamp to a date! - by dominik523 - 05.12.2014, 20:09
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 07.12.2014, 10:20
Re: TimestampToDate.inc - Convert a timestamp to a date! - by dominik523 - 07.12.2014, 17:23
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 07.12.2014, 19:44
Re: TimestampToDate.inc - Convert a timestamp to a date! - by dominik523 - 07.12.2014, 20:25
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 07.12.2014, 20:44
Re: TimestampToDate.inc - Convert a timestamp to a date! - by dominik523 - 07.12.2014, 21:19
Re : TimestampToDate.inc - Convert a timestamp to a date! - by Baltimore - 27.12.2014, 22:41
Re : TimestampToDate.inc - Convert a timestamp to a date! - by Baltimore - 28.12.2014, 03:19
Re: Re : TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 28.12.2014, 15:59
Re: TimestampToDate.inc - Convert a timestamp to a date! - by DetoNater - 23.06.2015, 08:28
Re: TimestampToDate.inc - Convert a timestamp to a date! - by darkowner - 23.06.2015, 09:39
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 23.06.2015, 20:32
Re: TimestampToDate.inc - Convert a timestamp to a date! - by TenTen - 07.07.2015, 21:05
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Crayder - 19.10.2015, 06:26
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 19.10.2015, 06:59
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Redirect Left - 17.11.2015, 16:26
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Firerfan - 29.12.2015, 20:22
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jeffry - 29.11.2016, 17:47
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Shetch - 29.01.2017, 00:11
Re: TimestampToDate.inc - Convert a timestamp to a date! - by MerryDeer - 29.01.2017, 08:02
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 04.02.2017, 15:14
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Kwarde - 23.12.2017, 01:15
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Jochemd - 29.12.2017, 09:50
Re: TimestampToDate.inc - Convert a timestamp to a date! - by Kwarde - 29.12.2017, 10:46

Forum Jump:


Users browsing this thread: 13 Guest(s)