#include "sdncal.h"
#include <time.h>
-/* {{{ proto int unixtojd([int timestamp])
- Convert UNIX timestamp to Julian Day */
+ #define SECS_PER_DAY (24 * 3600)
+
+/* {{{ Convert UNIX timestamp to Julian Day */
PHP_FUNCTION(unixtojd)
{
- time_t ts = 0;
+ time_t ts;
zend_long tl = 0;
+ zend_bool tl_is_null = 1;
struct tm *ta, tmbuf;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &tl) == FAILURE) {
- return;
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &tl, &tl_is_null) == FAILURE) {
+ RETURN_THROWS();
}
- if (!tl) {
+ if (tl_is_null) {
ts = time(NULL);
} else if (tl >= 0) {
ts = (time_t) tl;
}
uday -= 2440588 /* J.D. of 1.1.1970 */;
- if (uday < 0 || uday > 24755) {
- zend_value_error("jday must be within the Unix epoch");
+ if (uday < 0 || uday > ZEND_LONG_MAX / SECS_PER_DAY) { /* before beginning of unix epoch or greater than representable */
- RETURN_FALSE;
++ zend_value_error("jday must be between 2440588 and " ZEND_LONG_FMT, ZEND_LONG_MAX / SECS_PER_DAY + 2440588);
+ RETURN_THROWS();
}
- RETURN_LONG(uday * 24 * 3600);
+ RETURN_LONG(uday * SECS_PER_DAY);
}
/* }}} */
--- /dev/null
-var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440589));
+ --TEST--
+ Bug #80185 (jdtounix() fails after 2037)
+ --SKIPIF--
+ <?php
+ if (!extension_loaded('calendar')) die('skip ext/calendar required');
+ if (PHP_INT_SIZE != 8) die("skip for 64bit platforms only");
+ ?>
+ --FILE--
+ <?php
+ var_dump(jdtounix(2465712));
+ var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440588));
-bool(false)
++try {
++ var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440589));
++} catch (ValueError $ex) {
++ echo $ex->getMessage(), PHP_EOL;
++}
+ ?>
+ --EXPECT--
+ int(2170713600)
+ int(9223372036854720000)
++jday must be between 2440588 and 106751993607888
--- /dev/null
-var_dump(jdtounix(2465712));
+ --TEST--
+ Bug #80185 (jdtounix() fails after 2037)
+ --SKIPIF--
+ <?php
+ if (!extension_loaded('calendar')) die('skip ext/calendar required');
+ if (PHP_INT_SIZE != 4) die("skip for 32bit platforms only");
+ ?>
+ --FILE--
+ <?php
-var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440589));
++try {
++ var_dump(jdtounix(2465712));
++} catch (ValueError $ex) {
++ echo $ex->getMessage(), PHP_EOL;
++}
+ var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440588));
-bool(false)
++try {
++ var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440589));
++} catch (ValueError $ex) {
++ echo $ex->getMessage(), PHP_EOL;
++}
+ ?>
+ --EXPECT--
-bool(false)
++jday must be between 2440588 and 2465443
+ int(2147472000)
++jday must be between 2440588 and 2465443
<?php include 'skipif.inc'; ?>
--FILE--
<?php
-var_dump(jdtounix(2440579)) . PHP_EOL;
+try {
+ jdtounix(2440579);
+} catch (ValueError $ex) {
+ echo $ex->getMessage(), PHP_EOL;
+}
?>
--EXPECT--
- jday must be within the Unix epoch
-bool(false)
++jday must be between 2440588 and 2465443