]> granicus.if.org Git - php/commitdiff
New calendar parsing API
authorIlia Alshanetsky <iliaa@php.net>
Wed, 13 Feb 2008 23:50:35 +0000 (23:50 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Wed, 13 Feb 2008 23:50:35 +0000 (23:50 +0000)
ext/calendar/cal_unix.c

index be0e0c5a2b0ca791a8baaf6e38f768c13e9aedca..b565ca0233e80614aebbc9e54cb64f5d59725da2 100644 (file)
    Convert UNIX timestamp to Julian Day */
 PHP_FUNCTION(unixtojd)
 {
-  zval *timestamp;
-  long jdate; 
-  time_t t;
-  struct tm *ta, tmbuf;
-  int myargc=ZEND_NUM_ARGS();
-       
-  if ((myargc > 1) || (zend_get_parameters(ht, myargc, &timestamp) != SUCCESS)) {
-    WRONG_PARAM_COUNT;
-  }
+       time_t ts = 0;
+       struct tm *ta, tmbuf;
 
-  if(myargc==1) {
-    convert_to_long(timestamp);
-    t = Z_LVAL_P(timestamp);
-  } else {
-    t = time(NULL);
-  }
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &ts) == FAILURE) {
+               return;
+       }       
 
-  if(t < 0) {
-       RETURN_FALSE;
-  }
+       if (!ts) {
+               ts = time(NULL);
+       } else if (ts < 0) {
+               RETURN_FALSE;
+       }
 
-  ta = php_localtime_r(&t, &tmbuf);
-  if (!ta) {
-         RETURN_FALSE;
-  }
+       if (!(ta = php_localtime_r(&ts, &tmbuf))) {
+               RETURN_FALSE;
+       }
 
-  jdate = GregorianToSdn(ta->tm_year+1900, ta->tm_mon+1, ta->tm_mday);
-  
-  RETURN_LONG(jdate);
+       RETURN_LONG(GregorianToSdn(ta->tm_year+1900, ta->tm_mon+1, ta->tm_mday));
 }
 /* }}} */
 
@@ -64,21 +53,18 @@ PHP_FUNCTION(unixtojd)
    Convert Julian Day to UNIX timestamp */
 PHP_FUNCTION(jdtounix)
 {
-  zval *jday;
-  long uday;
+       long uday;
 
-  if ((ZEND_NUM_ARGS()!= 1) || (zend_get_parameters(ht, 1, &jday) != SUCCESS)) {
-    WRONG_PARAM_COUNT;
-  }
-  
-  convert_to_long(jday);
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &uday) == FAILURE) {
+               return;
+       }       
+       uday -= 2440588 /* J.D. of 1.1.1970 */;
 
-  uday = Z_LVAL_P(jday) - 2440588 /* J.D. of 1.1.1970 */;
-  
-  if(uday<0)     RETURN_FALSE; /* before beginning of unix epoch */ 
-  if(uday>24755) RETURN_FALSE; /* behind end of unix epoch */
+       if (uday < 0 || uday > 24755) { /* before beginning of unix epoch or behind end of unix epoch */ 
+               RETURN_FALSE;
+       }
 
-  RETURN_LONG(uday*24*3600);
+       RETURN_LONG(uday * 24 * 3600);
 }
 /* }}} */