UNIX time_t

/*3:*/

#line 80 "timedate.w"

#ifndef TIMEDATE_HEADER_DEFINES

#define TIMEDATE_HEADER_DEFINES

#include <stdio.h> 

#include <math.h>        

#include <time.h> 

#include <assert.h> 

#include <string> 

#include <stdexcept> 

using namespace std;

/*6:*/

#line 117 "timedate.w"

class angle{

#define PI 3.14159265358979323846          

public:

static double Pi(void){

return PI;

}

static double Asec(double x){

return x/3600.0;

}

static double dtr(double d){

return d*(PI/180);

}

static double rtd(double r){

return r/(PI/180);

}

static double fixangle(double d){

return d-360.0*(floor(d/360.0));

}

static double fixangr(double r){

return r-(PI*2)*(floor(r/(PI*2)));

}

static double d_m_s_to_decimal(int d,int m,double s){

return d+(m/60.0)+(s/(60.0*60.0));

}

static string degrees_to_d_m_s(double a);

#undef PI

};

/*:6*//*8:*/

#line 209 "timedate.w"

class systemtime:protected angle{

private:

time_t t;

public:

static const int SecondsPerDay= 24*60*60,

SecondsPerHour= 60*60,

SecondsPerMinute= 60;

systemtime(time_t it= 0){

set_time(it);

}

systemtime(string dateTime){

fromString(dateTime);

}

time_t get_time(void){

return t;

}

void set_time(const time_t nt){

t= nt;

}

void now(void){

t= time(NULL);

}

time_t midnight(void){

return t-(t%SecondsPerDay);

}

void dayStep(int days){

t= ((t/SecondsPerDay)+days)*SecondsPerDay;

}

void nextDay(void){

dayStep(1);

}

void previousDay(void){

dayStep(-1);

}

void toUTC(int*year= NULL,int*month= NULL,int*mday= NULL,

int*hour= NULL,int*min= NULL,int*sec= NULL);

void fromUTC(int year= 1970,int month= 1,int mday= 1,

int hour= 0,int min= 0,int sec= 0);

string dateToString(void);

string timeToString(void);

void fromString(string s);

double toJulian(void);

void fromJulian(double jd);

int weekday(void){

return jwday(toJulian());

}

double siderealTime(void){

return gmst(toJulian());

}

static double utctoj(long year,int mon,int mday,

int hour= 0,int min= 0,int sec= 0);

static void jyear(double td,

int*yy= NULL,int*mm= NULL,int*dd= NULL);

static void jhms(double j,int*h= NULL,int*m= NULL,int*s= NULL);

static int jwday(double j){

return((int)(j+1.5))%7;

}

static double gmst(double jd);

static double meanObliquityOfEcliptic(double jd);

static void nutation(double jd,double*deltaPsi,double*deltaEpsilon);

static double equationTime(double jd);

static void sunpos(double jd,bool apparent,

double*ra,double*dec,double*rv,double*slong);

static void moon_and_sun(double pdate,double*phaseang,

double*pphase,double*mage,double*dist,

double*angdia,double*sudist,double*suangdia);

static double kepler(double m,double ecc);

static void moonphases(double sdate,double phases[5]);

protected:

static void store_if(int*p,const int v){

if(p!=NULL){

*p= v;

}

}

static void dstore_if(double*p,const double v){

if(p!=NULL){

*p= v;

}

}

static double meanphase(double sdate,double k);

static double truephase(double k,double phase);

};

/*:8*/

#line 92 "timedate.w"

#endif

/*:3*/

////////////////////////////////////////////////

/*2:*/

#line 67 "timedate.w"

#include "config.h"                   

#define JulianCentury 36525.0

#define JulianMillennium (JulianCentury*10) 

#define J2000 2451545.0

#define SynMonth 29.53058868 \

 \

#define NUTERMS 63 \

#line 70 "timedate.w"

/*4:*/

#line 99 "timedate.w"

#include "timedate.h"       

/*:4*/

#line 72 "timedate.w"

/*5:*/

#line 106 "timedate.w"

/*7:*/

#line 168 "timedate.w"

string angle::degrees_to_d_m_s(double a){

int dd,mm;

double ss;

char result[16];

char*sign= "";

if(a<0){

sign= "-";

a= -a;

}

dd= (int)a;

mm= (int)((a-dd)*60);

ss= (a-(dd+(mm/60.0)))*3600;

sprintf(result,"%s%d°%d'%.3f\"",sign,dd,mm,ss);

return string(result);

}

/*:7*/

#line 107 "timedate.w"

/*9:*/

#line 374 "timedate.w"

void systemtime::toUTC(int*year,int*month,int*mday,

int*hour,int*min,int*sec){

struct tm*gt= gmtime(&t);

store_if(year,gt->tm_year+1900);

store_if(month,gt->tm_mon+1);

store_if(mday,gt->tm_mday);

store_if(hour,gt->tm_hour);

store_if(min,gt->tm_min);

store_if(sec,gt->tm_sec);

}

/*:9*//*10:*/

#line 394 "timedate.w"

void systemtime::fromUTC(int year,int month,int mday,

int hour,int min,int sec){

double jd;

const double J1970= 2440587.5;

jd= utctoj(year,month,mday,hour,min,sec);

t= (time_t)(((jd-J1970)*(60*60*24))+0.5);

}

/*:10*//*11:*/

#line 409 "timedate.w"

string systemtime::dateToString(void){

int year,month,day;

char edate[16];

toUTC(&year,&month,&day);

sprintf(edate,"%04d-%02d-%02d",year,month,day);

return string(edate);

}

/*:11*//*12:*/

#line 423 "timedate.w"

string systemtime::timeToString(void){

int hour,min,sec;

char etime[16];

toUTC(NULL,NULL,NULL,&hour,&min,&sec);

sprintf(etime,"%02d:%02d:%02d",hour,min,sec);

return string(etime);

}

/*:12*//*13:*/

#line 439 "timedate.w"

void systemtime::fromString(string s)

{

int year= 1970,month= 1,mday= 1,

hour= 0,min= 0,sec= 0,n;

char d1,d2,d3,d4,d5;

n= sscanf(s.c_str(),"%d%c%d%c%d%c%d%c%d%c%d",

&year,&d1,&month,&d2,&mday,&d3,

&hour,&d4,&min,&d5,&sec);

if(n<5){

throw(invalid_argument("Invalid date format"));

}

fromUTC(year,month,mday,hour,min,sec);

}

/*:13*//*14:*/

#line 466 "timedate.w"

double systemtime::utctoj(long year,int mon,int mday,

int hour,int min,int sec)

{

int a,b,m;

long y;

mon--;

assert(mon>=0&&mon<12);

assert(mday> 0&&mday<32);

assert(hour>=0&&hour<24);

assert(min>=0&&min<60);

assert(sec>=0&&sec<60);

m= mon+1;

y= year;

if(m<=2){

y--;

m+= 12;

}

if((year<1582)||((year==1582)&&((mon<9)||(mon==9&&mday<5)))){

b= 0;

}else{

a= ((int)(y/100));

b= 2-a+(a/4);

}

return(((long)(365.25*(y+4716)))+((int)(30.6001*(m+1)))+

mday+b-1524.5)+

((sec+60L*(min+60L*hour))/86400.0);

}

/*:14*//*15:*/

#line 513 "timedate.w"

void systemtime::jyear(double td,int*yy,int*mm,int*dd)

{

double z,f,a,alpha,b,c,d,e;

td+= 0.5;

z= floor(td);

f= td-z;

if(z<2299161.0){

a= z;

}else{

alpha= floor((z-1867216.25)/36524.25);

a= z+1+alpha-floor(alpha/4);

}

b= a+1524;

c= floor((b-122.1)/365.25);

d= floor(365.25*c);

e= floor((b-d)/30.6001);

store_if(dd,(int)(b-d-floor(30.6001*e)+f));

store_if(mm,(int)((e<14)?(e-1):(e-13)));

store_if(yy,(int)((*mm> 2)?(c-4716):(c-4715)));

}

/*:15*//*16:*/

#line 544 "timedate.w"

void systemtime::jhms(double j,int*h,int*m,int*s)

{

long ij;

j+= 0.5;

ij= (long)((j-floor(j))*86400.0);

store_if(h,(int)(ij/3600L));

store_if(m,(int)((ij/60L)%60L));

store_if(s,(int)(ij%60L));

}

/*:16*//*17:*/

#line 561 "timedate.w"

double systemtime::toJulian(void){

int year,month,mday,hour,min,sec;

toUTC(&year,&month,&mday,&hour,&min,&sec);

return utctoj(year,month,mday,hour,min,sec);

}

void systemtime::fromJulian(double jd){

int year,month,mday,hour,min,sec;

jyear(jd,&year,&month,&mday);

jhms(jd,&hour,&min,&sec);

fromUTC(year,month,mday,hour,min,sec);

}

/*:17*//*18:*/

#line 586 "timedate.w"

double systemtime::gmst(double jd){

double t,theta0;

t= ((floor(jd+0.5)-0.5)-J2000)/JulianCentury;

theta0= d_m_s_to_decimal(6,41,50.54841)+

(d_m_s_to_decimal(0,0,8640184.812866)*t)+

(d_m_s_to_decimal(0,0,0.093104)*t*t)+

(d_m_s_to_decimal(0,0,0.0000062)*t*t*t);

t= (jd+0.5)-(floor(jd+0.5));

theta0+= (t*24)*1.00273790935;

theta0= (theta0-24.0*(floor(theta0/24.0)));

return theta0;

}

/*:18*//*19:*/

#line 633 "timedate.w"

double systemtime::equationTime(double jd)

{

double T,tau,eps,deltaEps,y,L0,e,M,E;

T= (jd-J2000)/JulianCentury;

tau= T/10;

eps= meanObliquityOfEcliptic(jd);

nutation(jd,NULL,&deltaEps);

y= eps+deltaEps;

y= tan(y/2);

y*= y;

L0= 280.4664567+(360007.6982779*tau)+

(0.03032028*tau*tau)+

((tau*tau*tau)/49931)-

((tau*tau*tau*tau)/15300)-

((tau*tau*tau*tau*tau)/2000000);

L0= fixangle(L0);

e= 0.016708634-(0.000042037*T)-

(0.0000001267*T*T);

M= 357.52911+(35999.05029*T)-(0.0001537*T*T);

E= y*sin(dtr(2*L0))-(2*e*sin(dtr(M)))+

(4*e*y*sin(dtr(M))*cos(dtr(2*L0)))-

(((y*y)*sin(dtr(4*L0)))/2)-

((5*(e*e)*sin(dtr(2*M)))/4);

#if 0

cout<<"E = "<<E<<"  degrees = "<<rtd(E)<<

"  min = "<<((rtd(E)/15)*60)<<"\n";

#endif

return rtd(E)*(3600/15);

}

/*:19*//*20:*/

#line 684 "timedate.w"

double systemtime::meanObliquityOfEcliptic(double jd)

{

/*29:*/

#line 1197 "timedate.w"

static double oterms[10]= {

Asec(-4680.93),

Asec(-1.55),

Asec(1999.25),

Asec(-51.38),

Asec(-249.67),

Asec(-39.05),

Asec(7.12),

Asec(27.87),

Asec(5.79),

Asec(2.45)

};

/*:29*/

#line 687 "timedate.w"

;

double eps= 23+(26/60.0)+(21.448/3600.0),u,v;

int i;

v= u= (jd-J2000)/(JulianCentury*100);

if(fabs(u)<1.0){

for(i= 0;i<10;i++){

eps+= oterms[i]*v;

v*= u;

}

}

return dtr(eps);

}

/*:20*//*21:*/

#line 725 "timedate.w"

void systemtime::nutation(double jd,double*deltaPsi,double*deltaEpsilon)

{

int i,j;

double t= (jd-J2000)/JulianCentury,t2,t3,to10;

double ta[5];

double dp= 0,de= 0,ang;

/*30:*/

#line 1222 "timedate.w"

static signed char nutArgMult[NUTERMS][5]= {

{0,0,0,0,1},

{-2,0,0,2,2},

{0,0,0,2,2},

{0,0,0,0,2},

{0,1,0,0,0},

{0,0,1,0,0},

{-2,1,0,2,2},

{0,0,0,2,1},

{0,0,1,2,2},

{-2,-1,0,2,2},

{-2,0,1,0,0},

{-2,0,0,2,1},

{0,0,-1,2,2},

{2,0,0,0,0},

{0,0,1,0,1},

{2,0,-1,2,2},

{0,0,-1,0,1},

{0,0,1,2,1},

{-2,0,2,0,0},

{0,0,-2,2,1},

{2,0,0,2,2},

{0,0,2,2,2},

{0,0,2,0,0},

{-2,0,1,2,2},

{0,0,0,2,0},

{-2,0,0,2,0},

{0,0,-1,2,1},

{0,2,0,0,0},

{2,0,-1,0,1},

{-2,2,0,2,2},

{0,1,0,0,1},

{-2,0,1,0,1},

{0,-1,0,0,1},

{0,0,2,-2,0},

{2,0,-1,2,1},

{2,0,1,2,2},

{0,1,0,2,2},

{-2,1,1,0,0},

{0,-1,0,2,2},

{2,0,0,2,1},

{2,0,1,0,0},

{-2,0,2,2,2},

{-2,0,1,2,1},

{2,0,-2,0,1},

{2,0,0,0,1},

{0,-1,1,0,0},

{-2,-1,0,2,1},

{-2,0,0,0,1},

{0,0,2,2,1},

{-2,0,2,0,1},

{-2,1,0,2,1},

{0,0,1,-2,0},

{-1,0,1,0,0},

{-2,1,0,0,0},

{1,0,0,0,0},

{0,0,1,2,0},

{-1,-1,1,0,0},

{0,1,1,0,0},

{0,-1,1,2,2},

{2,-1,-1,2,2},

{0,0,-2,2,2},

{0,0,3,2,2},

{2,-1,0,2,2}

};

/*:30*/

#line 734 "timedate.w"

;

/*31:*/

#line 1301 "timedate.w"

static long nutArgCoeff[NUTERMS][4]= {

{-171996,-1742,92095,89},

{-13187,-16,5736,-31},

{-2274,-2,977,-5},

{2062,2,-895,5},

{1426,-34,54,-1},

{712,1,-7,0},

{-517,12,224,-6},

{-386,-4,200,0},

{-301,0,129,-1},

{217,-5,-95,3},

{-158,0,0,0},

{129,1,-70,0},

{123,0,-53,0},

{63,0,0,0},

{63,1,-33,0},

{-59,0,26,0},

{-58,-1,32,0},

{-51,0,27,0},

{48,0,0,0},

{46,0,-24,0},

{-38,0,16,0},

{-31,0,13,0},

{29,0,0,0},

{29,0,-12,0},

{26,0,0,0},

{-22,0,0,0},

{21,0,-10,0},

{17,-1,0,0},

{16,0,-8,0},

{-16,1,7,0},

{-15,0,9,0},

{-13,0,7,0},

{-12,0,6,0},

{11,0,0,0},

{-10,0,5,0},

{-8,0,3,0},

{7,0,-3,0},

{-7,0,0,0},

{-7,0,3,0},

{-7,0,3,0},

{6,0,0,0},

{6,0,-3,0},

{6,0,-3,0},

{-6,0,3,0},

{-6,0,3,0},

{5,0,0,0},

{-5,0,3,0},

{-5,0,3,0},

{-5,0,3,0},

{4,0,0,0},

{4,0,0,0},

{4,0,0,0},

{-4,0,0,0},

{-4,0,0,0},

{-4,0,0,0},

{3,0,0,0},

{-3,0,0,0},

{-3,0,0,0},

{-3,0,0,0},

{-3,0,0,0},

{-3,0,0,0},

{-3,0,0,0},

{-3,0,0,0}

};

/*:31*/

#line 735 "timedate.w"

;

t3= t*(t2= t*t);

ta[0]= dtr(297.85036+445267.11148*t-0.0019142*t2+

t3/189474.0);

ta[1]= dtr(357.52772+35999.05034*t-0.0001603*t2-

t3/300000.0);

ta[2]= dtr(134.96298+477198.867398*t+0.0086972*t2+

t3/56250.0);

ta[3]= dtr(93.27191+483202.017538*t-0.0036825*t2+

t3/327270.0);

ta[4]= dtr(125.04452-1934.136261*t+0.0020708*t2+

t3/450000.0);

for(i= 0;i<5;i++){

ta[i]= fixangr(ta[i]);

}

to10= t/10.0;

for(i= 0;i<NUTERMS;i++){

ang= 0;

for(j= 0;j<5;j++){

if(nutArgMult[i][j]!=0){

ang+= nutArgMult[i][j]*ta[j];

}

}

dp+= (nutArgCoeff[i][0]+nutArgCoeff[i][1]*to10)*sin(ang);

de+= (nutArgCoeff[i][2]+nutArgCoeff[i][3]*to10)*cos(ang);

}

dstore_if(deltaPsi,dtr(dp/(3600.0*10000.0)));

dstore_if(deltaEpsilon,dtr(de/(3600.0*10000.0)));

}

/*:21*//*22:*/

#line 788 "timedate.w"

void systemtime::sunpos(double jd,bool apparent,

double*ra,double*dec,double*rv,double*slong)

{

double t,t2,C,l,m,e,v,theta,omega,eps;

t= (jd-J2000)/JulianCentury;

t2= t*t;

l= fixangle(280.46646+(36000.76983*t)+(0.0003032*t2));

m= fixangle(357.52911+(35999.05029*t)-(0.0001537*t2));

e= 0.016708634-(0.000042037*t)-(0.0000001267*t2);

C= ((1.914602-(0.004817*t)-(0.000014*t2))*sin(dtr(m)))+

((0.019993-(0.000101*t))*sin(2*dtr(m)))+

(0.000289*sin(3*dtr(m)));

theta= l+C;

v= m+C;

eps= rtd(meanObliquityOfEcliptic(jd));

if(apparent){

omega= fixangle(125.04-1934.136*t);

theta= theta-0.00569-0.00478*sin(dtr(omega));

eps+= 0.00256*cos(dtr(omega));

}

dstore_if(slong,dtr(theta));

dstore_if(rv,(1.000001018*(1-(e*e)))/(1+e*cos(dtr(v))));

dstore_if(ra,fixangr(atan2(cos(dtr(eps))*sin(dtr(theta)),cos(dtr(theta)))));

dstore_if(dec,asin(sin(dtr(eps))*sin(dtr(theta))));

}

/*:22*//*23:*/

#line 868 "timedate.w"

void systemtime::moon_and_sun(double jd,double*phaseang,

double*pphase,double*mage,double*dist,

double*angdia,double*sudist,

double*suangdia)

{

double Day,N,M,Ec,Lambdasun,ml,MM,MN,Ev,Ae,A3,MmP,

mEc,A4,lP,Varia,lPP,NP,y,x,Lambdamoon,

MoonAge,MoonPhase,

MoonDist,MoonDFrac,MoonAng,

F,SunDist,SunAng;

static const double

AstronomicalUnit= 149597870.0,

SunSMAX= (AstronomicalUnit*1.000001018),

epoch= 2444238.5,

elonge= 278.833540,

elongp= 282.596403,

eccent= 0.016718,

sunangsiz= 0.533128,

mmlong= 64.975464,

mmlongp= 349.383063,

mlnode= 151.950429,

minc= 5.145396,

mecc= 0.054900,

mangsiz= 0.5181,

msmax= 384401.0;

Day= jd-epoch;

N= fixangle((360/365.2422)*Day);

M= fixangle(N+elonge-elongp);

Ec= kepler(M,eccent);

Ec= sqrt((1+eccent)/(1-eccent))*tan(Ec/2);

Ec= 2*rtd(atan(Ec));

Lambdasun= fixangle(Ec+elongp);

F= ((1+eccent*cos(dtr(Ec)))/(1-eccent*eccent));

SunDist= SunSMAX/F;

SunAng= F*sunangsiz;

ml= fixangle(13.1763966*Day+mmlong);

MM= fixangle(ml-0.1114041*Day-mmlongp);

MN= fixangle(mlnode-0.0529539*Day);

Ev= 1.2739*sin(dtr(2*(ml-Lambdasun)-MM));

Ae= 0.1858*sin(dtr(M));

A3= 0.37*sin(dtr(M));

MmP= MM+Ev-Ae-A3;

mEc= 6.2886*sin(dtr(MmP));

A4= 0.214*sin(dtr(2*MmP));

lP= ml+Ev+mEc-Ae+A4;

Varia= 0.6583*sin(dtr(2*(lP-Lambdasun)));

lPP= lP+Varia;

NP= MN-0.16*sin(dtr(M));

y= sin(dtr(lPP-NP))*cos(dtr(minc));

x= cos(dtr(lPP-NP));

Lambdamoon= rtd(atan2(y,x));

Lambdamoon+= NP;

MoonAge= lPP-Lambdasun;

MoonPhase= (1-cos(dtr(MoonAge)))/2;

MoonDist= (msmax*(1-mecc*mecc))/

(1+mecc*cos(dtr(MmP+mEc)));

MoonDFrac= MoonDist/msmax;

MoonAng= mangsiz/MoonDFrac;

dstore_if(pphase,MoonPhase);

dstore_if(mage,SynMonth*(fixangle(MoonAge)/360.0));

dstore_if(dist,MoonDist);

dstore_if(angdia,dtr(MoonAng));

dstore_if(sudist,SunDist);

dstore_if(suangdia,dtr(SunAng));

dstore_if(phaseang,fixangle(MoonAge)/360.0);

}

/*:23*//*24:*/

#line 1024 "timedate.w"

double systemtime::kepler(double m,double ecc)

{

double E,delta;

static const double EPSILON= 1E-6;

E= m= dtr(m);

do{

delta= E-ecc*sin(E)-m;

E-= delta/(1-ecc*cos(E));

}while(fabs(delta)> EPSILON);

return E;

}

/*:24*//*25:*/

#line 1045 "timedate.w"

double systemtime::meanphase(double sdate,double k)

{

double t,t2,t3,nt1;

t= (sdate-2415020.0)/JulianCentury;

t2= t*t;

t3= t2*t;

nt1= 2415020.75933+SynMonth*k

+0.0001178*t2

-0.000000155*t3

+0.00033*sin(dtr(166.56+132.87*t-0.009173*t2));

return nt1;

}

/*:25*//*26:*/

#line 1068 "timedate.w"

double systemtime::truephase(double k,double phase)

{

double t,t2,t3,pt,m,mprime,f;

k+= phase;

t= k/1236.85;

t2= t*t;

t3= t2*t;

pt= 2415020.75933

+SynMonth*k

+0.0001178*t2

-0.000000155*t3

+0.00033*sin(dtr(166.56+132.87*t-0.009173*t2));

m= 359.2242

+29.10535608*k

-0.0000333*t2

-0.00000347*t3;

mprime= 306.0253

+385.81691806*k

+0.0107306*t2

+0.00001236*t3;

f= 21.2964

+390.67050646*k

-0.0016528*t2

-0.00000239*t3;

if((phase<0.01)||(fabs(phase-0.5)<0.01)){

pt+= (0.1734-0.000393*t)*sin(dtr(m))

+0.0021*sin(dtr(2*m))

-0.4068*sin(dtr(mprime))

+0.0161*sin(dtr(2*mprime))

-0.0004*sin(dtr(3*mprime))

+0.0104*sin(dtr(2*f))

-0.0051*sin(dtr(m+mprime))

-0.0074*sin(dtr(m-mprime))

+0.0004*sin(dtr(2*f+m))

-0.0004*sin(dtr(2*f-m))

-0.0006*sin(dtr(2*f+mprime))

+0.0010*sin(dtr(2*f-mprime))

+0.0005*sin(dtr(m+2*mprime));

}else if((fabs(phase-0.25)<0.01||(fabs(phase-0.75)<0.01))){

pt+= (0.1721-0.0004*t)*sin(dtr(m))

+0.0021*sin(dtr(2*m))

-0.6280*sin(dtr(mprime))

+0.0089*sin(dtr(2*mprime))

-0.0004*sin(dtr(3*mprime))

+0.0079*sin(dtr(2*f))

-0.0119*sin(dtr(m+mprime))

-0.0047*sin(dtr(m-mprime))

+0.0003*sin(dtr(2*f+m))

-0.0004*sin(dtr(2*f-m))

-0.0006*sin(dtr(2*f+mprime))

+0.0021*sin(dtr(2*f-mprime))

+0.0003*sin(dtr(m+2*mprime))

+0.0004*sin(dtr(m-2*mprime))

-0.0003*sin(dtr(2*m+mprime));

if(phase<0.5)

pt+= 0.0028-0.0004*cos(dtr(m))+0.0003*cos(dtr(mprime));

else

pt+= -0.0028+0.0004*cos(dtr(m))-0.0003*cos(dtr(mprime));

}

return pt;

}

/*:26*//*27:*/

#line 1155 "timedate.w"

void systemtime::moonphases(double sdate,double phases[5])

{

double adate,k1,k2;

int yy,mm,dd;

adate= sdate-45;

jyear(adate,&yy,&mm,&dd);

k1= floor((yy+((mm-1)*(1.0/12.0))-1900)*12.3685);

adate= meanphase(adate,k1);

while(true){

adate+= SynMonth;

k2= k1+1;

if(truephase(k1,0.0)<=sdate&&truephase(k2,0.0)> sdate){

break;

}

k1= k2;

}

phases[0]= truephase(k1,0.0);

phases[1]= truephase(k1,0.25);

phases[2]= truephase(k1,0.5);

phases[3]= truephase(k1,0.75);

phases[4]= truephase(k2,0.0);

}

/*:27*/

#line 108 "timedate.w"

/*:5*/

#line 73 "timedate.w"

/*:2*/

///////////////////////////////////////////////////////////////////////

/*1:*/

#line 58 "timedate.w"

#define REVDATE "2nd February 2002"

/*:1*//*32:*/

#line 1377 "timedate.w"

/*35:*/

#line 1541 "timedate.w"

#include "config.h"         

#include <iostream> 

#include <exception> 

#include <stdexcept> 

#include <string> 

using namespace std;

#include <stdio.h> 

#include <stdlib.h> 

#include <ctype.h> 

#ifdef HAVE_GETOPT

#ifdef HAVE_UNISTD_H

#include <unistd.h> 

#endif

#else

#include "getopt.h"     

#endif

#include "timedate.h"        

/*:35*/

#line 1379 "timedate.w"

;

/*34:*/

#line 1523 "timedate.w"

static void usage(void)

{

cout<<PRODUCT<<"  --  Analyse eggsummary files.  Call:\n";

cout<<"              "<<PRODUCT<<" [options] [infile] [outfile]\n";

cout<<"\n";

cout<<"Options:\n";

cout<<"           --copyright       Print copyright information\n";

cout<<"           -u, --help        Print this message\n";

cout<<"           --version         Print version number\n";

cout<<"\n";

cout<<"by John Walker\n";

cout<<"http://www.fourmilab.ch/\n";

}

/*:34*/

#line 1381 "timedate.w"

;

int main(int argc,char*argv[])

{

extern char*optarg;

extern int optind;

int opt;

/*33:*/

#line 1491 "timedate.w"

while((opt= getopt(argc,argv,"nu-:"))!=-1){

switch(opt){

case'u':

case'?':

usage();

return 0;

case'-':

switch(optarg[0]){

case'c':

cout<<"This program is in the public domain.\n";

return 0;

case'h':

usage();

return 0;

case'v':

cout<<PRODUCT<<" "<<VERSION<<"\n";

cout<<"Last revised: "<<REVDATE<<"\n";

cout<<"The latest version is always available\n";

cout<<"at http://www.fourmilab.ch/eggtools/eggshell\n";

return 0;

}

}

}

/*:33*/

#line 1390 "timedate.w"

;

string s;

systemtime td,td1;

td.now();

cout<<td.dateToString()<<" "<<td.timeToString()<<"\n";

td1= td;

td1.nextDay();

cout<<"Tomorrow: "<<td1.dateToString()<<" "<<td1.timeToString()<<"\n";

td1= td;

td1.previousDay();

cout<<"Yesterday: "<<td1.dateToString()<<" "<<td1.timeToString()<<"\n";

td1= td;

td1.dayStep(7);

cout<<"Next week: "<<td1.dateToString()<<" "<<td1.timeToString()<<"\n";

td.fromString("1981-04-12 13:51:18");

cout<<td.dateToString()<<" "<<td.timeToString()<<"\n";

td.fromString("2021-04-12");

cout<<td.dateToString()<<" "<<td.timeToString()<<"\n";

cout<<"Weekday = "<<systemtime::jwday(systemtime::utctoj(2001,

5,12,23,59,59))<<"\n";

td.fromJulian(2436116.31);

cout<<td.dateToString()<<" "<<td.timeToString()<<"\n";

cout<<"To Julian = "<<td.toJulian()<<" Weekday = "<<td.weekday()<<"\n";

td.fromString("1987-04-10 19:21:00");

cout<<"Sidereal time = "<<angle::degrees_to_d_m_s(td.siderealTime())<<"\n";

cout.flush();

int t1,t2,t3;

systemtime::jyear(2436116.31,&t1,&t2,&t3);

printf("%04d-%02d-%02d\n",t1,t2,t3);

systemtime::jhms(systemtime::utctoj(1989,11,14,16,23,18),&t1,&t2,&t3);

printf("%02d:%02d:%02d\n",t1,t2,t3);

{

double j= 2446895.5;

double o= angle::rtd(systemtime::meanObliquityOfEcliptic(j));

double delpsi,deleps;

cout<<"meanObliquityOfEcliptic("<<((long)j)<<") = "<<

angle::degrees_to_d_m_s(o)<<"\n";

systemtime::nutation(j,&delpsi,&deleps);

cout<<"  delpsi = "<<

angle::degrees_to_d_m_s(angle::rtd(delpsi))<<

" deleps = "<<

angle::degrees_to_d_m_s(angle::rtd(deleps))<<"\n";

double eOt= systemtime::equationTime(2448908.5);

cout<<"Equation of time = "<<((int)(eOt/60))<<" minutes "<<

(eOt-(60*((int)(eOt/60))))<<" seconds\n";

j= 2448908.5;

double sura,sudec,surv,suslong;

systemtime::sunpos(j,true,

&sura,&sudec,&surv,&suslong);

cout<<"Sun position:  RA = "<<angle::rtd(sura)<<

"  Dec = "<<angle::rtd(sudec)<<"  Rv = "<<surv<<

"  Long = "<<angle::rtd(suslong)<<"\n";

}

{

double phaseang,pphase,mage,dist,angdia,

sudist,suangdia;

systemtime::moon_and_sun(2452060.13874,

&phaseang,&pphase,&mage,&dist,&angdia,

&sudist,&suangdia);

cout<<"Moon:  Phaseang = "<<(phaseang*100)<<

"  Pphase = "<<((int)(pphase*100))<<"%\n       "<<

"  Moon age = "<<((int)mage)<<" days, "<<

angle::degrees_to_d_m_s((mage-((int)mage))*24)<<

"  Dist = "<<dist<<" km\n"<<

"Sun:   Angdia = "<<angle::rtd(angdia)<<

"  Sudist = "<<((int)sudist)<<" km"<<

"  Suangdia = "<<angle::rtd(suangdia)<<"\n";

}

{

double phases[5];

systemtime pt[5];

int i;

systemtime::moonphases(2452060.13874,phases);

for(i= 0;i<5;i++){

pt[i].fromJulian(phases[i]);

cout<<"Phase "<<i<<": "<<pt[i].dateToString()<<

" "<<pt[i].timeToString()<<"\n";

}

}

return 0;

}

/*:32*/

Facilities for working with times and dates, using UNIX time_t quantities as the underlying type. Conversion to and from string representations, Julian day numbers, and computation of astronomical quantities such as sidereal time, the phase of the Moon, and the position of the Sun and Moon are provided.

//////////////////////////////////////////////