]> git.mxchange.org Git - simgear.git/blob - simgear/timing/sg_time.hxx
SG-ified logstream.
[simgear.git] / simgear / timing / sg_time.hxx
1 // sg_time.hxx -- data structures and routines for managing time related stuff.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24
25 #ifndef _SG_TIME_HXX
26 #define _SG_TIME_HXX
27
28
29 #ifndef __cplusplus                                                          
30 # error This library requires C++
31 #endif                                   
32
33
34 #ifdef HAVE_CONFIG_H
35 #  include <config.h>
36 #endif
37
38 #include <simgear/compiler.h>
39
40 #ifdef SG_HAVE_STD_INCLUDES
41 #  include <ctime>
42 #else
43 #  include <time.h>
44 #endif
45
46 #include <simgear/timing/timezone.h>
47
48
49 // Define a structure containing time parameters
50 class SGTime {
51
52 private:
53     // tzContainer stores all the current Timezone control points/
54     TimezoneContainer* tzContainer;
55
56     // Points to the current local timezone name;
57     char *zonename;
58
59     // Unix "calendar" time in seconds
60     time_t cur_time;
61
62     // Break down of equivalent GMT time
63     struct tm *gmt;
64
65     // offset of local time relative to GMT
66     time_t local_offset;
67
68     // Julian date
69     double jd;
70
71     // modified Julian date
72     double mjd;
73
74     // side real time at prime meridian
75     double gst;
76
77     // local sidereal time
78     double lst;
79
80     // the difference between the precise / expensive sidereal time
81     // algorithm result and the quick course result.  course_gst +
82     // gst_diff has pretty good accuracy over the span of a couple hours
83     double gst_diff;
84
85 public:
86
87     SGTime( double lon, double lat, const string& root );
88     SGTime( const string& root );
89     SGTime();
90     ~SGTime();
91
92     // Update the time related variables
93     void update( double lon, double lat, long int warp = 0 );
94
95     // Given lon/lat, update timezone information and local_offset
96     void updateLocal( double lon, double lat, const string& root );
97
98     inline time_t get_cur_time() const { return cur_time; };
99     inline char* get_zonename() const { return zonename; }
100     inline struct tm* getGmt()const { return gmt; };
101     inline double getJD() const { return jd; };
102     inline double getMjd() const { return mjd; };
103     inline double getLst() const { return lst; };
104     inline double getGst() const { return gst; };
105 };
106
107
108 // Some useful utility functions that don't make sense to be part of
109 // the SGTime class
110
111 // Return unix time in seconds for the given data (relative to GMT)
112 time_t sgTimeGetGMT(int year, int month, int day, 
113                     int hour, int minute, int second);
114
115 // this is just a wrapper
116 inline time_t sgTimeGetGMT(struct tm* the_time) {
117     // printf("Using: %24s as input\n", asctime(the_time));
118     return sgTimeGetGMT(the_time->tm_year,
119                         the_time->tm_mon,
120                         the_time->tm_mday,
121                         the_time->tm_hour,
122                         the_time->tm_min,
123                         the_time->tm_sec);
124 }
125
126 // given a date in months, mn, days, dy, years, yr, return the
127 // modified Julian date (number of days elapsed since 1900 jan 0.5),
128 // mjd.  Adapted from Xephem.
129 double sgTimeCalcMJD(int mn, double dy, int yr);
130
131 // given an mjd, calculate greenwich mean sidereal time, gst
132 double sgTimeCalcGST( double mjd );
133
134 // format time
135 char* sgTimeFormatTime( const struct tm* p, char* buf );
136
137
138 #endif // _SG_TIME_HXX