]> git.mxchange.org Git - flightgear.git/blob - tests/test-mktime.cxx
Including missing OSG plugins, use LZMA compression
[flightgear.git] / tests / test-mktime.cxx
1 // test the systems mktime() function
2
3
4 #ifdef HAVE_CONFIG_H
5 #  include <config.h>
6 #endif
7
8 #include <math.h>
9 #include <stdio.h>
10 #include <time.h>
11
12 #ifdef HAVE_SYS_TIMEB_H
13 #  include <sys/types.h>
14 #  include <sys/timeb.h> // for ftime() and struct timeb
15 #endif
16 #ifdef HAVE_UNISTD_H
17 #  include <unistd.h>    // for gettimeofday()
18 #endif
19 #ifdef HAVE_SYS_TIME_H
20 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
21 #endif
22
23 #define LST_MAGIC_TIME_1998 890481600
24
25 // For now we assume that if daylight is not defined in
26 // /usr/include/time.h that we have a machine with a BSD behaving
27 // mktime()
28 #if !defined(HAVE_DAYLIGHT)
29 #  define MK_TIME_IS_GMT 1
30 #endif
31
32
33 // Fix up timezone if using ftime()
34 long int fix_up_timezone( long int timezone_orig ) {
35 #if !defined( HAVE_GETTIMEOFDAY ) && defined( HAVE_FTIME )
36     // ftime() needs a little extra help finding the current timezone
37     struct timeb current;
38     ftime(&current);
39     return( current.timezone * 60 );
40 #else
41     return( timezone_orig );
42 #endif
43 }
44
45
46 // Return time_t for Sat Mar 21 12:00:00 GMT
47 //
48 // I believe the mktime() has a SYSV vs. BSD behavior difference.
49 //
50 // The BSD style mktime() is nice because it returns its result
51 // assuming you have specified the input time in GMT
52 //
53 // The SYSV style mktime() is a pain because it returns its result
54 // assuming you have specified the input time in your local timezone.
55 // Therefore you have to go to extra trouble to convert back to GMT.
56 //
57 // If you are having problems with incorrectly positioned astronomical
58 // bodies, this is a really good place to start looking.
59
60 time_t get_start_gmt(int year) {
61     struct tm mt;
62
63     mt.tm_mon = 2;
64     mt.tm_mday = 21;
65     mt.tm_year = year;
66     mt.tm_hour = 12;
67     mt.tm_min = 0;
68     mt.tm_sec = 0;
69     mt.tm_isdst = -1; // let the system determine the proper time zone
70
71 #if defined( HAVE_TIMEGM ) 
72     return ( timegm(&mt) );
73 #elif defined( MK_TIME_IS_GMT )
74     return ( mktime(&mt) );
75 #else // ! defined ( MK_TIME_IS_GMT )
76
77     // timezone seems to work as a proper offset for Linux & Solaris
78 #  if defined( __linux__ ) || defined(__sun) || defined( __CYGWIN__ )
79 #   define TIMEZONE_OFFSET_WORKS 1
80 #  endif
81
82 #if defined(__CYGWIN__)
83 #define TIMEZONE _timezone
84 #else
85 #define TIMEZONE timezone
86 #endif
87
88     time_t start = mktime(&mt);
89
90     printf("start1 = %ld\n", start);
91     printf("start2 = %s", ctime(&start));
92     printf("(tm_isdst = %d)\n", mt.tm_isdst);
93
94     TIMEZONE = fix_up_timezone( TIMEZONE );
95         
96 #  if defined( TIMEZONE_OFFSET_WORKS )
97     printf("start = %ld, timezone = %ld\n", start, TIMEZONE);
98     return( start - TIMEZONE );
99 #  else // ! defined( TIMEZONE_OFFSET_WORKS )
100
101     daylight = mt.tm_isdst;
102     if ( daylight > 0 ) {
103         daylight = 1;
104     } else if ( daylight < 0 ) {
105         printf("OOOPS, problem in fg_time.cxx, no daylight savings info.\n");
106     }
107
108     long int offset = -(TIMEZONE / 3600 - daylight);
109
110     printf("  Raw time zone offset = %ld\n", TIMEZONE);
111     printf("  Daylight Savings = %d\n", daylight);
112     printf("  Local hours from GMT = %ld\n", offset);
113     
114     long int start_gmt = start - TIMEZONE + (daylight * 3600);
115     
116     printf("  March 21 noon (CST) = %ld\n", start);
117
118     return ( start_gmt );
119 #  endif // ! defined( TIMEZONE_OFFSET_WORKS )
120 #endif // ! defined ( MK_TIME_IS_GMT )
121 }
122
123
124 int main() {
125     time_t start_gmt;
126
127     start_gmt = get_start_gmt(98);
128
129
130     if ( start_gmt == LST_MAGIC_TIME_1998 ) {
131         printf("Time test = PASSED\n\n");
132 #ifdef HAVE_TIMEGM
133         printf("You have timegm() which is just like mktime() except that\n");
134         printf("it explicitely expects input in GMT ... lucky you!\n");
135 #elif MK_TIME_IS_GMT
136         printf("You don't seem to have timegm(), but mktime() seems to\n");
137         printf("assume input is GMT on your system ... I guess that works\n");
138 #else
139         printf("mktime() assumes local time zone on your system, but we can\n");
140         printf("compensate just fine.\n");
141 #endif
142     } else {
143         printf("Time test = FAILED\n\n");
144         printf("There is likely a problem with mktime() on your system.\n");
145         printf("This will cause the sun/moon/stars/planets to be in the\n");
146         printf("wrong place in the sky and the rendered time of day will be\n");
147         printf("incorrect.\n\n");
148         printf("Please report this to http://www.flightgear.org/~curt so we can work to fix\n");
149         printf("the problem on your platform.\n");
150     }
151 }