PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/calendar - calendar.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 212 238 89.1 %
Date: 2014-11-22 Functions: 18 18 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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             :    |          Wez Furlong               <wez@thebrainroom.com>            |
      19             :    +----------------------------------------------------------------------+
      20             :  */
      21             : /* $Id$ */
      22             : 
      23             : #ifdef HAVE_CONFIG_H
      24             : #include "config.h"
      25             : #endif
      26             : 
      27             : #ifdef PHP_WIN32
      28             : #define _WINNLS_
      29             : #endif
      30             : 
      31             : #include "php.h"
      32             : #include "ext/standard/info.h"
      33             : #include "php_calendar.h"
      34             : #include "sdncal.h"
      35             : 
      36             : #include <stdio.h>
      37             : 
      38             : /* {{{ arginfo */
      39             : ZEND_BEGIN_ARG_INFO_EX(arginfo_unixtojd, 0, 0, 0)
      40             :         ZEND_ARG_INFO(0, timestamp)
      41             : ZEND_END_ARG_INFO()
      42             : 
      43             : ZEND_BEGIN_ARG_INFO(arginfo_jdtounix, 0)
      44             :         ZEND_ARG_INFO(0, jday)
      45             : ZEND_END_ARG_INFO()
      46             : 
      47             : ZEND_BEGIN_ARG_INFO_EX(arginfo_cal_info, 0, 0, 0)
      48             :         ZEND_ARG_INFO(0, calendar)
      49             : ZEND_END_ARG_INFO()
      50             : 
      51             : ZEND_BEGIN_ARG_INFO(arginfo_cal_days_in_month, 0)
      52             :         ZEND_ARG_INFO(0, calendar)
      53             :         ZEND_ARG_INFO(0, month)
      54             :         ZEND_ARG_INFO(0, year)
      55             : ZEND_END_ARG_INFO()
      56             : 
      57             : ZEND_BEGIN_ARG_INFO(arginfo_cal_to_jd, 0)
      58             :         ZEND_ARG_INFO(0, calendar)
      59             :         ZEND_ARG_INFO(0, month)
      60             :         ZEND_ARG_INFO(0, day)
      61             :         ZEND_ARG_INFO(0, year)
      62             : ZEND_END_ARG_INFO()
      63             : 
      64             : ZEND_BEGIN_ARG_INFO(arginfo_cal_from_jd, 0)
      65             :         ZEND_ARG_INFO(0, jd)
      66             :         ZEND_ARG_INFO(0, calendar)
      67             : ZEND_END_ARG_INFO()
      68             : 
      69             : ZEND_BEGIN_ARG_INFO(arginfo_jdtogregorian, 0)
      70             :         ZEND_ARG_INFO(0, juliandaycount)
      71             : ZEND_END_ARG_INFO()
      72             : 
      73             : ZEND_BEGIN_ARG_INFO(arginfo_gregoriantojd, 0)
      74             :         ZEND_ARG_INFO(0, month)
      75             :         ZEND_ARG_INFO(0, day)
      76             :         ZEND_ARG_INFO(0, year)
      77             : ZEND_END_ARG_INFO()
      78             : 
      79             : ZEND_BEGIN_ARG_INFO(arginfo_jdtojulian, 0)
      80             :         ZEND_ARG_INFO(0, juliandaycount)
      81             : ZEND_END_ARG_INFO()
      82             : 
      83             : ZEND_BEGIN_ARG_INFO(arginfo_juliantojd, 0)
      84             :         ZEND_ARG_INFO(0, month)
      85             :         ZEND_ARG_INFO(0, day)
      86             :         ZEND_ARG_INFO(0, year)
      87             : ZEND_END_ARG_INFO()
      88             : 
      89             : ZEND_BEGIN_ARG_INFO_EX(arginfo_jdtojewish, 0, 0, 1)
      90             :         ZEND_ARG_INFO(0, juliandaycount)
      91             :         ZEND_ARG_INFO(0, hebrew)
      92             :         ZEND_ARG_INFO(0, fl)
      93             : ZEND_END_ARG_INFO()
      94             : 
      95             : ZEND_BEGIN_ARG_INFO(arginfo_jewishtojd, 0)
      96             :         ZEND_ARG_INFO(0, month)
      97             :         ZEND_ARG_INFO(0, day)
      98             :         ZEND_ARG_INFO(0, year)
      99             : ZEND_END_ARG_INFO()
     100             : 
     101             : ZEND_BEGIN_ARG_INFO(arginfo_jdtofrench, 0)
     102             :         ZEND_ARG_INFO(0, juliandaycount)
     103             : ZEND_END_ARG_INFO()
     104             : 
     105             : ZEND_BEGIN_ARG_INFO(arginfo_frenchtojd, 0)
     106             :         ZEND_ARG_INFO(0, month)
     107             :         ZEND_ARG_INFO(0, day)
     108             :         ZEND_ARG_INFO(0, year)
     109             : ZEND_END_ARG_INFO()
     110             : 
     111             : ZEND_BEGIN_ARG_INFO_EX(arginfo_jddayofweek, 0, 0, 1)
     112             :         ZEND_ARG_INFO(0, juliandaycount)
     113             :         ZEND_ARG_INFO(0, mode)
     114             : ZEND_END_ARG_INFO()
     115             : 
     116             : ZEND_BEGIN_ARG_INFO(arginfo_jdmonthname, 0)
     117             :         ZEND_ARG_INFO(0, juliandaycount)
     118             :         ZEND_ARG_INFO(0, mode)
     119             : ZEND_END_ARG_INFO()
     120             : 
     121             : ZEND_BEGIN_ARG_INFO_EX(arginfo_easter_date, 0, 0, 0)
     122             :         ZEND_ARG_INFO(0, year)
     123             : ZEND_END_ARG_INFO()
     124             : 
     125             : ZEND_BEGIN_ARG_INFO_EX(arginfo_easter_days, 0, 0, 0)
     126             :         ZEND_ARG_INFO(0, year)
     127             :         ZEND_ARG_INFO(0, method)
     128             : ZEND_END_ARG_INFO()
     129             : 
     130             : /* }}} */
     131             : 
     132             : const zend_function_entry calendar_functions[] = {
     133             :         PHP_FE(jdtogregorian, arginfo_jdtogregorian)
     134             :         PHP_FE(gregoriantojd, arginfo_gregoriantojd)
     135             :         PHP_FE(jdtojulian, arginfo_jdtojulian)
     136             :         PHP_FE(juliantojd, arginfo_juliantojd)
     137             :         PHP_FE(jdtojewish, arginfo_jdtojewish)
     138             :         PHP_FE(jewishtojd, arginfo_jewishtojd)
     139             :         PHP_FE(jdtofrench, arginfo_jdtofrench)
     140             :         PHP_FE(frenchtojd, arginfo_frenchtojd)
     141             :         PHP_FE(jddayofweek, arginfo_jddayofweek)
     142             :         PHP_FE(jdmonthname, arginfo_jdmonthname)
     143             :         PHP_FE(easter_date, arginfo_easter_date)
     144             :         PHP_FE(easter_days, arginfo_easter_days)
     145             :         PHP_FE(unixtojd, arginfo_unixtojd)
     146             :         PHP_FE(jdtounix, arginfo_jdtounix)
     147             :         PHP_FE(cal_to_jd, arginfo_cal_to_jd)
     148             :         PHP_FE(cal_from_jd, arginfo_cal_from_jd)
     149             :         PHP_FE(cal_days_in_month, arginfo_cal_days_in_month)
     150             :         PHP_FE(cal_info, arginfo_cal_info)
     151             :         PHP_FE_END
     152             : };
     153             : 
     154             : 
     155             : zend_module_entry calendar_module_entry = {
     156             :         STANDARD_MODULE_HEADER,
     157             :         "calendar",
     158             :         calendar_functions,
     159             :         PHP_MINIT(calendar),
     160             :         NULL,
     161             :         NULL,
     162             :         NULL,
     163             :         PHP_MINFO(calendar),
     164             :         NO_VERSION_YET,
     165             :         STANDARD_MODULE_PROPERTIES,
     166             : };
     167             : 
     168             : #ifdef COMPILE_DL_CALENDAR
     169             : ZEND_GET_MODULE(calendar)
     170             : #endif
     171             : 
     172             : /* this order must match the conversion table below */
     173             : enum cal_name_type_t {
     174             :         CAL_GREGORIAN = 0,
     175             :         CAL_JULIAN,
     176             :         CAL_JEWISH,
     177             :         CAL_FRENCH,
     178             :         CAL_NUM_CALS
     179             : };
     180             : 
     181             : typedef zend_long (*cal_to_jd_func_t) (int month, int day, int year);
     182             : typedef void (*cal_from_jd_func_t) (zend_long jd, int *year, int *month, int *day);
     183             : typedef char *(*cal_as_string_func_t) (int year, int month, int day);
     184             : 
     185             : struct cal_entry_t {
     186             :         char *name;
     187             :         char *symbol;
     188             :         cal_to_jd_func_t to_jd;
     189             :         cal_from_jd_func_t from_jd;
     190             :         int num_months;
     191             :         int max_days_in_month;
     192             :         char **month_name_short;
     193             :         char **month_name_long;
     194             : };
     195             : 
     196             : static struct cal_entry_t cal_conversion_table[CAL_NUM_CALS] = {
     197             :         {"Gregorian", "CAL_GREGORIAN", GregorianToSdn, SdnToGregorian, 12, 31,
     198             :          MonthNameShort, MonthNameLong},
     199             :         {"Julian", "CAL_JULIAN", JulianToSdn, SdnToJulian, 12, 31,
     200             :          MonthNameShort, MonthNameLong},
     201             :         {"Jewish", "CAL_JEWISH", JewishToSdn, SdnToJewish, 13, 30,
     202             :          JewishMonthNameLeap, JewishMonthNameLeap},
     203             :         {"French", "CAL_FRENCH", FrenchToSdn, SdnToFrench, 13, 30,
     204             :          FrenchMonthName, FrenchMonthName}
     205             : };
     206             : 
     207             : #define JEWISH_MONTH_NAME(year)         ((monthsPerYear[((year)-1) % 19] == 13)?JewishMonthNameLeap:JewishMonthName)
     208             : #define JEWISH_HEB_MONTH_NAME(year) ((monthsPerYear[((year)-1) % 19] == 13)?JewishMonthHebNameLeap:JewishMonthHebName)
     209             : 
     210             : /* For jddayofweek */
     211             : enum { CAL_DOW_DAYNO, CAL_DOW_SHORT, CAL_DOW_LONG };
     212             : 
     213             : /* For jdmonthname */
     214             : enum { CAL_MONTH_GREGORIAN_SHORT, CAL_MONTH_GREGORIAN_LONG,
     215             :         CAL_MONTH_JULIAN_SHORT, CAL_MONTH_JULIAN_LONG, CAL_MONTH_JEWISH,
     216             :         CAL_MONTH_FRENCH
     217             : };
     218             : 
     219             : /* for heb_number_to_chars */
     220             : static char alef_bet[25] = "0אבגדהוזחטיכלמנסעפצקרשת";
     221             : 
     222             : #define CAL_JEWISH_ADD_ALAFIM_GERESH 0x2
     223             : #define CAL_JEWISH_ADD_ALAFIM 0x4
     224             : #define CAL_JEWISH_ADD_GERESHAYIM 0x8
     225             : 
     226       20507 : PHP_MINIT_FUNCTION(calendar)
     227             : {
     228       20507 :         REGISTER_LONG_CONSTANT("CAL_GREGORIAN", CAL_GREGORIAN, CONST_CS | CONST_PERSISTENT);
     229       20507 :         REGISTER_LONG_CONSTANT("CAL_JULIAN", CAL_JULIAN, CONST_CS | CONST_PERSISTENT);
     230       20507 :         REGISTER_LONG_CONSTANT("CAL_JEWISH", CAL_JEWISH, CONST_CS | CONST_PERSISTENT);
     231       20507 :         REGISTER_LONG_CONSTANT("CAL_FRENCH", CAL_FRENCH, CONST_CS | CONST_PERSISTENT);
     232       20507 :         REGISTER_LONG_CONSTANT("CAL_NUM_CALS", CAL_NUM_CALS, CONST_CS | CONST_PERSISTENT);
     233             : /* constants for jddayofweek */
     234       20507 :         REGISTER_LONG_CONSTANT("CAL_DOW_DAYNO", CAL_DOW_DAYNO, CONST_CS | CONST_PERSISTENT);
     235       20507 :         REGISTER_LONG_CONSTANT("CAL_DOW_SHORT", CAL_DOW_SHORT, CONST_CS | CONST_PERSISTENT);
     236       20507 :         REGISTER_LONG_CONSTANT("CAL_DOW_LONG", CAL_DOW_LONG, CONST_CS | CONST_PERSISTENT);
     237             : /* constants for jdmonthname */
     238       20507 :         REGISTER_LONG_CONSTANT("CAL_MONTH_GREGORIAN_SHORT", CAL_MONTH_GREGORIAN_SHORT, CONST_CS | CONST_PERSISTENT);
     239       20507 :         REGISTER_LONG_CONSTANT("CAL_MONTH_GREGORIAN_LONG", CAL_MONTH_GREGORIAN_LONG, CONST_CS | CONST_PERSISTENT);
     240       20507 :         REGISTER_LONG_CONSTANT("CAL_MONTH_JULIAN_SHORT", CAL_MONTH_JULIAN_SHORT, CONST_CS | CONST_PERSISTENT);
     241       20507 :         REGISTER_LONG_CONSTANT("CAL_MONTH_JULIAN_LONG", CAL_MONTH_JULIAN_LONG, CONST_CS | CONST_PERSISTENT);
     242       20507 :         REGISTER_LONG_CONSTANT("CAL_MONTH_JEWISH", CAL_MONTH_JEWISH, CONST_CS | CONST_PERSISTENT);
     243       20507 :         REGISTER_LONG_CONSTANT("CAL_MONTH_FRENCH", CAL_MONTH_FRENCH, CONST_CS | CONST_PERSISTENT);
     244             : /* constants for easter calculation */
     245       20507 :         REGISTER_LONG_CONSTANT("CAL_EASTER_DEFAULT", CAL_EASTER_DEFAULT, CONST_CS | CONST_PERSISTENT);
     246       20507 :         REGISTER_LONG_CONSTANT("CAL_EASTER_ROMAN", CAL_EASTER_ROMAN, CONST_CS | CONST_PERSISTENT);
     247       20507 :         REGISTER_LONG_CONSTANT("CAL_EASTER_ALWAYS_GREGORIAN", CAL_EASTER_ALWAYS_GREGORIAN, CONST_CS | CONST_PERSISTENT);
     248       20507 :         REGISTER_LONG_CONSTANT("CAL_EASTER_ALWAYS_JULIAN", CAL_EASTER_ALWAYS_JULIAN, CONST_CS | CONST_PERSISTENT);
     249             : /* constants for Jewish date formatting */
     250       20507 :         REGISTER_LONG_CONSTANT("CAL_JEWISH_ADD_ALAFIM_GERESH", CAL_JEWISH_ADD_ALAFIM_GERESH, CONST_CS | CONST_PERSISTENT);
     251       20507 :         REGISTER_LONG_CONSTANT("CAL_JEWISH_ADD_ALAFIM", CAL_JEWISH_ADD_ALAFIM, CONST_CS | CONST_PERSISTENT);
     252       20507 :         REGISTER_LONG_CONSTANT("CAL_JEWISH_ADD_GERESHAYIM", CAL_JEWISH_ADD_GERESHAYIM, CONST_CS | CONST_PERSISTENT);
     253       20507 :         return SUCCESS;
     254             : }
     255             : 
     256         143 : PHP_MINFO_FUNCTION(calendar)
     257             : {
     258         143 :         php_info_print_table_start();
     259         143 :         php_info_print_table_row(2, "Calendar support", "enabled");
     260         143 :         php_info_print_table_end();
     261         143 : }
     262             : 
     263           5 : static void _php_cal_info(int cal, zval *ret)
     264             : {
     265             :         zval months, smonths;
     266             :         int i;
     267             :         struct cal_entry_t *calendar;
     268             : 
     269           5 :         calendar = &cal_conversion_table[cal];
     270           5 :         array_init(ret);
     271             : 
     272           5 :         array_init(&months);
     273           5 :         array_init(&smonths);
     274             : 
     275          67 :         for (i = 1; i <= calendar->num_months; i++) {
     276          62 :                 add_index_string(&months, i, calendar->month_name_long[i]);
     277          62 :                 add_index_string(&smonths, i, calendar->month_name_short[i]);
     278             :         }
     279             :         
     280           5 :         add_assoc_zval(ret, "months", &months);
     281           5 :         add_assoc_zval(ret, "abbrevmonths", &smonths);
     282           5 :         add_assoc_long(ret, "maxdaysinmonth", calendar->max_days_in_month);
     283           5 :         add_assoc_string(ret, "calname", calendar->name);
     284           5 :         add_assoc_string(ret, "calsymbol", calendar->symbol);
     285             :         
     286           5 : }
     287             : 
     288             : /* {{{ proto array cal_info([int calendar])
     289             :    Returns information about a particular calendar */
     290           3 : PHP_FUNCTION(cal_info)
     291             : {
     292           3 :         zend_long cal = -1;
     293             : 
     294             : 
     295           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &cal) == FAILURE) {
     296           0 :                 RETURN_FALSE;
     297             :         }
     298             : 
     299           3 :         if (cal == -1) {
     300             :                 int i;
     301             :                 zval val;
     302             : 
     303           1 :                 array_init(return_value);
     304             : 
     305           5 :                 for (i = 0; i < CAL_NUM_CALS; i++) {
     306           4 :                         _php_cal_info(i, &val);
     307           4 :                         add_index_zval(return_value, i, &val);
     308             :                 }
     309           1 :                 return;
     310             :         }
     311             : 
     312             : 
     313           2 :         if (cal != -1 && (cal < 0 || cal >= CAL_NUM_CALS)) {
     314           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid calendar ID %pd.", cal);
     315           1 :                 RETURN_FALSE;
     316             :         }
     317             : 
     318           1 :         _php_cal_info(cal, return_value);
     319             : 
     320             : }
     321             : /* }}} */
     322             : 
     323             : /* {{{ proto int cal_days_in_month(int calendar, int month, int year)
     324             :    Returns the number of days in a month for a given year and calendar */
     325          34 : PHP_FUNCTION(cal_days_in_month)
     326             : {
     327             :         zend_long cal, month, year;
     328             :         struct cal_entry_t *calendar;
     329             :         zend_long sdn_start, sdn_next;
     330             : 
     331          34 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &cal, &month, &year) == FAILURE) {
     332           0 :                 RETURN_FALSE;
     333             :         }
     334             : 
     335          34 :         if (cal < 0 || cal >= CAL_NUM_CALS) {
     336           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid calendar ID %pd.", cal);
     337           1 :                 RETURN_FALSE;
     338             :         }
     339             : 
     340          33 :         calendar = &cal_conversion_table[cal];
     341             : 
     342          33 :         sdn_start = calendar->to_jd(year, month, 1);
     343             : 
     344          33 :         if (sdn_start == 0) {
     345           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid date.");
     346           1 :                 RETURN_FALSE;
     347             :         }
     348             : 
     349          32 :         sdn_next = calendar->to_jd(year, 1 + month, 1);
     350             : 
     351          32 :         if (sdn_next == 0) {
     352             :                 /* If the next month is invalid, then we need to try the first month of
     353             :                  * the next year, bearing in mind that the next year after 1 BCE is
     354             :                  * actually 1 AD and not 0. */
     355           5 :                 if (year == -1) {
     356           2 :                         sdn_next = calendar->to_jd(1, 1, 1);
     357             :                 }
     358             :                 else {
     359           3 :                         sdn_next = calendar->to_jd(year + 1, 1, 1);
     360             :                 }
     361             :         }
     362             : 
     363          32 :         RETURN_LONG(sdn_next - sdn_start);
     364             : }
     365             : /* }}} */
     366             : 
     367             : /* {{{ proto int cal_to_jd(int calendar, int month, int day, int year)
     368             :    Converts from a supported calendar to Julian Day Count */
     369           4 : PHP_FUNCTION(cal_to_jd)
     370             : {
     371             :         zend_long cal, month, day, year;
     372             : 
     373           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "llll", &cal, &month, &day, &year) != SUCCESS) {
     374           0 :                 RETURN_FALSE;
     375             :         }
     376             : 
     377           4 :         if (cal < 0 || cal >= CAL_NUM_CALS) {
     378           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid calendar ID %pd.", cal);
     379           0 :                 RETURN_FALSE;
     380             :         }
     381             : 
     382           4 :         RETURN_LONG(cal_conversion_table[cal].to_jd(year, month, day));
     383             : }
     384             : /* }}} */
     385             : 
     386             : /* {{{ proto array cal_from_jd(int jd, int calendar)
     387             :    Converts from Julian Day Count to a supported calendar and return extended information */
     388           6 : PHP_FUNCTION(cal_from_jd)
     389             : {
     390             :         zend_long jd, cal;
     391             :         int month, day, year, dow;
     392             :         char date[16];
     393             :         struct cal_entry_t *calendar;
     394             : 
     395           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "ll", &jd, &cal) == FAILURE) {
     396           0 :                 RETURN_FALSE;
     397             :         }
     398             : 
     399           6 :         if (cal < 0 || cal >= CAL_NUM_CALS) {
     400           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid calendar ID %pd", cal);
     401           0 :                 RETURN_FALSE;
     402             :         }
     403           6 :         calendar = &cal_conversion_table[cal];
     404             : 
     405           6 :         array_init(return_value);
     406             : 
     407           6 :         calendar->from_jd(jd, &year, &month, &day);
     408             : 
     409           6 :         snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
     410           6 :         add_assoc_string(return_value, "date", date);
     411             : 
     412           6 :         add_assoc_long(return_value, "month", month);
     413           6 :         add_assoc_long(return_value, "day", day);
     414           6 :         add_assoc_long(return_value, "year", year);
     415             : 
     416             : /* day of week */
     417           6 :         dow = DayOfWeek(jd);
     418           6 :         add_assoc_long(return_value, "dow", dow);
     419           6 :         add_assoc_string(return_value, "abbrevdayname", DayNameShort[dow]);
     420           6 :         add_assoc_string(return_value, "dayname", DayNameLong[dow]);
     421             : /* month name */
     422           6 :         if(cal == CAL_JEWISH) {
     423             :                 /* special case for Jewish calendar */
     424           1 :                 add_assoc_string(return_value, "abbrevmonth", JEWISH_MONTH_NAME(year)[month]);
     425           1 :                 add_assoc_string(return_value, "monthname", JEWISH_MONTH_NAME(year)[month]);
     426             :         } else {
     427           5 :                 add_assoc_string(return_value, "abbrevmonth", calendar->month_name_short[month]);
     428           5 :                 add_assoc_string(return_value, "monthname", calendar->month_name_long[month]);
     429             :         }
     430             : }
     431             : /* }}} */
     432             : 
     433             : /* {{{ proto string jdtogregorian(int juliandaycount)
     434             :    Converts a julian day count to a gregorian calendar date */
     435           5 : PHP_FUNCTION(jdtogregorian)
     436             : {
     437             :         zend_long julday;
     438             :         int year, month, day;
     439             :         char date[16];
     440             : 
     441           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &julday) == FAILURE) {
     442           0 :                 RETURN_FALSE;
     443             :         }
     444             : 
     445           5 :         SdnToGregorian(julday, &year, &month, &day);
     446           5 :         snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
     447             : 
     448          10 :         RETURN_STRING(date);
     449             : }
     450             : /* }}} */
     451             : 
     452             : /* {{{ proto int gregoriantojd(int month, int day, int year)
     453             :    Converts a gregorian calendar date to julian day count */
     454          16 : PHP_FUNCTION(gregoriantojd)
     455             : {
     456             :         zend_long year, month, day;
     457             : 
     458          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &month, &day, &year) == FAILURE) {
     459           0 :                 RETURN_FALSE;
     460             :         }
     461             : 
     462          16 :         RETURN_LONG(GregorianToSdn(year, month, day));
     463             : }
     464             : /* }}} */
     465             : 
     466             : /* {{{ proto string jdtojulian(int juliandaycount)
     467             :    Convert a julian day count to a julian calendar date */
     468           5 : PHP_FUNCTION(jdtojulian)
     469             : {
     470             :         zend_long julday;
     471             :         int year, month, day;
     472             :         char date[16];
     473             : 
     474           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &julday) == FAILURE) {
     475           0 :                 RETURN_FALSE;
     476             :         }
     477             : 
     478           5 :         SdnToJulian(julday, &year, &month, &day);
     479           5 :         snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
     480             : 
     481          10 :         RETURN_STRING(date);
     482             : }
     483             : /* }}} */
     484             : 
     485             : /* {{{ proto int juliantojd(int month, int day, int year)
     486             :    Converts a julian calendar date to julian day count */
     487           5 : PHP_FUNCTION(juliantojd)
     488             : {
     489             :         zend_long year, month, day;
     490             : 
     491           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &month, &day, &year) == FAILURE) {
     492           0 :                 RETURN_FALSE;
     493             :         }
     494             : 
     495           5 :         RETURN_LONG(JulianToSdn(year, month, day));
     496             : }
     497             : /* }}} */
     498             : 
     499             : /* {{{ heb_number_to_chars*/
     500             : /*
     501             : caution: the Hebrew format produces non unique result.
     502             : for example both: year '5' and year '5000' produce 'ה'.
     503             : use the numeric one for calculations. 
     504             :  */
     505          18 : static char *heb_number_to_chars(int n, int fl, char **ret)
     506             : {
     507             :         char *p, old[18], *endofalafim;
     508             : 
     509          18 :         p = endofalafim = old;
     510             : /* 
     511             :    prevents the option breaking the jewish beliefs, and some other 
     512             :    critical resources ;)
     513             :  */
     514          18 :         if (n > 9999 || n < 1) {
     515           0 :                 *ret = NULL;
     516           0 :                 return NULL;
     517             :         }       
     518             : 
     519             : /* alafim (thousands) case */
     520          18 :         if (n / 1000) {
     521           9 :                 *p = alef_bet[n / 1000];
     522           9 :                 p++;
     523             : 
     524           9 :                 if (CAL_JEWISH_ADD_ALAFIM_GERESH & fl) {
     525           4 :                         *p = '\'';
     526           4 :                         p++;
     527             :                 }
     528           9 :                 if (CAL_JEWISH_ADD_ALAFIM & fl) {
     529           4 :                         strcpy(p, " אלפים ");
     530           4 :                         p += 7;
     531             :                 }
     532             : 
     533           9 :                 endofalafim = p;
     534           9 :                 n = n % 1000;
     535             :         }
     536             : 
     537             : /* tav-tav (tav=400) case */
     538          45 :         while (n >= 400) {
     539           9 :                 *p = alef_bet[22];
     540           9 :                 p++;
     541           9 :                 n -= 400;
     542             :         }
     543             : 
     544             : /* meot (hundreads) case */
     545          18 :         if (n >= 100) {
     546           9 :                 *p = alef_bet[18 + n / 100];
     547           9 :                 p++;
     548           9 :                 n = n % 100;
     549             :         }
     550             : 
     551             : /* tet-vav & tet-zain case (special case for 15 and 16) */
     552          18 :         if (n == 15 || n == 16) {
     553           0 :                 *p = alef_bet[9];
     554           0 :                 p++;
     555           0 :                 *p = alef_bet[n - 9];
     556           0 :                 p++;
     557             :         } else {
     558             : /* asarot (tens) case */
     559          18 :                 if (n >= 10) {
     560          14 :                         *p = alef_bet[9 + n / 10];
     561          14 :                         p++;
     562          14 :                         n = n % 10;
     563             :                 }
     564             : 
     565             : /* yehidot (ones) case */
     566          18 :                 if (n > 0) {
     567          18 :                         *p = alef_bet[n];
     568          18 :                         p++;
     569             :                 }
     570             :         }
     571             : 
     572          18 :         if (CAL_JEWISH_ADD_GERESHAYIM & fl) {
     573          10 :                 switch (p - endofalafim) {
     574             :                 case 0:
     575           0 :                         break;
     576             :                 case 1:
     577           4 :                         *p = '\'';
     578           4 :                         p++;
     579           4 :                         break;
     580             :                 default:
     581           6 :                         *(p) = *(p - 1);
     582           6 :                         *(p - 1) = '"';
     583           6 :                         p++;
     584             :                 }
     585             :         }
     586             : 
     587          18 :         *p = '\0';
     588          18 :         *ret = estrndup(old, (p - old) + 1);
     589          18 :         p = *ret;
     590          18 :         return p;
     591             : }
     592             : /* }}} */
     593             : 
     594             : /* {{{ proto string jdtojewish(int juliandaycount [, bool hebrew [, int fl]])
     595             :    Converts a julian day count to a jewish calendar date */
     596          15 : PHP_FUNCTION(jdtojewish)
     597             : {
     598          15 :         zend_long julday, fl = 0;
     599          15 :         zend_bool heb   = 0;
     600             :         int year, month, day;
     601             :         char date[16], hebdate[32];
     602             :         char *dayp, *yearp;
     603             : 
     604          15 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|bl", &julday, &heb, &fl) == FAILURE) {
     605           0 :                 RETURN_FALSE;
     606             :         }
     607             : 
     608          15 :         SdnToJewish(julday, &year, &month, &day);
     609          15 :         if (!heb) {
     610           6 :                 snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
     611          12 :                 RETURN_STRING(date);
     612             :         } else {
     613           9 :                 if (year <= 0 || year > 9999) {
     614           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Year out of range (0-9999).");
     615           0 :                         RETURN_FALSE;
     616             :                 }
     617             : 
     618           9 :                 snprintf(hebdate, sizeof(hebdate), "%s %s %s", heb_number_to_chars(day, fl, &dayp), JEWISH_HEB_MONTH_NAME(year)[month], heb_number_to_chars(year, fl, &yearp));
     619             : 
     620           9 :                 if (dayp) {
     621           9 :                         efree(dayp);
     622             :                 }
     623           9 :                 if (yearp) {
     624           9 :                         efree(yearp);
     625             :                 }
     626             : 
     627          18 :                 RETURN_STRING(hebdate);
     628             : 
     629             :         }
     630             : }
     631             : /* }}} */
     632             : 
     633             : /* {{{ proto int jewishtojd(int month, int day, int year)
     634             :    Converts a jewish calendar date to a julian day count */
     635           4 : PHP_FUNCTION(jewishtojd)
     636             : {
     637             :         zend_long year, month, day;
     638             : 
     639           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &month, &day, &year) == FAILURE) {
     640           0 :                 RETURN_FALSE;
     641             :         }
     642             : 
     643           4 :         RETURN_LONG(JewishToSdn(year, month, day));
     644             : }
     645             : /* }}} */
     646             : 
     647             : /* {{{ proto string jdtofrench(int juliandaycount)
     648             :    Converts a julian day count to a french republic calendar date */
     649           6 : PHP_FUNCTION(jdtofrench)
     650             : {
     651             :         zend_long julday;
     652             :         int year, month, day;
     653             :         char date[16];
     654             : 
     655           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &julday) == FAILURE) {
     656           0 :                 RETURN_FALSE;
     657             :         }
     658             : 
     659           6 :         SdnToFrench(julday, &year, &month, &day);
     660           6 :         snprintf(date, sizeof(date), "%i/%i/%i", month, day, year);
     661             : 
     662          12 :         RETURN_STRING(date);
     663             : }
     664             : /* }}} */
     665             : 
     666             : /* {{{ proto int frenchtojd(int month, int day, int year)
     667             :    Converts a french republic calendar date to julian day count */
     668           4 : PHP_FUNCTION(frenchtojd)
     669             : {
     670             :         zend_long year, month, day;
     671             : 
     672           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &month, &day, &year) == FAILURE) {
     673           0 :                 RETURN_FALSE;
     674             :         }
     675             : 
     676           4 :         RETURN_LONG(FrenchToSdn(year, month, day));
     677             : }
     678             : /* }}} */
     679             : 
     680             : /* {{{ proto mixed jddayofweek(int juliandaycount [, int mode])
     681             :    Returns name or number of day of week from julian day count */
     682          96 : PHP_FUNCTION(jddayofweek)
     683             : {
     684          96 :         zend_long julday, mode = CAL_DOW_DAYNO;
     685             :         int day;
     686             :         char *daynamel, *daynames;
     687             : 
     688          96 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &julday, &mode) == FAILURE) {
     689           0 :                 RETURN_FALSE;
     690             :         }
     691             : 
     692          96 :         day = DayOfWeek(julday);
     693          96 :         daynamel = DayNameLong[day];
     694          96 :         daynames = DayNameShort[day];
     695             : 
     696          96 :         switch (mode) {
     697             :         case CAL_DOW_SHORT:
     698          64 :                 RETURN_STRING(daynamel);
     699             :                 break;
     700             :         case CAL_DOW_LONG:
     701          64 :                 RETURN_STRING(daynames);
     702             :                 break;
     703             :         case CAL_DOW_DAYNO:
     704             :         default:
     705          32 :                 RETURN_LONG(day);
     706             :                 break;
     707             :         }
     708             : }
     709             : /* }}} */
     710             : 
     711             : /* {{{ proto string jdmonthname(int juliandaycount, int mode)
     712             :    Returns name of month for julian day count */
     713         303 : PHP_FUNCTION(jdmonthname)
     714             : {
     715             :         zend_long julday, mode;
     716         303 :         char *monthname = NULL;
     717             :         int month, day, year;
     718             : 
     719         303 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &julday, &mode) == FAILURE) {
     720           6 :                 RETURN_FALSE;
     721             :         }
     722             : 
     723         297 :         switch (mode) {
     724             :         case CAL_MONTH_GREGORIAN_LONG:  /* gregorian or julian month */
     725          43 :                 SdnToGregorian(julday, &year, &month, &day);
     726          43 :                 monthname = MonthNameLong[month];
     727          43 :                 break;
     728             :         case CAL_MONTH_JULIAN_SHORT:    /* gregorian or julian month */
     729          43 :                 SdnToJulian(julday, &year, &month, &day);
     730          43 :                 monthname = MonthNameShort[month];
     731          43 :                 break;
     732             :         case CAL_MONTH_JULIAN_LONG:     /* gregorian or julian month */
     733          43 :                 SdnToJulian(julday, &year, &month, &day);
     734          43 :                 monthname = MonthNameLong[month];
     735          43 :                 break;
     736             :         case CAL_MONTH_JEWISH:          /* jewish month */
     737          43 :                 SdnToJewish(julday, &year, &month, &day);
     738          43 :                 monthname = JEWISH_MONTH_NAME(year)[month];
     739          43 :                 break;
     740             :         case CAL_MONTH_FRENCH:          /* french month */
     741          43 :                 SdnToFrench(julday, &year, &month, &day);
     742          43 :                 monthname = FrenchMonthName[month];
     743          43 :                 break;
     744             :         default:                                        /* default gregorian */
     745             :         case CAL_MONTH_GREGORIAN_SHORT: /* gregorian or julian month */
     746          82 :                 SdnToGregorian(julday, &year, &month, &day);
     747          82 :                 monthname = MonthNameShort[month];
     748             :                 break;
     749             :         }
     750             : 
     751         594 :         RETURN_STRING(monthname);
     752             : }
     753             : /* }}} */
     754             : 
     755             : /*
     756             :  * Local variables:
     757             :  * tab-width: 4
     758             :  * c-basic-offset: 4
     759             :  * End:
     760             :  * vim600: sw=4 ts=4 fdm=marker
     761             :  * vim<600: sw=4 ts=4
     762             :  */

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:13 +0000 (16 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.