3 * Data structures and routines for managing time related values.
6 // Written by Curtis Olson, started August 1997.
8 // Copyright (C) 1997 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
33 # error This library requires C++
37 #include <simgear/compiler.h>
39 #ifdef SG_HAVE_STD_INCLUDES
45 #include <simgear/timing/timezone.h>
49 * A class to calculate and manage a variety of time parameters.
50 * The SGTime class provides many real-world time values. It
51 * calculates current time in seconds, GMT time, local time zone,
52 * local offset in seconds from GMT, Julian date, and sidereal
53 * time. All of these operate with seconds as their granularity so
54 * this class is not intended for timing sub-second events. These
55 * values are intended as input to things like real world lighting
56 * calculations and real astronomical object placement.
58 * To properly use the SGTime class there are a couple of things to be
59 * aware of. After creating an instance of the class, you will need to
60 * periodically (i.e. before every frame) call the update()
61 * method. Optionally, if you care about updating time zone
62 * information based on your latitude and longitude, you can call the
63 * updateLocal() method periodically as your position changes by
64 * significant amounts.
71 // tzContainer stores all the current Timezone control points/
72 TimezoneContainer* tzContainer;
74 // Points to the current local timezone name;
77 // Unix "calendar" time in seconds
80 // Break down of equivalent GMT time
81 #if defined(_MSC_VER) || defined(__MINGW32__)
82 struct tm m_gmt; // copy of system gmtime(&time_t) structure
87 // offset of local time relative to GMT
93 // modified Julian date
96 // side real time at prime meridian
99 // local sidereal time
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
109 /** Default constructor */
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.
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,
123 * @param lon current longitude
124 * @param lat current latitude
125 * @param root root path point to data file location (timezone, etc.)
126 * @param init_time provide an initialization time, 0 means use
127 current clock time */
128 SGTime( double lon, double lat, const string& root, time_t init_time /* = 0 */ );
131 * Create an instance given a data file path.
132 * @param root root path point to data file location (timezone, etc.)
134 SGTime( const string& root );
139 /** init common constructor code */
140 void init( double lon, double lat, const string& root, time_t init_time /* = 0 */ );
143 * Update the time related variables.
144 * The update() method requires you to pass in your position and
145 * an optional time offset in seconds. The offset (or warp) allows
146 * you to offset "sim" time relative to "real" time. The update()
147 * method is designed to be called by the host application before
149 * @param lon current longitude
150 * @param lat current latitude
151 * @param ct specify a unix time, otherwise specify 0 to use current
153 * @param warp an optional time offset specified in seconds. This
154 * allows us to advance or rewind "time" if we choose to. */
155 void update( double lon, double lat, time_t ct /* = 0 */, long int warp /* = 0 */ );
158 * Given lon/lat, update timezone information and local_offset
159 * The updateLocal() method is intended to be called less
160 * frequently - only when your position is likely to be changed
161 * enough that your timezone may have changed as well. In the
162 * FlightGear project we call updateLocal() every few minutes from
163 * our periodic event manager.
164 * @param lon current longitude
165 * @param lat current latitude
166 * @param root base path containing time zone directory */
167 void updateLocal( double lon, double lat, const string& root );
169 /** @return current system/unix time in seconds */
170 inline time_t get_cur_time() const { return cur_time; };
172 /** @return time zone name for your current position*/
173 inline char* get_zonename() const { return zonename; }
175 /** @return GMT in a "brokent down" tm structure */
176 #if defined(_MSC_VER) || defined(__MINGW32__)
177 inline struct tm* getGmt()const { return (struct tm *)&m_gmt; };
179 inline struct tm* getGmt()const { return gmt; };
182 /** @return julian date */
183 inline double getJD() const { return jd; };
185 /** @return modified julian date */
186 inline double getMjd() const { return mjd; };
188 /** @return local side real time */
189 inline double getLst() const { return lst; };
191 /** @return grenich side real time (lst when longitude == 0) */
192 inline double getGst() const { return gst; };
196 // Some useful utility functions that don't make sense to be part of
201 * Return unix time in seconds for the given data (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
210 time_t sgTimeGetGMT(int year, int month, int day,
211 int hour, int minute, int second);
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
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,
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
238 * @return modified julian date */
239 double sgTimeCalcMJD(int mn, double dy, int yr);
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
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 */ );
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 );
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
266 char* sgTimeFormatTime( const struct tm* p, char* buf );
269 #endif // _SG_TIME_HXX