php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #40213
Patch correct_easter_function_to_use_TimeZone revision 2013-09-17 12:24 UTC by pierre dot renaudet at gmail dot com

Patch correct_easter_function_to_use_TimeZone for Calendar related Bug #40213

Patch version 2013-09-17 12:24 UTC

Return to Bug #40213 | Download this patch
Patch Revisions:

Developer: pierre.renaudet@gmail.com

diff --git a/ext/calendar/easter.c b/ext/calendar/easter.c
index 1948ff9..ad0ddd9 100644
--- a/ext/calendar/easter.c
+++ b/ext/calendar/easter.c
@@ -22,35 +22,37 @@
 #include "php.h"
 #include "php_calendar.h"
 #include "sdncal.h"
-#include <time.h>
+#include "../date/php_date.h"
 
 static void _cal_easter(INTERNAL_FUNCTION_PARAMETERS, int gm)
 {
 
 	/* based on code by Simon Kershaw, <webmaster@ely.anglican.org> */
 
-	struct tm te;
-	long year, golden, solar, lunar, pfm, dom, tmp, easter;
+	struct tm;
+        long golden, solar, lunar, pfm, dom, tmp, easter, ts;
+	long year = 0;
 	long method = CAL_EASTER_DEFAULT;
-
-	/* Default to the current year if year parameter is not given */
-	{
-		time_t a;
-		struct tm b, *res;
-		time(&a);
-		res = php_localtime_r(&a, &b);
-		if (!res) {
-			year = 1900;
-		} else {
-			year = 1900 + b.tm_year;
-		}
-	}
-
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
-		"|ll", &year, &method) == FAILURE) {
+        int error;
+        
+        timelib_time *tt;
+        timelib_tzinfo *tzi = NULL;
+        
+        /* Init the Timelib_time */
+        tt = timelib_time_ctor();
+        tzi = get_timezone_info(TSRMLS_C);
+        tt->tz_info = tzi;
+        tt->zone_type = TIMELIB_ZONETYPE_ID;
+	
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &year, &method) == FAILURE) {
 			return;
 	}
- 
+        
+        if(!year){
+            timelib_unixtime2local(tt, (timelib_sll) time(NULL));
+            year = tt->y;
+        }
+        
 	if (gm && (year<1970 || year>2037)) {				/* out of range for timestamps */
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "This function is only valid for years between 1970 and 2037 inclusive");
 		RETURN_FALSE;
@@ -98,26 +100,30 @@
 	easter = pfm + tmp + 1;	    					/* Easter as the number of days after 21st March */
 
 	if (gm) {							/* return a timestamp */
-		te.tm_isdst = -1;
-		te.tm_year = year-1900;
-		te.tm_sec = 0;
-		te.tm_min = 0;
-		te.tm_hour = 0;
-
+                tt->y = year;
+		tt->s = 0;
+                tt->i = 0;
+                tt->h = 0;
 		if (easter < 11) {
-			te.tm_mon = 2;			/* March */
-			te.tm_mday = easter+21;
+			tt->m = 3;			/* March */
+                        tt->d = easter+21;
 		} else {
-			te.tm_mon = 3;			/* April */
-			te.tm_mday = easter-10;
+			tt->m = 4;			/* April */
+			tt->d = easter-10;
 		}
-
-	        Z_LVAL_P(return_value) = mktime(&te);
-	} else {							/* return the days after March 21 */	
-	        Z_LVAL_P(return_value) = easter;
+                timelib_update_ts(tt, tzi);
+                ts = timelib_date_to_int(tt, &error);
+                timelib_time_dtor(tt);
+                if(error){                                              /* Unable to convert TimeLib to TimeStamp */
+                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to convert in TimeStamp");
+                        RETURN_FALSE;
+                }else{
+                        RETURN_LONG(ts);
+                }
+	} else {						/* return the days after March 21 */	
+                RETURN_LONG(easter)
 	}
 
-        Z_TYPE_P(return_value) = IS_LONG;
 
 }
 
diff --git a/ext/calendar/tests/easter_days.phpt b/ext/calendar/tests/easter_days.phpt
index 04aa7ae..8967a47 100644
--- a/ext/calendar/tests/easter_days.phpt
+++ b/ext/calendar/tests/easter_days.phpt
@@ -1,5 +1,7 @@
 --TEST--
 easter_days()
+--INI--
+date.timezone=UTC
 --SKIPIF--
 <?php include 'skipif.inc'; ?>
 --FILE--
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Jun 23 10:01:23 2021 UTC