]> git.mxchange.org Git - simgear.git/blob - simgear/timing/sg_time.hxx
5a6da887ae7dc65d6e2155e0702f9b2c6479badb
[simgear.git] / simgear / timing / sg_time.hxx
1 // sg_time.hxx -- data structures and routines for managing time related stuff.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _SG_TIME_HXX
25 #define _SG_TIME_HXX
26
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36
37 #include <simgear/compiler.h>
38
39 #ifdef FG_HAVE_STD_INCLUDES
40 #  include <ctime>
41 #else
42 #  include <time.h>
43 #endif
44
45 #include "timezone.h"
46
47
48 // Define a structure containing time parameters
49 class SGTime {
50
51 private:
52     // tzContainer stores all the current Timezone control points/
53     TimezoneContainer* tzContainer;
54
55     // Points to the current local timezone name;
56     char *zonename;
57
58     // Unix "calendar" time in seconds
59     time_t cur_time;
60
61     // Break down of equivalent GMT time
62     struct tm *gmt;
63
64     // offset of local time relative to GMT
65     time_t local_offset;
66
67     // Julian date
68     double jd;
69
70     // modified Julian date
71     double mjd;
72
73     // side real time at prime meridian
74     double gst;
75
76     // local sidereal time
77     double lst;
78
79     // the difference between the precise / expensive sidereal time
80     // algorithm result and the quick course result.  course_gst +
81     // gst_diff has pretty good accuracy over the span of a couple hours
82     double gst_diff;
83
84 public:
85
86     SGTime( const string& root );
87     ~SGTime();
88
89     // Initialize the time related variables
90     void init( double lon, double lat, const string& root );
91
92     // Update the time related variables
93     void update( double lon, double lat, double alt_m, long int warp );
94
95     // Given lon/lat, update timezone information and local_offset
96     void updateLocal( double lon, double lat, const string& root );
97
98     // given Julian Date and Longitude (decimal degrees West) compute
99     // Local Sidereal Time, in decimal hours.
100     //
101     // Provided courtesy of ecdowney@noao.edu (Elwood Downey) 
102     double sidereal_precise( double lng );
103
104     // return a courser but cheaper estimate of sidereal time
105     double sidereal_course( double lng );
106
107     // Some other stuff which were changed to SGTime members on
108     // questionable grounds -:)
109     time_t get_gmt(int year, int month, int day, 
110                    int hour, int minute, int second);
111     time_t get_gmt(struct tm* the_time);
112
113     inline double getJD() const { return jd; };
114     inline double getMjd() const { return mjd; };
115     inline double getLst() const { return lst; };
116     inline double getGst() const { return gst; };
117     inline time_t get_cur_time() const { return cur_time; };
118     inline struct tm* getGmt()const { return gmt; };
119     inline char* get_zonename() const { return zonename; }
120 };
121
122
123 // this is just a wrapper
124 inline time_t SGTime::get_gmt(struct tm* the_time) {
125     // printf("Using: %24s as input\n", asctime(the_time));
126     return get_gmt(the_time->tm_year,
127                    the_time->tm_mon,
128                    the_time->tm_mday,
129                    the_time->tm_hour,
130                    the_time->tm_min,
131                    the_time->tm_sec);
132 }
133
134 // given a date in months, mn, days, dy, years, yr, return the
135 // modified Julian date (number of days elapsed since 1900 jan 0.5),
136 // mjd.  Adapted from Xephem.
137 double sgTimeCalcMJD(int mn, double dy, int yr);
138
139 // given an mjd, calculate greenwich mean sidereal time, gst
140 double sgTimeCalcGST( double mjd );
141
142 // format time
143 char* sgTimeFormatTime( const struct tm* p, char* buf );
144
145
146 #endif // _SG_TIME_HXX