File: test-mktime.cxx

package info (click to toggle)
flightgear 1:2016.4.4+dfsg-3+deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,512 kB
  • ctags: 71,727
  • sloc: cpp: 195,395; ansic: 177,152; sh: 13,661; perl: 4,475; python: 2,381; asm: 642; makefile: 398; java: 314; xml: 85
file content (151 lines) | stat: -rw-r--r-- 4,333 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// test the systems mktime() function


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <math.h>
#include <stdio.h>
#include <time.h>

#ifdef HAVE_SYS_TIMEB_H
#  include <sys/types.h>
#  include <sys/timeb.h> // for ftime() and struct timeb
#endif
#ifdef HAVE_UNISTD_H
#  include <unistd.h>    // for gettimeofday()
#endif
#ifdef HAVE_SYS_TIME_H
#  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
#endif

#define LST_MAGIC_TIME_1998 890481600

// For now we assume that if daylight is not defined in
// /usr/include/time.h that we have a machine with a BSD behaving
// mktime()
#if !defined(HAVE_DAYLIGHT)
#  define MK_TIME_IS_GMT 1
#endif


// Fix up timezone if using ftime()
long int fix_up_timezone( long int timezone_orig ) {
#if !defined( HAVE_GETTIMEOFDAY ) && defined( HAVE_FTIME )
    // ftime() needs a little extra help finding the current timezone
    struct timeb current;
    ftime(&current);
    return( current.timezone * 60 );
#else
    return( timezone_orig );
#endif
}


// Return time_t for Sat Mar 21 12:00:00 GMT
//
// I believe the mktime() has a SYSV vs. BSD behavior difference.
//
// The BSD style mktime() is nice because it returns its result
// assuming you have specified the input time in GMT
//
// The SYSV style mktime() is a pain because it returns its result
// assuming you have specified the input time in your local timezone.
// Therefore you have to go to extra trouble to convert back to GMT.
//
// If you are having problems with incorrectly positioned astronomical
// bodies, this is a really good place to start looking.

time_t get_start_gmt(int year) {
    struct tm mt;

    mt.tm_mon = 2;
    mt.tm_mday = 21;
    mt.tm_year = year;
    mt.tm_hour = 12;
    mt.tm_min = 0;
    mt.tm_sec = 0;
    mt.tm_isdst = -1; // let the system determine the proper time zone

#if defined( HAVE_TIMEGM ) 
    return ( timegm(&mt) );
#elif defined( MK_TIME_IS_GMT )
    return ( mktime(&mt) );
#else // ! defined ( MK_TIME_IS_GMT )

    // timezone seems to work as a proper offset for Linux & Solaris
#  if defined( __linux__ ) || defined(__sun) || defined( __CYGWIN__ )
#   define TIMEZONE_OFFSET_WORKS 1
#  endif

#if defined(__CYGWIN__)
#define TIMEZONE _timezone
#else
#define TIMEZONE timezone
#endif

    time_t start = mktime(&mt);

    printf("start1 = %ld\n", start);
    printf("start2 = %s", ctime(&start));
    printf("(tm_isdst = %d)\n", mt.tm_isdst);

    TIMEZONE = fix_up_timezone( TIMEZONE );
	
#  if defined( TIMEZONE_OFFSET_WORKS )
    printf("start = %ld, timezone = %ld\n", start, TIMEZONE);
    return( start - TIMEZONE );
#  else // ! defined( TIMEZONE_OFFSET_WORKS )

    daylight = mt.tm_isdst;
    if ( daylight > 0 ) {
	daylight = 1;
    } else if ( daylight < 0 ) {
	printf("OOOPS, problem in fg_time.cxx, no daylight savings info.\n");
    }

    long int offset = -(TIMEZONE / 3600 - daylight);

    printf("  Raw time zone offset = %ld\n", TIMEZONE);
    printf("  Daylight Savings = %d\n", daylight);
    printf("  Local hours from GMT = %ld\n", offset);
    
    long int start_gmt = start - TIMEZONE + (daylight * 3600);
    
    printf("  March 21 noon (CST) = %ld\n", start);

    return ( start_gmt );
#  endif // ! defined( TIMEZONE_OFFSET_WORKS )
#endif // ! defined ( MK_TIME_IS_GMT )
}


int main() {
    time_t start_gmt;

    start_gmt = get_start_gmt(98);


    if ( start_gmt == LST_MAGIC_TIME_1998 ) {
	printf("Time test = PASSED\n\n");
#ifdef HAVE_TIMEGM
	printf("You have timegm() which is just like mktime() except that\n");
	printf("it explicitely expects input in GMT ... lucky you!\n");
#elif MK_TIME_IS_GMT
	printf("You don't seem to have timegm(), but mktime() seems to\n");
	printf("assume input is GMT on your system ... I guess that works\n");
#else
	printf("mktime() assumes local time zone on your system, but we can\n");
	printf("compensate just fine.\n");
#endif
    } else {
	printf("Time test = FAILED\n\n");
	printf("There is likely a problem with mktime() on your system.\n");
        printf("This will cause the sun/moon/stars/planets to be in the\n");
	printf("wrong place in the sky and the rendered time of day will be\n");
	printf("incorrect.\n\n");
	printf("Please report this to http://www.flightgear.org/~curt so we can work to fix\n");
	printf("the problem on your platform.\n");
    }
}