1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Shane Caraveo <shane@caraveo.com> |
14 | Colin Viebrock <colin@easydns.com> |
15 | Hartmut Holzgraefe <hholzgra@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #include "php.h"
20 #include "php_calendar.h"
21 #include "sdncal.h"
22 #include <time.h>
23
24 #define SECS_PER_DAY (24 * 3600)
25
26 /* {{{ Convert UNIX timestamp to Julian Day */
PHP_FUNCTION(unixtojd)27 PHP_FUNCTION(unixtojd)
28 {
29 time_t ts;
30 zend_long tl = 0;
31 bool tl_is_null = 1;
32 struct tm *ta, tmbuf;
33
34 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!", &tl, &tl_is_null) == FAILURE) {
35 RETURN_THROWS();
36 }
37
38 if (tl_is_null) {
39 ts = time(NULL);
40 } else if (tl >= 0) {
41 ts = (time_t) tl;
42 } else {
43 zend_argument_value_error(1, "must be greater than or equal to 0");
44 RETURN_THROWS();
45 }
46
47 if (!(ta = php_localtime_r(&ts, &tmbuf))) {
48 RETURN_FALSE;
49 }
50
51 RETURN_LONG(GregorianToSdn(ta->tm_year+1900, ta->tm_mon+1, ta->tm_mday));
52 }
53 /* }}} */
54
55 /* {{{ Convert Julian Day to UNIX timestamp */
PHP_FUNCTION(jdtounix)56 PHP_FUNCTION(jdtounix)
57 {
58 zend_long uday;
59
60 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &uday) == FAILURE) {
61 RETURN_THROWS();
62 }
63 if (uday < 2440588 || (uday - 2440588) > (ZEND_LONG_MAX / SECS_PER_DAY)) { /* before beginning of unix epoch or greater than representable */
64 zend_value_error("jday must be between 2440588 and " ZEND_LONG_FMT, ZEND_LONG_MAX / SECS_PER_DAY + 2440588);
65 RETURN_THROWS();
66 }
67
68 uday -= 2440588 /* J.D. of 1.1.1970 */;
69
70 RETURN_LONG(uday * SECS_PER_DAY);
71 }
72 /* }}} */
73