Back to index

php5  5.3.10
cal_unix.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Shane Caraveo             <shane@caraveo.com>               | 
00016    |          Colin Viebrock            <colin@easydns.com>               |
00017    |          Hartmut Holzgraefe        <hholzgra@php.net>                |
00018    +----------------------------------------------------------------------+
00019  */
00020 /* $Id: */
00021 
00022 #include "php.h"
00023 #include "php_calendar.h"
00024 #include "sdncal.h"
00025 #include <time.h>
00026 
00027 /* {{{ proto int unixtojd([int timestamp])
00028    Convert UNIX timestamp to Julian Day */
00029 PHP_FUNCTION(unixtojd)
00030 {
00031        time_t ts = 0;
00032        struct tm *ta, tmbuf;
00033 
00034        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &ts) == FAILURE) {
00035               return;
00036        }      
00037 
00038        if (!ts) {
00039               ts = time(NULL);
00040        } else if (ts < 0) {
00041               RETURN_FALSE;
00042        }
00043 
00044        if (!(ta = php_localtime_r(&ts, &tmbuf))) {
00045               RETURN_FALSE;
00046        }
00047 
00048        RETURN_LONG(GregorianToSdn(ta->tm_year+1900, ta->tm_mon+1, ta->tm_mday));
00049 }
00050 /* }}} */
00051 
00052 /* {{{ proto int jdtounix(int jday)
00053    Convert Julian Day to UNIX timestamp */
00054 PHP_FUNCTION(jdtounix)
00055 {
00056        long uday;
00057 
00058        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &uday) == FAILURE) {
00059               return;
00060        }      
00061        uday -= 2440588 /* J.D. of 1.1.1970 */;
00062 
00063        if (uday < 0 || uday > 24755) { /* before beginning of unix epoch or behind end of unix epoch */ 
00064               RETURN_FALSE;
00065        }
00066 
00067        RETURN_LONG(uday * 24 * 3600);
00068 }
00069 /* }}} */
00070 
00071 /*
00072  * Local variables:
00073  * tab-width: 4
00074  * c-basic-offset: 4
00075  * End:
00076  * vim600: sw=4 ts=4 fdm=marker
00077  * vim<600: sw=4 ts=4
00078  */