]> git.mxchange.org Git - flightgear.git/blob - tests/test-mktime.cxx
7602d17424d4c25548ca9d96a4fbe5175be6416a
[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
26 // Fix up timezone if using ftime()
27 long int fix_up_timezone( long int timezone_orig ) {
28 #if !defined( HAVE_GETTIMEOFDAY ) && defined( HAVE_FTIME )
29     // ftime() needs a little extra help finding the current timezone
30     struct timeb current;
31     ftime(&current);
32     return( current.timezone * 60 );
33 #else
34     return( timezone_orig );
35 #endif
36 }
37
38
39 // Return time_t for Sat Mar 21 12:00:00 GMT
40 //
41 // I believe the mktime() has a SYSV vs. BSD behavior difference.
42 //
43 // The BSD style mktime() is nice because it returns its result
44 // assuming you have specified the input time in GMT
45 //
46 // The SYSV style mktime() is a pain because it returns its result
47 // assuming you have specified the input time in your local timezone.
48 // Therefore you have to go to extra trouble to convert back to GMT.
49 //
50 // If you are having problems with incorrectly positioned astronomical
51 // bodies, this is a really good place to start looking.
52
53 time_t get_start_gmt(int year) {
54     struct tm mt;
55
56     // For now we assume that if daylight is not defined in
57     // /usr/include/time.h that we have a machine with a BSD behaving
58     // mktime()
59 #   if !defined(HAVE_DAYLIGHT)
60 #       define MK_TIME_IS_GMT 1
61 #   endif
62
63     // timezone seems to work as a proper offset for Linux & Solaris
64 #   if defined( __linux__ ) || defined( __sun__ ) 
65 #       define TIMEZONE_OFFSET_WORKS 1
66 #   endif
67
68     mt.tm_mon = 2;
69     mt.tm_mday = 21;
70     mt.tm_year = year;
71     mt.tm_hour = 12;
72     mt.tm_min = 0;
73     mt.tm_sec = 0;
74     mt.tm_isdst = -1; // let the system determine the proper time zone
75
76 #   if defined( MK_TIME_IS_GMT )
77     return ( mktime(&mt) );
78 #   else // ! defined ( MK_TIME_IS_GMT )
79
80     long int start = mktime(&mt);
81
82     printf("start1 = %ld\n", start);
83     printf("start2 = %s", ctime(&start));
84     printf("(tm_isdst = %d)\n", mt.tm_isdst);
85
86     timezone = fix_up_timezone( timezone );
87
88 #   if defined( TIMEZONE_OFFSET_WORKS )
89     printf("start = %ld, timezone = %ld\n", start, timezone);
90     return( start - timezone );
91 #   else // ! defined( TIMEZONE_OFFSET_WORKS )
92
93     daylight = mt.tm_isdst;
94     if ( daylight > 0 ) {
95         daylight = 1;
96     } else if ( daylight < 0 ) {
97         printf("OOOPS, problem in fg_time.cxx, no daylight savings info.\n");
98     }
99
100     long int offset = -(timezone / 3600 - daylight);
101
102     printf("  Raw time zone offset = %ld\n", timezone);
103     printf("  Daylight Savings = %d\n", daylight);
104     printf("  Local hours from GMT = %ld\n", offset);
105     
106     long int start_gmt = start - timezone + (daylight * 3600);
107     
108     printf("  March 21 noon (CST) = %ld\n", start);
109
110     return ( start_gmt );
111 #   endif // ! defined( TIMEZONE_OFFSET_WORKS )
112 #   endif // ! defined ( MK_TIME_IS_GMT )
113 }
114
115
116 int main() {
117     time_t start_gmt;
118
119     start_gmt = get_start_gmt(98);
120
121
122     if ( start_gmt == LST_MAGIC_TIME_1998 ) {
123 #ifdef MK_TIME_IS_GMT
124         printf("mktime() assumes GMT on your system, lucky you!\n");
125 #else
126         printf("mktime() assumes local time zone on your system, but we can\n");
127         printf("compensate just fine.\n");
128 #endif
129     } else {
130         printf("There is likely a problem with mktime() on your system.\n");
131         printf("This will cause the sun/moon/stars/planets to be in the\n");
132         printf("wrong place in the sky and the rendered time of day will be\n");
133         printf("incorrect.\n\n");
134         printf("Please report this to curt@me.umn.edu so we can work to fix\n");
135         printf("the problem on your platform.\n");
136     }
137 }