xref: /PHP-5.6/ext/calendar/cal_unix.c (revision 49493a2d)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Shane Caraveo             <shane@caraveo.com>               |
16    |          Colin Viebrock            <colin@easydns.com>               |
17    |          Hartmut Holzgraefe        <hholzgra@php.net>                |
18    +----------------------------------------------------------------------+
19  */
20 /* $Id: */
21 
22 #include "php.h"
23 #include "php_calendar.h"
24 #include "sdncal.h"
25 #include <time.h>
26 
27 /* {{{ proto int unixtojd([int timestamp])
28    Convert UNIX timestamp to Julian Day */
PHP_FUNCTION(unixtojd)29 PHP_FUNCTION(unixtojd)
30 {
31 	time_t ts = 0;
32 	struct tm *ta, tmbuf;
33 
34 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &ts) == FAILURE) {
35 		return;
36 	}
37 
38 	if (!ts) {
39 		ts = time(NULL);
40 	} else if (ts < 0) {
41 		RETURN_FALSE;
42 	}
43 
44 	if (!(ta = php_localtime_r(&ts, &tmbuf))) {
45 		RETURN_FALSE;
46 	}
47 
48 	RETURN_LONG(GregorianToSdn(ta->tm_year+1900, ta->tm_mon+1, ta->tm_mday));
49 }
50 /* }}} */
51 
52 /* {{{ proto int jdtounix(int jday)
53    Convert Julian Day to UNIX timestamp */
PHP_FUNCTION(jdtounix)54 PHP_FUNCTION(jdtounix)
55 {
56 	long uday;
57 
58 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &uday) == FAILURE) {
59 		return;
60 	}
61 	uday -= 2440588 /* J.D. of 1.1.1970 */;
62 
63 	if (uday < 0 || uday > 24755) { /* before beginning of unix epoch or behind end of unix epoch */
64 		RETURN_FALSE;
65 	}
66 
67 	RETURN_LONG(uday * 24 * 3600);
68 }
69 /* }}} */
70 
71 /*
72  * Local variables:
73  * tab-width: 4
74  * c-basic-offset: 4
75  * End:
76  * vim600: sw=4 ts=4 fdm=marker
77  * vim<600: sw=4 ts=4
78  */
79