2003-02-17 14:28:07 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| PHP Version 5 |
|
2003-02-17 14:28:07 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| Copyright (c) 1997-2004 The PHP Group |
|
2003-02-17 14:28:07 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2003-06-11 04:04:29 +08:00
|
|
|
| This source file is subject to version 3.0 of the PHP license, |
|
2003-02-17 14:28:07 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_0.txt. |
|
2003-02-17 14:28:07 +08:00
|
|
|
| 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 <mosdoron@netvision.net.il> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2003-02-17 06:28:01 +08:00
|
|
|
/*
|
|
|
|
The sun position algorithm taken from the 'US Naval Observatory's
|
|
|
|
Almanac for Computers', implemented by Ken Bloom <kekabloom@ucdavis.edu>
|
|
|
|
for the zmanim project <http://sourceforge.net/projects/zmanim/>
|
|
|
|
and finally converted to C by Moshe Doron <mosdoron@netvision.net.il>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "php.h"
|
2003-02-17 14:28:07 +08:00
|
|
|
#include "php_sunfuncs.h"
|
2003-02-17 06:28:01 +08:00
|
|
|
#include "datetime.h"
|
|
|
|
#include "php_ini.h"
|
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
2003-02-17 06:28:01 +08:00
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
/* {{{ macros and constants
|
|
|
|
*/
|
|
|
|
#ifndef M_PI
|
|
|
|
#define M_PI 3.14159265358979323846
|
|
|
|
#endif
|
2003-02-17 06:28:01 +08:00
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
#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)
|
2003-02-17 06:28:01 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
/* {{{ php_sunrise_sunset
|
|
|
|
returns time in UTC */
|
|
|
|
static double php_sunrise_sunset(long N, double latitude, double longitude, double zenith, int calc_sunset)
|
|
|
|
{
|
2003-02-17 06:28:01 +08:00
|
|
|
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
|
2003-02-17 14:28:07 +08:00
|
|
|
int N = theday - date(1, 1, theday.year()) + 1;
|
2003-02-17 06:28:01 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* step 2: convert the longitude to hour value and calculate an approximate time */
|
2003-02-17 14:28:07 +08:00
|
|
|
lngHour = longitude / 15;
|
2003-02-17 06:28:01 +08:00
|
|
|
|
|
|
|
/* use 18 for sunset instead of 6 */
|
2003-02-17 14:28:07 +08:00
|
|
|
if (calc_sunset) {
|
|
|
|
t = (double) N + ((18 - lngHour) / 24); /* Sunset */
|
|
|
|
} else {
|
|
|
|
t = (double) N + ((6 - lngHour) / 24); /* Sunrise */
|
|
|
|
}
|
|
|
|
|
2003-02-17 06:28:01 +08:00
|
|
|
/* 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;
|
2003-02-17 14:28:07 +08:00
|
|
|
|
|
|
|
while (L < 0) {
|
|
|
|
Lx = L + 360;
|
2003-02-17 06:28:01 +08:00
|
|
|
assert (Lx != L); /* askingtheguru: realy needed? */
|
|
|
|
L = Lx;
|
|
|
|
}
|
2003-02-17 14:28:07 +08:00
|
|
|
|
|
|
|
while (L >= 360) {
|
|
|
|
Lx = L - 360;
|
2003-02-17 06:28:01 +08:00
|
|
|
assert (Lx != L); /* askingtheguru: realy needed? */
|
|
|
|
L = Lx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* step 5a: calculate the sun's right ascension */
|
2003-02-17 14:28:07 +08:00
|
|
|
RA = to_deg(atan(0.91764 * tan(to_rad(L))));
|
2003-02-17 06:28:01 +08:00
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
while (RA < 0) {
|
|
|
|
RAx = RA + 360;
|
2003-02-17 06:28:01 +08:00
|
|
|
assert (RAx != RA); /* askingtheguru: realy needed? */
|
|
|
|
RA = RAx;
|
|
|
|
}
|
2003-02-17 14:28:07 +08:00
|
|
|
|
|
|
|
while (RA >= 360) {
|
|
|
|
RAx = RA - 360;
|
2003-02-17 06:28:01 +08:00
|
|
|
assert (RAx != RA); /* askingtheguru: realy needed? */
|
|
|
|
RA = RAx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* step 5b: right ascension value needs to be in the same quadrant as L */
|
2003-02-17 14:28:07 +08:00
|
|
|
Lquadrant = floor(L / 90) * 90;
|
|
|
|
RAquadrant = floor(RA / 90) * 90;
|
|
|
|
RA = RA + (Lquadrant - RAquadrant);
|
2003-02-17 06:28:01 +08:00
|
|
|
|
|
|
|
/* step 5c: right ascension value needs to be converted into hours */
|
2003-02-17 14:28:07 +08:00
|
|
|
RA /= 15;
|
2003-02-17 06:28:01 +08:00
|
|
|
|
|
|
|
/* 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)));
|
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
/* XXX: What's the use of this block.. ?
|
|
|
|
* if (!calc_sunset && cosH > 1 || calc_sunset && cosH < -1) {
|
|
|
|
* throw doesnthappen();
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
2003-02-17 06:28:01 +08:00
|
|
|
/* step 7b: finish calculating H and convert into hours */
|
2003-02-17 14:28:07 +08:00
|
|
|
if (calc_sunset) {
|
|
|
|
H = to_deg(acos(cosH)); /* Sunset */
|
|
|
|
} else {
|
|
|
|
H = 360 - to_deg(acos(cosH)); /* Sunrise */
|
|
|
|
}
|
|
|
|
H = H / 15;
|
2003-02-17 06:28:01 +08:00
|
|
|
|
|
|
|
/* step 8: calculate local mean time */
|
|
|
|
T = H + RA - (0.06571 * t) - 6.622;
|
|
|
|
|
|
|
|
/* step 9: convert to UTC */
|
|
|
|
UT = T - lngHour;
|
2003-02-17 14:28:07 +08:00
|
|
|
|
|
|
|
while (UT < 0) {
|
|
|
|
UTx = UT + 24;
|
2003-02-17 06:28:01 +08:00
|
|
|
assert (UTx != UT); /* askingtheguru: realy needed? */
|
|
|
|
UT = UTx;
|
|
|
|
}
|
2003-02-17 14:28:07 +08:00
|
|
|
|
|
|
|
while (UT >= 24) {
|
|
|
|
UTx = UT - 24;
|
2003-02-17 06:28:01 +08:00
|
|
|
assert (UTx != UT); /* askingtheguru: realy needed? */
|
|
|
|
UT = UTx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UT;
|
|
|
|
}
|
2003-02-17 14:28:07 +08:00
|
|
|
/* }}} */
|
2003-02-17 06:28:01 +08:00
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
/* {{{ 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)
|
2003-02-17 06:28:01 +08:00
|
|
|
{
|
|
|
|
zval *date;
|
|
|
|
double latitude, longitude, zenith, gmt_offset, ret;
|
2004-02-26 04:16:27 +08:00
|
|
|
int time, N;
|
|
|
|
long retformat;
|
2003-02-17 06:28:01 +08:00
|
|
|
char retstr[6];
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ldddd", &date, &retformat, &latitude, &longitude, &zenith, &gmt_offset) == FAILURE) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
switch (Z_TYPE_P(date)) {
|
2003-02-17 06:28:01 +08:00
|
|
|
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;
|
|
|
|
}
|
2003-02-17 14:28:07 +08:00
|
|
|
|
|
|
|
N = php_idate('z', time, 0) + 1;
|
2003-02-17 06:28:01 +08:00
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
switch (ZEND_NUM_ARGS()) {
|
2003-02-17 06:28:01 +08:00
|
|
|
case 1:
|
|
|
|
retformat = SUNFUNCS_RET_STRING;
|
|
|
|
case 2:
|
|
|
|
latitude = INI_FLT("date.default_latitude");
|
|
|
|
case 3:
|
|
|
|
longitude = INI_FLT("date.default_longitude");
|
|
|
|
case 4:
|
2003-02-17 14:28:07 +08:00
|
|
|
if (calc_sunset) {
|
|
|
|
zenith = INI_FLT("date.sunset_zenith");
|
|
|
|
} else {
|
|
|
|
zenith = INI_FLT("date.sunrise_zenith");
|
|
|
|
}
|
2003-02-17 06:28:01 +08:00
|
|
|
case 5:
|
2003-02-17 14:28:07 +08:00
|
|
|
gmt_offset = php_idate('Z', time, 0) / 3600;
|
2004-08-04 08:28:03 +08:00
|
|
|
case 6:
|
2004-07-21 08:14:48 +08:00
|
|
|
break;
|
2003-02-17 06:28:01 +08:00
|
|
|
default:
|
2004-07-21 08:14:48 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid format");
|
|
|
|
RETURN_FALSE;
|
2003-02-17 06:28:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
ret = php_sunrise_sunset(N, latitude, longitude, zenith, calc_sunset) + gmt_offset;
|
2003-02-17 06:28:01 +08:00
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
switch (retformat) {
|
2003-02-17 06:28:01 +08:00
|
|
|
case SUNFUNCS_RET_TIMESTAMP:
|
2003-02-17 14:28:07 +08:00
|
|
|
RETURN_LONG((int) (time - (time % (24 * 3600))) + (int) (60 * ret));
|
2003-02-17 06:28:01 +08:00
|
|
|
break;
|
|
|
|
case SUNFUNCS_RET_STRING:
|
2003-02-17 14:28:07 +08:00
|
|
|
N = (int) ret;
|
|
|
|
sprintf(retstr, "%02d:%02d", N, (int) (60 * (ret - (double) N)));
|
2003-02-17 06:28:01 +08:00
|
|
|
RETVAL_STRINGL(retstr, 5, 1);
|
|
|
|
break;
|
|
|
|
case SUNFUNCS_RET_DOUBLE:
|
|
|
|
RETURN_DOUBLE(ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
/* {{{ proto mixed date_sunrise(mixed time [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]])
|
2004-09-05 16:13:56 +08:00
|
|
|
Returns time of sunrise for a given day and location */
|
2003-02-17 14:28:07 +08:00
|
|
|
PHP_FUNCTION(date_sunrise)
|
2003-02-17 06:28:01 +08:00
|
|
|
{
|
2003-02-17 14:28:07 +08:00
|
|
|
php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
|
|
|
|
}
|
|
|
|
/* }}} */
|
2003-02-17 06:28:01 +08:00
|
|
|
|
2003-02-17 14:28:07 +08:00
|
|
|
/* {{{ proto mixed date_sunset(mixed time [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]])
|
2004-09-05 16:13:56 +08:00
|
|
|
Returns time of sunset for a given day and location */
|
2003-02-17 14:28:07 +08:00
|
|
|
PHP_FUNCTION(date_sunset)
|
|
|
|
{
|
|
|
|
php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
|
2003-02-17 06:28:01 +08:00
|
|
|
}
|
2003-02-17 14:28:07 +08:00
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
|
|
|
*/
|