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 - http://www.flightgear.org/~curt
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 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.
32 # error This library requires C++
36 #include <simgear/compiler.h>
40 #include <simgear/timing/timezone.h>
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.
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.
66 // tzContainer stores all the current Timezone control points/
67 SGTimeZoneContainer* tzContainer;
69 // Points to the current local timezone name;
72 // Unix "calendar" time in seconds
75 // Break down of equivalent GMT time
76 struct tm m_gmt; // copy of system gmtime(&time_t) structure
78 // offset of local time relative to GMT
84 // modified Julian date
87 // side real time at prime meridian
90 // local sidereal time
93 // the difference between the precise / expensive sidereal time
94 // algorithm result and the quick course result. course_gst +
95 // gst_diff has pretty good accuracy over the span of a couple hours
98 /** init common constructor code */
99 void init( double lon_rad, double lat_rad, const std::string& root,
104 /** Default constructor */
108 * Create an instance based on a specified position and data file path.
109 * This creates an instance of the SGTime object. When calling the
110 * constructor you need to provide a root path pointing to your
111 * time zone definition tree. Optionally, you can call a form of
112 * the constructor that accepts your current longitude and
113 * latitude in radians.
115 * If you don't know your position when you call the SGTime
116 * constructor, you can just use the first form (which assumes 0,
118 * @param lon_rad current longitude (radians)
119 * @param lat_rad current latitude (radians)
120 * @param root root path point to data file location (timezone, etc.)
121 * @param init_time provide an initialization time, 0 means use
122 current clock time */
123 SGTime( double lon_rad, double lat_rad, const std::string& root,
127 * Create an instance given a data file path.
128 * @param root root path point to data file location (timezone, etc.)
130 SGTime( const std::string& root );
136 * Update the time related variables.
137 * The update() method requires you to pass in your position and
138 * an optional time offset in seconds. The offset (or warp) allows
139 * you to offset "sim" time relative to "real" time. The update()
140 * method is designed to be called by the host application before
142 * @param lon_rad current longitude (radians)
143 * @param lat_rad current latitude (radians)
144 * @param ct specify a unix time, otherwise specify 0 to use current
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_rad, double lat_rad, time_t ct, long int warp );
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_rad current longitude (radians)
158 * @param lat_rad current latitude (radians)
159 * @param root base path containing time zone directory */
160 void updateLocal( double lon_rad, double lat_rad, const std::string& root );
162 /** @return current system/unix time in seconds */
163 inline time_t get_cur_time() const { return cur_time; };
165 /** @return time zone name for your current position*/
166 inline const char * get_zonename() const { return zonename.c_str(); }
168 /** @return GMT in a "brokent down" tm structure */
169 inline struct tm* getGmt()const { return (struct tm *)&m_gmt; };
171 /** @return julian date */
172 inline double getJD() const { return jd; };
174 /** @return modified julian date */
175 inline double getMjd() const { return mjd; };
177 /** @return local side real time */
178 inline double getLst() const { return lst; };
180 /** @return grenich side real time (lst when longitude == 0) */
181 inline double getGst() const { return gst; };
183 /** @return offset in seconds to local timezone time */
184 inline time_t get_local_offset() const { return local_offset; };
188 // Some useful utility functions that don't make sense to be part of
193 * Return unix time in seconds for the given date (relative to GMT)
194 * @param year current GMT year
195 * @param month current GMT month
196 * @param day current GMT day
197 * @param hour current GMT hour
198 * @param minute current minute
199 * @param second current second
200 * @return unix/system time in seconds
202 time_t sgTimeGetGMT(int year, int month, int day,
203 int hour, int minute, int second);
207 * this is just a wrapper for sgTimeGetGMT that allows an alternate
208 * form of input parameters.
209 * @param the_time the current GMT time in the tm structure
210 * @return unix/system time in seconds
212 inline time_t sgTimeGetGMT(struct tm* the_time) {
213 // printf("Using: %24s as input\n", asctime(the_time));
214 return sgTimeGetGMT(the_time->tm_year,
224 * Given a date in our form, return the equivalent modified Julian
225 * date (number of days elapsed since 1900 jan 0.5), mjd. Adapted
230 * @return modified julian date */
231 double sgTimeCalcMJD(int mn, double dy, int yr);
235 * Given an optional offset from current time calculate the current
236 * modified julian date.
237 * @param ct specify a unix time, otherwise specify 0 to use current
239 * @param warp number of seconds to offset from current time (0 if no offset)
240 * @return current modified Julian date (number of days elapsed
241 * since 1900 jan 0.5), mjd. */
242 double sgTimeCurrentMJD( time_t ct /* = 0 */, long int warp /* = 0 */ );
246 * Given an mjd, calculate greenwich mean sidereal time, gst
247 * @param mjd modified julian date
248 * @return greenwich mean sidereal time (gst) */
249 double sgTimeCalcGST( double mjd );
253 * Format time in a pretty form
254 * @param p time specified in a tm struct
255 * @param buf buffer space to contain the result
256 * @return pointer to character array containt the result
258 char* sgTimeFormatTime( const struct tm* p, char* buf );
261 #endif // _SG_TIME_HXX