예제 #1
0
void calculate_sun(
	double latitude,
	double longitude,
	struct tm *current_time,
	double *sunrise,
	double *sunset) {

	float JD = calcJD(current_time->tm_year, current_time->tm_mon, current_time->tm_mday);

	if (sunrise)
		*sunrise = calcSunriseUTC(JD, latitude, longitude);
	if (sunset)
		*sunset = calcSunsetUTC(JD, latitude, longitude);
}
예제 #2
0
bool GetSunriseSunset(time_t &tSunrise,time_t &tSunset,time_t &tSunriseTomorrow,time_t &tSunsetTomorrow,float latitude,float longitude)
{
    longitude*=-1; // For some reason this is reversed from all the databases I found
    time_t rawtime;
    tm ptm;
    time ( &rawtime );
    gmtime_r ( &rawtime, &ptm );
    float JD=calcJD(ptm.tm_year+1900,ptm.tm_mon+1,ptm.tm_mday);

    time_t seconds;
    time_t tseconds;
    struct tm  tm;

    tm.tm_year= ptm.tm_year;
    tm.tm_mon=ptm.tm_mon;  /* Jan = 0, Feb = 1,.. Dec = 11 */
    tm.tm_mday=ptm.tm_mday;
    tm.tm_hour=0;
    tm.tm_min=0;
    tm.tm_sec=0;
    tm.tm_isdst=-1;  

    seconds = mktime(&tm);
    //int dst=tm.tm_isdst;

    gmtime_r ( &seconds, &ptm );
    int delta= ptm.tm_hour;

    tseconds= seconds;
    seconds= seconds + calcSunriseUTC( JD,  latitude,  longitude)*60;
    tSunrise = seconds - delta*3600;

    seconds=tseconds;
    seconds+=calcSunsetUTC( JD,  latitude,  longitude)*60;
    tSunset= seconds - delta*3600;

    // Don't know why sometimes this returns yesterday's sunrise/sunset
    if( tSunrise<rawtime && tSunset<rawtime )
    {
        tseconds += 86400;
        JD+=1;

        seconds=tseconds;
        seconds= seconds + calcSunriseUTC( JD,  latitude,  longitude)*60;
        tSunrise = seconds - delta*3600;

        seconds=tseconds;
        seconds+=calcSunsetUTC( JD,  latitude,  longitude)*60;
        tSunset= seconds - delta*3600;
    }
    // Or doesn't return the next sunrise/sunset if we're close
    else if( tSunrise>rawtime && tSunset>rawtime )
    {
        tseconds -= 86400;
        JD-=1;

        seconds=tseconds;
        seconds= seconds + calcSunriseUTC( JD,  latitude,  longitude)*60;
        tSunrise = seconds - delta*3600;

        seconds=tseconds;
        seconds+=calcSunsetUTC( JD,  latitude,  longitude)*60;
        tSunset= seconds - delta*3600;
    }

    seconds=tseconds+86400; // 1 day forward
    seconds= seconds + calcSunriseUTC( JD+1,  latitude,  longitude)*60;
    tSunriseTomorrow = seconds - delta*3600;

    seconds=tseconds+86400; // 1 day forward
    seconds+=calcSunsetUTC( JD+1,  latitude,  longitude)*60;
    tSunsetTomorrow= seconds - delta*3600;

    return true;

}