]> git.mxchange.org Git - simgear.git/blob - simgear/timing/sg_time.hxx
805eeffa6369467ce05bde5a1a2d929fb64301b8
[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  */
55
56 class SGTime {
57
58 private:
59     // tzContainer stores all the current Timezone control points/
60     TimezoneContainer* tzContainer;
61
62     // Points to the current local timezone name;
63     char *zonename;
64
65     // Unix "calendar" time in seconds
66     time_t cur_time;
67
68     // Break down of equivalent GMT time
69     struct tm *gmt;
70
71     // offset of local time relative to GMT
72     time_t local_offset;
73
74     // Julian date
75     double jd;
76
77     // modified Julian date
78     double mjd;
79
80     // side real time at prime meridian
81     double gst;
82
83     // local sidereal time
84     double lst;
85
86     // the difference between the precise / expensive sidereal time
87     // algorithm result and the quick course result.  course_gst +
88     // gst_diff has pretty good accuracy over the span of a couple hours
89     double gst_diff;
90
91 public:
92
93     /** Default constructor */
94     SGTime();
95
96     /**
97      * Create an instance based on a specified position and data file path.
98      * @param lon current longitude
99      * @param lat current latitude
100      * @param root root path point to data file location (timezone, etc.)
101      */
102     SGTime( double lon, double lat, const string& root );
103
104     /**
105      * Create an instance given a data file path
106      * @param root root path point to data file location (timezone, etc.)
107      */
108     SGTime( const string& root );
109
110     /** Destructor */
111     ~SGTime();
112
113     /** 
114      * Update the time related variables.
115      * @param lon current longitude
116      * @param lat current latitude
117      * @param warp an optional time offset specified in seconds.  This
118      *        allows us to advance or rewind "time" if we choose to.
119      */
120     void update( double lon, double lat, long int warp = 0 );
121
122     // Given lon/lat, update timezone information and local_offset
123     void updateLocal( double lon, double lat, const string& root );
124
125     inline time_t get_cur_time() const { return cur_time; };
126     inline char* get_zonename() const { return zonename; }
127     inline struct tm* getGmt()const { return gmt; };
128     inline double getJD() const { return jd; };
129     inline double getMjd() const { return mjd; };
130     inline double getLst() const { return lst; };
131     inline double getGst() const { return gst; };
132 };
133
134
135 // Some useful utility functions that don't make sense to be part of
136 // the SGTime class
137
138 // Return unix time in seconds for the given data (relative to GMT)
139 time_t sgTimeGetGMT(int year, int month, int day, 
140                     int hour, int minute, int second);
141
142 // this is just a wrapper
143 inline time_t sgTimeGetGMT(struct tm* the_time) {
144     // printf("Using: %24s as input\n", asctime(the_time));
145     return sgTimeGetGMT(the_time->tm_year,
146                         the_time->tm_mon,
147                         the_time->tm_mday,
148                         the_time->tm_hour,
149                         the_time->tm_min,
150                         the_time->tm_sec);
151 }
152
153 // given a date in months, mn, days, dy, years, yr, return the
154 // modified Julian date (number of days elapsed since 1900 jan 0.5),
155 // mjd.  Adapted from Xephem.
156 double sgTimeCalcMJD(int mn, double dy, int yr);
157
158 // given an mjd, calculate greenwich mean sidereal time, gst
159 double sgTimeCalcGST( double mjd );
160
161 // format time
162 char* sgTimeFormatTime( const struct tm* p, char* buf );
163
164
165 #endif // _SG_TIME_HXX