sniper Mon Feb 17 01:28:07 2003 EDT Added files: /php4/ext/standard php_sunfuncs.h
Removed files: /php4/ext/standard sunfuncs.h Modified files: /php4/ext/standard basic_functions.c datetime.h datetime.c php_standard.h sunfuncs.c /php4/win32 php4dllts.dsp Log: - 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
Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.579 php4/ext/standard/basic_functions.c:1.580 --- php4/ext/standard/basic_functions.c:1.579 Sun Feb 16 17:28:00 2003 +++ php4/ext/standard/basic_functions.c Mon Feb 17 01:28:06 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.579 2003/02/16 22:28:00 momo Exp $ */ +/* $Id: basic_functions.c,v 1.580 2003/02/17 06:28:06 sniper Exp $ */ #include "php.h" #include "php_streams.h" @@ -940,9 +940,9 @@ 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() Index: php4/ext/standard/datetime.h diff -u php4/ext/standard/datetime.h:1.12 php4/ext/standard/datetime.h:1.13 --- php4/ext/standard/datetime.h:1.12 Sun Feb 16 17:28:00 2003 +++ php4/ext/standard/datetime.h Mon Feb 17 01:28:06 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: datetime.h,v 1.12 2003/02/16 22:28:00 momo Exp $ */ +/* $Id: datetime.h,v 1.13 2003/02/17 06:28:06 sniper Exp $ */ #ifndef DATETIME_H #define DATETIME_H @@ -37,7 +37,7 @@ #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 Index: php4/ext/standard/datetime.c diff -u php4/ext/standard/datetime.c:1.105 php4/ext/standard/datetime.c:1.106 --- php4/ext/standard/datetime.c:1.105 Sun Feb 16 17:28:00 2003 +++ php4/ext/standard/datetime.c Mon Feb 17 01:28:06 2003 @@ -18,9 +18,7 @@ +----------------------------------------------------------------------+ */ - -/* $Id: datetime.c,v 1.105 2003/02/16 22:28:00 momo Exp $ */ - +/* $Id: datetime.c,v 1.106 2003/02/17 06:28:06 sniper Exp $ */ #include "php.h" #include "zend_operators.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 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 @@ 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_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 @@ size += 2; break; case '\\': - if(i < Z_STRLEN_PP(format)-1) { + if (i < Z_STRLEN_PP(format) - 1) { i++; } size ++; @@ -404,9 +399,9 @@ 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 @@ 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 @@ } /* }}} */ +/* {{{ 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 @@ 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 @@ 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 @@ 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 @@ default: return 0; } - } /* }}} */ @@ -737,26 +741,27 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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); Index: php4/ext/standard/php_standard.h diff -u php4/ext/standard/php_standard.h:1.19 php4/ext/standard/php_standard.h:1.20 --- php4/ext/standard/php_standard.h:1.19 Sun Feb 16 17:28:00 2003 +++ php4/ext/standard/php_standard.h Mon Feb 17 01:28:06 2003 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_standard.h,v 1.19 2003/02/16 22:28:00 momo Exp $ */ +/* $Id: php_standard.h,v 1.20 2003/02/17 06:28:06 sniper Exp $ */ #include "basic_functions.h" #include "php_math.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); Index: php4/ext/standard/sunfuncs.c diff -u php4/ext/standard/sunfuncs.c:1.2 php4/ext/standard/sunfuncs.c:1.3 --- php4/ext/standard/sunfuncs.c:1.2 Sun Feb 16 19:01:31 2003 +++ php4/ext/standard/sunfuncs.c Mon Feb 17 01:28:06 2003 @@ -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 | + | [EMAIL PROTECTED] so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Moshe Doron <[EMAIL PROTECTED]> | + +----------------------------------------------------------------------+ +*/ + +/* $Id: sunfuncs.c,v 1.3 2003/02/17 06:28:06 sniper Exp $ */ + /* The sun position algorithm taken from the 'US Naval Observatory's Almanac for Computers', implemented by Ken Bloom <[EMAIL PROTECTED]> @@ -5,134 +25,86 @@ and finally converted to C by Moshe Doron <[EMAIL PROTECTED]>. */ -#include <assert.h> -#include <math.h> -#include <stdlib.h> #include "php.h" -#include "sunfuncs.h" +#include "php_sunfuncs.h" #include "datetime.h" #include "php_ini.h" +#include <assert.h> +#include <math.h> +#include <stdlib.h> - -/* {{{ 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; - } -} +/* {{{ macros and constants + */ +#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) /* }}} */ -/* 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 @@ /* 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_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 @@ 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 @@ } /* }}} */ - -/* 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 + */ Index: php4/win32/php4dllts.dsp diff -u php4/win32/php4dllts.dsp:1.104 php4/win32/php4dllts.dsp:1.105 --- php4/win32/php4dllts.dsp:1.104 Sun Feb 16 17:39:01 2003 +++ php4/win32/php4dllts.dsp Mon Feb 17 01:28:07 2003 @@ -1900,7 +1900,7 @@ # End Source File # Begin Source File -SOURCE=..\ext\standard\sunfuncs.h +SOURCE=..\ext\standard\php_sunfuncs.h # End Source File # Begin Source File Index: php4/ext/standard/php_sunfuncs.h +++ php4/ext/standard/php_sunfuncs.h /* +----------------------------------------------------------------------+ | 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 | | [EMAIL PROTECTED] so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Moshe Doron <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ /* $Id: php_sunfuncs.h,v 1.1 2003/02/17 06:28:06 sniper Exp $ */ #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 */
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php