From: foobar Date: Mon, 17 Feb 2003 06:28:07 +0000 (+0000) Subject: - Cleaned this mess a bit: X-Git-Tag: RELEASE_0_5~947 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7e9fb9f21f2a993197b6b88bb2ea705f4fcc9c2a;p=php - Cleaned this mess a bit: . Centralized the functions sunrise/sunset to php_sunrise_sunset to lessen duplicate code. . Made wrapper function php_do_date_sunrise_sunset() to lessen duplicate code. . Coding style fixes. . renamed sunfuncs.h -> php_sunfuncs.h --- diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 3c2c567f3a..1325930511 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -940,9 +940,9 @@ PHP_INI_BEGIN() PHP_INI_ENTRY_EX("safe_mode_protected_env_vars", SAFE_MODE_PROTECTED_ENV_VARS, PHP_INI_SYSTEM, OnUpdateSafeModeProtectedEnvVars, NULL) PHP_INI_ENTRY_EX("safe_mode_allowed_env_vars", SAFE_MODE_ALLOWED_ENV_VARS, PHP_INI_SYSTEM, OnUpdateSafeModeAllowedEnvVars, NULL) PHP_INI_ENTRY("date.default_latitude", DATE_DEFAULT_LATITUDE, PHP_INI_ALL, NULL) - PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL) - PHP_INI_ENTRY("date.sunset_zenith",DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL) - PHP_INI_ENTRY("date.sunrise_zenith",DATE_SUNRISE_ZENITH, PHP_INI_ALL, NULL) + PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL) + PHP_INI_ENTRY("date.sunset_zenith",DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL) + PHP_INI_ENTRY("date.sunrise_zenith",DATE_SUNRISE_ZENITH, PHP_INI_ALL, NULL) PHP_INI_END() diff --git a/ext/standard/datetime.c b/ext/standard/datetime.c index 3727a0e712..cb1b04c523 100644 --- a/ext/standard/datetime.c +++ b/ext/standard/datetime.c @@ -18,10 +18,8 @@ +----------------------------------------------------------------------+ */ - /* $Id$ */ - #include "php.h" #include "zend_operators.h" #include "datetime.h" @@ -35,22 +33,21 @@ #include "php_parsedate.h" -char *mon_full_names[] = -{ +char *mon_full_names[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; -char *mon_short_names[] = -{ + +char *mon_short_names[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -char *day_full_names[] = -{ + +char *day_full_names[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; -char *day_short_names[] = -{ + +char *day_short_names[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; @@ -62,13 +59,12 @@ extern time_t timezone; extern int daylight; #endif -static int phpday_tab[2][12] = -{ +static int phpday_tab[2][12] = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; -#define isleap(year) (((year%4) == 0 && (year%100)!=0) || (year%400)==0) +#define isleap(year) (((year % 4) == 0 && (year % 100) != 0) || (year % 400)==0) #define YEAR_BASE 1900 /* {{{ proto int time(void) @@ -217,7 +213,7 @@ void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gm) t1 = *localtime(&t); t2 = *localtime(&seconds); - if(t1.tm_isdst != t2.tm_isdst) { + if (t1.tm_isdst != t2.tm_isdst) { seconds += (t1.tm_isdst == 1) ? 3600 : -3600; ta = localtime(&seconds); } @@ -268,8 +264,7 @@ PHP_FUNCTION(gmmktime) /* {{{ php_date */ -static void -php_date(INTERNAL_FUNCTION_PARAMETERS, int gm) +static void php_date(INTERNAL_FUNCTION_PARAMETERS, int gm) { pval **format, **timestamp; time_t the_time; @@ -384,7 +379,7 @@ php_date(INTERNAL_FUNCTION_PARAMETERS, int gm) size += 2; break; case '\\': - if(i < Z_STRLEN_PP(format)-1) { + if (i < Z_STRLEN_PP(format) - 1) { i++; } size ++; @@ -404,9 +399,9 @@ php_date(INTERNAL_FUNCTION_PARAMETERS, int gm) for (i = 0; i < Z_STRLEN_PP(format); i++) { switch (Z_STRVAL_PP(format)[i]) { case '\\': - if(i < Z_STRLEN_PP(format)-1) { + if (i < Z_STRLEN_PP(format) - 1) { char ch[2]; - ch[0]=Z_STRVAL_PP(format)[i+1]; + ch[0]=Z_STRVAL_PP(format)[i + 1]; ch[1]='\0'; strcat(Z_STRVAL_P(return_value), ch); i++; @@ -588,7 +583,7 @@ php_date(INTERNAL_FUNCTION_PARAMETERS, int gm) strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'W': /* ISO-8601 week number of year, weeks starting on Monday */ - wd = ta->tm_wday==0 ? 6 : ta->tm_wday-1;/* weekday */ + wd = ta->tm_wday == 0 ? 6 : ta->tm_wday - 1; /* weekday */ yd = ta->tm_yday + 1; /* days since January 1st */ fd = (7 + wd - yd % 7+ 1) % 7; /* weekday (1st January) */ @@ -630,9 +625,17 @@ PHP_FUNCTION(date) } /* }}} */ +/* {{{ proto string gmdate(string format [, int timestamp]) + Format a GMT/UTC date/time */ +PHP_FUNCTION(gmdate) +{ + php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); +} +/* }}} */ -/* {{{ idate */ -int idate(char format, int timestamp, int gm) +/* {{{ php_idate + */ +int php_idate(char format, int timestamp, int gm) { time_t the_time; struct tm *ta, tmbuf; @@ -669,7 +672,7 @@ int idate(char format, int timestamp, int gm) case 'z': /* day (of the year) */ return ta->tm_yday; case 'y': /* year, numeric, 2 digits */ - return (ta->tm_year)%100; + return (ta->tm_year) % 100; case 'm': /* month, numeric */ case 'n': return ta->tm_mon + 1; @@ -681,27 +684,29 @@ int idate(char format, int timestamp, int gm) return ta->tm_hour; case 'h': /* hour, numeric, 12 hour format */ case 'g': - h = ta->tm_hour % 12; if (h==0) h = 12; + h = ta->tm_hour % 12; + if (h == 0) { + h = 12; + } return h; case 'i': /* minutes, numeric */ return ta->tm_min; case 's': /* seconds, numeric */ return ta->tm_sec; case 't': /* days in current month */ - return phpday_tab[isleap((ta->tm_year+YEAR_BASE))][ta->tm_mon]; + return phpday_tab[isleap((ta->tm_year + YEAR_BASE))][ta->tm_mon]; case 'w': /* day of the week, numeric EXTENSION */ return ta->tm_wday; case 'Z': /* timezone offset in seconds */ #if HAVE_TM_GMTOFF return ta->tm_gmtoff; #else - return ta->tm_isdst ? -(tzone- 3600) : -tzone; + return ta->tm_isdst ? -(tzone - 3600) : -tzone; #endif case 'L': /* boolean for leapyear */ - return isleap(ta->tm_year+YEAR_BASE)?1:0; + return isleap(ta->tm_year + YEAR_BASE) ? 1 : 0; case 'B': /* Swatch Beat a.k.a. Internet Time */ - beat = (((((long)the_time)-(((long)the_time) - - ((((long)the_time) % 86400) + 3600))) * 10) / 864); + beat = (((((long)the_time) - (((long)the_time) - ((((long)the_time) % 86400) + 3600))) * 10) / 864); while (beat < 0) { beat += 1000; } @@ -710,14 +715,14 @@ int idate(char format, int timestamp, int gm) case 'I': return ta->tm_isdst; case 'W': /* ISO-8601 week number of year, weeks starting on Monday */ - wd = ta->tm_wday==0 ? 6 : ta->tm_wday-1;/* weekday */ + wd = (ta->tm_wday == 0) ? 6 : ta->tm_wday - 1; /* weekday */ yd = ta->tm_yday + 1; /* days since January 1st */ - fd = (7 + wd - yd % 7+ 1) % 7; /* weekday (1st January) */ /* week is a last year week (52 or 53) */ - if ((yd <= 7 - fd) && fd > 3){ + fd = (7 + wd - yd % 7+ 1) % 7; /* weekday (1st January) */ + if ((yd <= 7 - fd) && fd > 3) { /* week is a last year week (52 or 53) */ wk = (fd == 4 || (fd == 5 && isleap((ta->tm_year + YEAR_BASE - 1)))) ? 53 : 52; } /* week is a next year week (1) */ - else if (isleap((ta->tm_year+YEAR_BASE)) + 365 - yd < 3 - wd){ + else if (isleap((ta->tm_year + YEAR_BASE)) + 365 - yd < 3 - wd) { wk = 1; } /* normal week */ @@ -729,7 +734,6 @@ int idate(char format, int timestamp, int gm) default: return 0; } - } /* }}} */ @@ -737,26 +741,27 @@ int idate(char format, int timestamp, int gm) Format a local time/date as integer */ PHP_FUNCTION(idate) { - pval **format, **timestamp; + zval **format, **timestamp; int t, ret; - switch(ZEND_NUM_ARGS()) { - case 1: - if (zend_get_parameters_ex(1, &format) == FAILURE) { - WRONG_PARAM_COUNT; - } - t = time(NULL); - break; - case 2: - if (zend_get_parameters_ex(2, &format, ×tamp) == FAILURE) { + switch (ZEND_NUM_ARGS()) { + case 1: + if (zend_get_parameters_ex(1, &format) == FAILURE) { + WRONG_PARAM_COUNT; + } + t = time(NULL); + break; + case 2: + if (zend_get_parameters_ex(2, &format, ×tamp) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_long_ex(timestamp); + t = Z_LVAL_PP(timestamp); + break; + default: WRONG_PARAM_COUNT; - } - convert_to_long_ex(timestamp); - t = Z_LVAL_PP(timestamp); - break; - default: - WRONG_PARAM_COUNT; } + convert_to_string_ex(format); if (Z_STRLEN_PP(format) != 1) { @@ -764,20 +769,11 @@ PHP_FUNCTION(idate) RETURN_FALSE; } - ret = idate(Z_STRVAL_PP(format)[0], t, 0); + ret = php_idate(Z_STRVAL_PP(format)[0], t, 0); RETURN_LONG(ret); } /* }}} */ - -/* {{{ proto string gmdate(string format [, int timestamp]) - Format a GMT/UTC date/time */ -PHP_FUNCTION(gmdate) -{ - php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); -} -/* }}} */ - /* {{{ proto array localtime([int timestamp [, bool associative_array]]) Returns the results of the C system call localtime as an associative array if the associative_array argument is set to 1 other wise it is a regular array */ PHP_FUNCTION(localtime) @@ -898,18 +894,18 @@ char *php_std_date(time_t t) day_short_names[tm1->tm_wday], tm1->tm_mday, mon_short_names[tm1->tm_mon], - tm1->tm_year+1900, + tm1->tm_year + 1900, tm1->tm_hour, tm1->tm_min, tm1->tm_sec); } else { snprintf(str, 80, "%s, %02d-%s-%02d %02d:%02d:%02d GMT", day_short_names[tm1->tm_wday], tm1->tm_mday, mon_short_names[tm1->tm_mon], - ((tm1->tm_year)%100), + ((tm1->tm_year) % 100), tm1->tm_hour, tm1->tm_min, tm1->tm_sec); } - str[79]=0; + str[79] = 0; return (str); } /* }}} */ @@ -926,9 +922,9 @@ PHP_FUNCTION(checkdate) WRONG_PARAM_COUNT; } - if(Z_TYPE_PP(year) == IS_STRING) { + if (Z_TYPE_PP(year) == IS_STRING) { res = is_numeric_string(Z_STRVAL_PP(year), Z_STRLEN_PP(year), NULL, NULL, 0); - if(res!=IS_LONG && res !=IS_DOUBLE) { + if (res != IS_LONG && res != IS_DOUBLE) { RETURN_FALSE; } } @@ -1001,11 +997,13 @@ void _php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gm) while ((real_len=strftime(buf, buf_len, format, ta))==buf_len || real_len==0) { buf_len *= 2; buf = (char *) erealloc(buf, buf_len); - if(!--max_reallocs) break; + if (!--max_reallocs) { + break; + } } - if(real_len && real_len != buf_len) { - buf = (char *) erealloc(buf, real_len+1); + if (real_len && real_len != buf_len) { + buf = (char *) erealloc(buf, real_len + 1); RETURN_STRINGL(buf, real_len, 0); } efree(buf); diff --git a/ext/standard/datetime.h b/ext/standard/datetime.h index 08feba26a8..8aef601797 100644 --- a/ext/standard/datetime.h +++ b/ext/standard/datetime.h @@ -37,7 +37,7 @@ PHP_FUNCTION(gmstrftime); #endif PHP_FUNCTION(strtotime); -int idate(char format, int timestamp, int gm); +int php_idate(char format, int timestamp, int gm); extern char *php_std_date(time_t t); void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gm); #if HAVE_STRFTIME diff --git a/ext/standard/php_standard.h b/ext/standard/php_standard.h index 230e23135a..aaa52bd6cb 100644 --- a/ext/standard/php_standard.h +++ b/ext/standard/php_standard.h @@ -60,7 +60,7 @@ #include "php_ftok.h" #include "php_type.h" #include "aggregation.h" -#include "sunfuncs.h" +#include "php_sunfuncs.h" #define phpext_standard_ptr basic_functions_module_ptr PHP_MINIT_FUNCTION(standard_filters); diff --git a/ext/standard/php_sunfuncs.h b/ext/standard/php_sunfuncs.h new file mode 100644 index 0000000000..bb79c58094 --- /dev/null +++ b/ext/standard/php_sunfuncs.h @@ -0,0 +1,42 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 4 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2003 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.02 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_02.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Moshe Doron | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifndef PHP_SUNFUNCS_H +#define PHP_SUNFUNCS_H + +/* default ini entries: */ +/* Jerusalem one. */ +#define DATE_DEFAULT_LATITUDE "31.7667" +#define DATE_DEFAULT_LONGITUDE "35.2333" + +/* on 90'50; common jewish sunset declaration (start of sun body appear) */ +#define DATE_SUNSET_ZENITH "90.83" + +/* on 90'50; common jewish sunrise declaration (sun body disappeared) */ +#define DATE_SUNRISE_ZENITH "90.83" + +#define SUNFUNCS_RET_TIMESTAMP 0 +#define SUNFUNCS_RET_STRING 1 +#define SUNFUNCS_RET_DOUBLE 2 + +PHP_FUNCTION(date_sunrise); +PHP_FUNCTION(date_sunset); + +#endif /* PHP_SUNFUNCS_H */ diff --git a/ext/standard/sunfuncs.c b/ext/standard/sunfuncs.c index 8fea247940..58bab3258e 100644 --- a/ext/standard/sunfuncs.c +++ b/ext/standard/sunfuncs.c @@ -1,3 +1,23 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 4 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2003 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 2.02 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available at through the world-wide-web at | + | http://www.php.net/license/2_02.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Moshe Doron | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + /* The sun position algorithm taken from the 'US Naval Observatory's Almanac for Computers', implemented by Ken Bloom @@ -5,134 +25,86 @@ and finally converted to C by Moshe Doron . */ -#include -#include -#include #include "php.h" -#include "sunfuncs.h" +#include "php_sunfuncs.h" #include "datetime.h" #include "php_ini.h" +#include +#include +#include +/* {{{ macros and constants + */ +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif -/* {{{ proto mixed sunrise(mixed time[, int format][, float latitude][, float longitude][, float zenith,][ float gmt_offset]) - Returns time of sunrise for a given day & location */ -PHP_FUNCTION(date_sunrise) -{ - zval *date; - double latitude, longitude, zenith, gmt_offset, ret; - int time, N, retformat; - char retstr[6]; - - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ldddd", &date, &retformat, &latitude, &longitude, &zenith, &gmt_offset) == FAILURE) { - RETURN_FALSE; - } - - switch(Z_TYPE_P(date)){ - case IS_LONG: - time = Z_LVAL_P(date); - break; - case IS_STRING: - /* todo: more user friendly format */ - default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "date must be timestamp for now"); - RETURN_FALSE; - } - N = idate('z',time,0)+1; - - switch(ZEND_NUM_ARGS()){ - case 1: - retformat = SUNFUNCS_RET_STRING; - case 2: - latitude = INI_FLT("date.default_latitude"); - case 3: - longitude = INI_FLT("date.default_longitude"); - case 4: - zenith = INI_FLT("date.sunrise_zenith"); - case 5: - gmt_offset = idate('Z',time,0)/3600; - default: - break; - } - - ret = sunrise(N, latitude, longitude, zenith) + gmt_offset; - - switch(retformat){ - case SUNFUNCS_RET_TIMESTAMP: - RETURN_LONG((int)(time-(time%(24*3600)))+(int)(60*ret)); - break; - case SUNFUNCS_RET_STRING: - N = (int)ret; - sprintf(retstr, "%02d:%02d", N,(int)(60*(ret-(double)N))); - RETVAL_STRINGL(retstr, 5, 1); - break; - case SUNFUNCS_RET_DOUBLE: - RETURN_DOUBLE(ret); - break; - default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid format"); - RETURN_FALSE; - } -} +#define to_rad(degrees) (degrees * M_PI / 180) +#define to_rad_with_min(degrees) (degrees + minutes / 60) +#define to_deg(rad) (rad * 180 / M_PI) /* }}} */ -/* returns time in UTC */ -double sunrise(long N, double latitude, double longitude, double zenith) { - +/* {{{ php_sunrise_sunset + returns time in UTC */ +static double php_sunrise_sunset(long N, double latitude, double longitude, double zenith, int calc_sunset) +{ double lngHour, t, M, L, Lx, RA, RAx, Lquadrant, RAquadrant, sinDec, cosDec, cosH, H, T, UT, UTx; /* step 1: First calculate the day of the year - int N=theday - date(1,1,theday.year()) + 1; + int N = theday - date(1, 1, theday.year()) + 1; */ /* step 2: convert the longitude to hour value and calculate an approximate time */ - lngHour= longitude / 15; + lngHour = longitude / 15; /* use 18 for sunset instead of 6 */ - t = (double)N + ((6 - lngHour) / 24); - + if (calc_sunset) { + t = (double) N + ((18 - lngHour) / 24); /* Sunset */ + } else { + t = (double) N + ((6 - lngHour) / 24); /* Sunrise */ + } + /* step 3: calculate the sun's mean anomaly */ M = (0.9856 * t) - 3.289; /* step 4: calculate the sun's true longitude */ L = M + (1.916 * sin(to_rad(M))) + (0.020 * sin (to_rad(2 * M))) + 282.634; - while (L<0) - { - Lx=L+360; + + while (L < 0) { + Lx = L + 360; assert (Lx != L); /* askingtheguru: realy needed? */ L = Lx; } - while (L>=360) - { - Lx=L-360; + + while (L >= 360) { + Lx = L - 360; assert (Lx != L); /* askingtheguru: realy needed? */ L = Lx; } /* step 5a: calculate the sun's right ascension */ - RA = to_deg(atan (0.91764 * tan(to_rad(L)))); + RA = to_deg(atan(0.91764 * tan(to_rad(L)))); - while (RA<0) - { - RAx=RA+360; + while (RA < 0) { + RAx = RA + 360; assert (RAx != RA); /* askingtheguru: realy needed? */ RA = RAx; } - while (RA>=360) - { - RAx=RA-360; + + while (RA >= 360) { + RAx = RA - 360; assert (RAx != RA); /* askingtheguru: realy needed? */ RA = RAx; } /* step 5b: right ascension value needs to be in the same quadrant as L */ - Lquadrant = floor(L/90)*90; - RAquadrant = floor(RA/90)*90; - RA=RA+(Lquadrant - RAquadrant); + Lquadrant = floor(L / 90) * 90; + RAquadrant = floor(RA / 90) * 90; + RA = RA + (Lquadrant - RAquadrant); /* step 5c: right ascension value needs to be converted into hours */ - RA/=15; + RA /= 15; /* step 6: calculate the sun's declination */ sinDec = 0.39782 * sin(to_rad(L)); @@ -141,56 +113,57 @@ double sunrise(long N, double latitude, double longitude, double zenith) { /* step 7a: calculate the sun's local hour angle */ cosH = (cos(to_rad(zenith)) - (sinDec * sin(to_rad(latitude)))) / (cosDec * cos(to_rad(latitude))); - if (cosH > 1){ - /* throw doesnthappen(); */ - } - - /* - FOR SUNSET use the following instead of the above if statement. - // if (cosH < -1) - */ - + /* XXX: What's the use of this block.. ? + * if (!calc_sunset && cosH > 1 || calc_sunset && cosH < -1) { + * throw doesnthappen(); + * } + */ + /* step 7b: finish calculating H and convert into hours */ - H = 360 - to_deg(acos(cosH)); - - /* FOR SUNSET remove "360 - " from the above */ - - H=H/15; + if (calc_sunset) { + H = to_deg(acos(cosH)); /* Sunset */ + } else { + H = 360 - to_deg(acos(cosH)); /* Sunrise */ + } + H = H / 15; /* step 8: calculate local mean time */ T = H + RA - (0.06571 * t) - 6.622; /* step 9: convert to UTC */ UT = T - lngHour; - while (UT<0) { - UTx=UT+24; + + while (UT < 0) { + UTx = UT + 24; assert (UTx != UT); /* askingtheguru: realy needed? */ UT = UTx; } - while (UT>=24) { - UTx=UT-24; + + while (UT >= 24) { + UTx = UT - 24; assert (UTx != UT); /* askingtheguru: realy needed? */ UT = UTx; } return UT; } +/* }}} */ -/* {{{ proto mixed sunset(mixed time[, int format][, float latitude][, float longitude][, float zenith,][ float gmt_offset]) - Returns time of sunset for a given day & location */ -PHP_FUNCTION(date_sunset) +/* {{{ php_do_date_sunrise_sunset + * Common for date_sunrise() and date_sunset() functions + */ +static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, int calc_sunset) { zval *date; double latitude, longitude, zenith, gmt_offset, ret; int time, N, retformat; char retstr[6]; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ldddd", &date, &retformat, &latitude, &longitude, &zenith, &gmt_offset) == FAILURE) { RETURN_FALSE; } - switch(Z_TYPE_P(date)){ + switch (Z_TYPE_P(date)) { case IS_LONG: time = Z_LVAL_P(date); break; @@ -200,9 +173,10 @@ PHP_FUNCTION(date_sunset) php_error_docref(NULL TSRMLS_CC, E_WARNING, "date must be timestamp for now"); RETURN_FALSE; } - N = idate('z',time,0)+1; + + N = php_idate('z', time, 0) + 1; - switch(ZEND_NUM_ARGS()){ + switch (ZEND_NUM_ARGS()) { case 1: retformat = SUNFUNCS_RET_STRING; case 2: @@ -210,22 +184,26 @@ PHP_FUNCTION(date_sunset) case 3: longitude = INI_FLT("date.default_longitude"); case 4: - zenith = INI_FLT("date.sunset_zenith"); + if (calc_sunset) { + zenith = INI_FLT("date.sunset_zenith"); + } else { + zenith = INI_FLT("date.sunrise_zenith"); + } case 5: - gmt_offset = idate('Z',time,0)/3600; + gmt_offset = php_idate('Z', time, 0) / 3600; default: break; } - ret = sunset(N, latitude, longitude, zenith) + gmt_offset; + ret = php_sunrise_sunset(N, latitude, longitude, zenith, calc_sunset) + gmt_offset; - switch(retformat){ + switch (retformat) { case SUNFUNCS_RET_TIMESTAMP: - RETURN_LONG((int)(time-(time%(24*3600)))+(int)(60*ret)); + RETURN_LONG((int) (time - (time % (24 * 3600))) + (int) (60 * ret)); break; case SUNFUNCS_RET_STRING: - N = (int)ret; - sprintf(retstr, "%02d:%02d", N,(int)(60*(ret-(double)N))); + N = (int) ret; + sprintf(retstr, "%02d:%02d", N, (int) (60 * (ret - (double) N))); RETVAL_STRINGL(retstr, 5, 1); break; case SUNFUNCS_RET_DOUBLE: @@ -238,95 +216,27 @@ PHP_FUNCTION(date_sunset) } /* }}} */ - -/* returns time in UTC */ -double sunset(long N, double latitude, double longitude, double zenith) +/* {{{ proto mixed date_sunrise(mixed time [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]]) + Returns time of sunrise for a given day & location */ +PHP_FUNCTION(date_sunrise) { - - double lngHour, t, M, L, Lx, RA, RAx, Lquadrant, RAquadrant, sinDec, cosDec, cosH, H, T, UT, UTx; - - /* - //step 1: First calculate the day of the year - int N=theday - date(1,1,theday.year()) + 1; - */ - - /* step 2: convert the longitude to hour value and calculate an approximate time */ - lngHour= longitude / 15; - - t = (double)N + ((18 - lngHour) / 24); - - /* step 3: calculate the sun's mean anomaly */ - M = (0.9856 * t) - 3.289; - - /* step 4: calculate the sun's true longitude */ - L = M + (1.916 * sin(to_rad(M))) + (0.020 * sin (to_rad(2 * M))) + 282.634; - while (L<0) - { - Lx=L+360; - assert (Lx != L); /* askingtheguru: realy needed? */ - L = Lx; - } - while (L>=360) - { - Lx=L-360; - assert (Lx != L); /* askingtheguru: realy needed? */ - L = Lx; - } - - /* step 5a: calculate the sun's right ascension */ - RA = to_deg(atan (0.91764 * tan(to_rad(L)))); - while (RA<0) - { - RAx=RA+360; - assert (RAx != RA); /* askingtheguru: realy needed? */ - RA = RAx; - } - while (RA>=360) - { - RAx=RA-360; - assert (RAx != RA); /* askingtheguru: realy needed? */ - RA = RAx; - } - - /* step 5b: right ascension value needs to be in the same quadrant as L */ - Lquadrant = floor(L/90)*90; - RAquadrant = floor(RA/90)*90; - RA=RA+(Lquadrant - RAquadrant); - - /* step 5c: right ascension value needs to be converted into hours */ - RA/=15; - - /* step 6: calculate the sun's declination */ - sinDec = 0.39782 * sin(to_rad(L)); - cosDec = cos(asin(sinDec)); - - /* step 7a: calculate the sun's local hour angle */ - cosH = (cos(to_rad(zenith)) - (sinDec * sin(to_rad(latitude)))) / (cosDec * cos(to_rad(latitude))); - - /* - if (cosH < -1) - throw doesnthappen(); - */ - - /* step 7b: finish calculating H and convert into hours */ - H = to_deg(acos(cosH)); - H=H/15; - - /* step 8: calculate local mean time */ - T = H + RA - (0.06571 * t) - 6.622; + php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); +} +/* }}} */ - /* step 9: convert to UTC */ - UT = T - lngHour; - while (UT<0) { - UTx=UT+24; - assert (UTx != UT); /* askingtheguru: realy needed? */ - UT = UTx; - } - while (UT>=24) { - UTx=UT-24; - assert (UTx != UT); /* askingtheguru: realy needed? */ - UT = UTx; - } - - return UT; +/* {{{ proto mixed date_sunset(mixed time [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]]) + Returns time of sunset for a given day & location */ +PHP_FUNCTION(date_sunset) +{ + php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); } +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/ext/standard/sunfuncs.h b/ext/standard/sunfuncs.h deleted file mode 100644 index eb3a8cb012..0000000000 --- a/ext/standard/sunfuncs.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef SUNFUNCS_H -#define SUNFUNCS_H - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -#define to_rad(degrees) (degrees * M_PI / 180) -#define to_rad_with_min(degrees) (degrees + minutes / 60) -#define to_deg(rad) (rad * 180 / M_PI) - - -/* default ini entries: */ -/* Jerusalem one. */ -#define DATE_DEFAULT_LATITUDE "31.7667" -#define DATE_DEFAULT_LONGITUDE "35.2333" -/* on 90'50; common jewish sunset declaration (start of sun body appear) */ -#define DATE_SUNSET_ZENITH "90.83" -/* on 90'50; common jewish sunrise declaration (sun body disappeared) */ -#define DATE_SUNRISE_ZENITH "90.83" - -#define SUNFUNCS_RET_TIMESTAMP 0 -#define SUNFUNCS_RET_STRING 1 -#define SUNFUNCS_RET_DOUBLE 2 -double sunrise(long N, double latitude, double longitude, double zenith); -double sunset(long N, double latitude, double longitude, double zenith); - -PHP_FUNCTION(date_sunrise); -PHP_FUNCTION(date_sunset); - -#endif /* SUNFUNCS_H */ diff --git a/win32/php4dllts.dsp b/win32/php4dllts.dsp index f84c86f587..a9b193fea6 100644 --- a/win32/php4dllts.dsp +++ b/win32/php4dllts.dsp @@ -1900,7 +1900,7 @@ SOURCE=..\ext\standard\sha1.h # End Source File # Begin Source File -SOURCE=..\ext\standard\sunfuncs.h +SOURCE=..\ext\standard\php_sunfuncs.h # End Source File # Begin Source File