Debian Codegraph @

test_suite/unit_tests/general/test-mktime.cxx

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