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