]> git.mxchange.org Git - simgear.git/blob - simgear/timing/sg_time.hxx
680c161134682a813c5cca24fb9379dce7c227de
[simgear.git] / simgear / timing / sg_time.hxx
1 /**
2  * \file sg_time.hxx
3  * Data structures and routines for managing time related values.
4  */
5
6 // Written by Curtis Olson, started August 1997.
7 //
8 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifndef _SG_TIME_HXX
28 #define _SG_TIME_HXX
29
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35
36 #include <simgear/compiler.h>
37
38 #include <ctime>
39
40 #include <simgear/timing/timezone.h>
41
42
43 /**
44  * A class to calculate and manage a variety of time parameters.
45  * The SGTime class provides many real-world time values. It
46  * calculates current time in seconds, GMT time, local time zone,
47  * local offset in seconds from GMT, Julian date, and sidereal
48  * time. All of these operate with seconds as their granularity so
49  * this class is not intended for timing sub-second events. These
50  * values are intended as input to things like real world lighting
51  * calculations and real astronomical object placement.
52
53  * To properly use the SGTime class there are a couple of things to be
54  * aware of. After creating an instance of the class, you will need to
55  * periodically (i.e. before every frame) call the update()
56  * method. Optionally, if you care about updating time zone
57  * information based on your latitude and longitude, you can call the
58  * updateLocal() method periodically as your position changes by
59  * significant amounts.
60
61  */
62
63 class SGTime {
64
65 private:
66     // tzContainer stores all the current Timezone control points/
67     SGTimeZoneContainer* tzContainer;
68
69     // Points to the current local timezone name;
70     std::string zonename;
71
72     // Unix "calendar" time in seconds
73     time_t cur_time;
74
75     // Break down of equivalent GMT time
76 #if defined(_MSC_VER) || defined(__MINGW32__)
77     struct tm m_gmt;    // copy of system gmtime(&time_t) structure
78 #else
79     struct tm *gmt;
80 #endif
81
82     // offset of local time relative to GMT
83     time_t local_offset;
84
85     // Julian date
86     double jd;
87
88     // modified Julian date
89     double mjd;
90
91     // side real time at prime meridian
92     double gst;
93
94     // local sidereal time
95     double lst;
96
97     // the difference between the precise / expensive sidereal time
98     // algorithm result and the quick course result.  course_gst +
99     // gst_diff has pretty good accuracy over the span of a couple hours
100     double gst_diff;
101
102     /** init common constructor code */
103     void init( double lon_rad, double lat_rad, const std::string& root,
104                time_t init_time );
105
106 public:
107
108     /** Default constructor */
109     SGTime();
110
111     /**
112      * Create an instance based on a specified position and data file path.
113      * This creates an instance of the SGTime object. When calling the
114      * constructor you need to provide a root path pointing to your
115      * time zone definition tree. Optionally, you can call a form of
116      * the constructor that accepts your current longitude and
117      * latitude in radians.
118      *
119      * If you don't know your position when you call the SGTime
120      * constructor, you can just use the first form (which assumes 0,
121      * 0).
122      * @param lon_rad current longitude (radians)
123      * @param lat_rad current latitude (radians)
124      * @param root root path point to data file location (timezone, etc.)
125      * @param init_time provide an initialization time, 0 means use
126               current clock time */
127     SGTime( double lon_rad, double lat_rad, const std::string& root,
128             time_t init_time );
129
130     /**
131      * Create an instance given a data file path.
132      * @param root root path point to data file location (timezone, etc.)
133      */
134     SGTime( const std::string& root );
135
136     /** Destructor */
137     ~SGTime();
138
139     /** 
140      * Update the time related variables.
141      * The update() method requires you to pass in your position and
142      * an optional time offset in seconds. The offset (or warp) allows
143      * you to offset "sim" time relative to "real" time. The update()
144      * method is designed to be called by the host application before
145      * every frame.
146      * @param lon_rad current longitude (radians)
147      * @param lat_rad current latitude (radians)
148      * @param ct specify a unix time, otherwise specify 0 to use current
149               clock time
150      * @param warp an optional time offset specified in seconds.  This
151      *        allows us to advance or rewind "time" if we choose to.  */
152     void update( double lon_rad, double lat_rad, time_t ct, long int warp );
153
154     /**
155      * Given lon/lat, update timezone information and local_offset
156      * The updateLocal() method is intended to be called less
157      * frequently - only when your position is likely to be changed
158      * enough that your timezone may have changed as well. In the
159      * FlightGear project we call updateLocal() every few minutes from
160      * our periodic event manager.
161      * @param lon_rad current longitude (radians)
162      * @param lat_rad current latitude (radians)
163      * @param root base path containing time zone directory */
164     void updateLocal( double lon_rad, double lat_rad, const std::string& root );
165
166     /** @return current system/unix time in seconds */
167     inline time_t get_cur_time() const { return cur_time; };
168
169     /** @return time zone name for your current position*/
170     inline const char * get_zonename() const { return zonename.c_str(); }
171
172     /** @return GMT in a "brokent down" tm structure */
173 #if defined(_MSC_VER) || defined(__MINGW32__)
174     inline struct tm* getGmt()const { return (struct tm *)&m_gmt; };
175 #else
176     inline struct tm* getGmt()const { return gmt; };
177 #endif
178
179     /** @return julian date */
180     inline double getJD() const { return jd; };
181
182     /** @return modified julian date */
183     inline double getMjd() const { return mjd; };
184
185     /** @return local side real time */
186     inline double getLst() const { return lst; };
187
188     /** @return grenich side real time (lst when longitude == 0) */
189     inline double getGst() const { return gst; };
190
191     /** @return offset in seconds to local timezone time */
192     inline time_t get_local_offset() const { return local_offset; };
193 };
194
195
196 // Some useful utility functions that don't make sense to be part of
197 // the SGTime class
198
199 /**
200  * \relates SGTime
201  * Return unix time in seconds for the given date (relative to GMT)
202  * @param year current GMT year
203  * @param month current GMT month
204  * @param day current GMT day
205  * @param hour current GMT hour
206  * @param minute current minute
207  * @param second current second
208  * @return unix/system time in seconds
209  */
210 time_t sgTimeGetGMT(int year, int month, int day, 
211                     int hour, int minute, int second);
212
213 /**
214  * \relates SGTime
215  * this is just a wrapper for sgTimeGetGMT that allows an alternate
216  * form of input parameters.
217  * @param the_time the current GMT time in the tm structure
218  * @return unix/system time in seconds
219  */
220 inline time_t sgTimeGetGMT(struct tm* the_time) {
221     // printf("Using: %24s as input\n", asctime(the_time));
222     return sgTimeGetGMT(the_time->tm_year,
223                         the_time->tm_mon,
224                         the_time->tm_mday,
225                         the_time->tm_hour,
226                         the_time->tm_min,
227                         the_time->tm_sec);
228 }
229
230 /**
231  * \relates SGTime
232  * Given a date in our form, return the equivalent modified Julian
233  * date (number of days elapsed since 1900 jan 0.5), mjd.  Adapted
234  * from Xephem.
235  * @param mn month
236  * @param dy day
237  * @param yr year
238  * @return modified julian date */
239 double sgTimeCalcMJD(int mn, double dy, int yr);
240
241 /**
242  * \relates SGTime
243  * Given an optional offset from current time calculate the current
244  * modified julian date.
245  * @param ct specify a unix time, otherwise specify 0 to use current
246           clock time
247  * @param warp number of seconds to offset from current time (0 if no offset)
248  * @return current modified Julian date (number of days elapsed
249  * since 1900 jan 0.5), mjd. */
250 double sgTimeCurrentMJD( time_t ct /* = 0 */, long int warp /* = 0 */ );
251
252 /**
253  * \relates SGTime
254  * Given an mjd, calculate greenwich mean sidereal time, gst
255  * @param mjd modified julian date
256  * @return greenwich mean sidereal time (gst) */
257 double sgTimeCalcGST( double mjd );
258
259 /**
260  * \relates SGTime
261  * Format time in a pretty form
262  * @param p time specified in a tm struct
263  * @param buf buffer space to contain the result
264  * @return pointer to character array containt the result
265  */
266 char* sgTimeFormatTime( const struct tm* p, char* buf );
267
268
269 #endif // _SG_TIME_HXX