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