ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/timer_unix.cpp
Revision: 1.2
Committed: 1999-10-14T16:05:18Z (24 years, 8 months ago) by cebix
Branch: MAIN
Changes since 1.1: +5 -4 lines
Log Message:
- corrected time zone handling in TimerDateTime()

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * timer_unix.cpp - Time Manager emulation, Unix specific stuff
3     *
4     * Basilisk II (C) 1997-1999 Christian Bauer
5     *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include "sysdeps.h"
22     #include "timer.h"
23    
24     #define DEBUG 0
25     #include "debug.h"
26    
27    
28     /*
29     * Return microseconds since boot (64 bit)
30     */
31    
32     void Microseconds(uint32 &hi, uint32 &lo)
33     {
34     D(bug("Microseconds\n"));
35     #ifdef HAVE_CLOCK_GETTIME
36     struct timespec t;
37     clock_gettime(CLOCK_REALTIME, &t);
38     uint64 tl = (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000;
39     #else
40     struct timeval t;
41     gettimeofday(&t, NULL);
42     uint64 tl = (uint64)t.tv_sec * 1000000 + t.tv_usec;
43     #endif
44     hi = tl >> 32;
45     lo = tl;
46     }
47    
48    
49     /*
50     * Return local date/time in Mac format (seconds since 1.1.1904)
51     */
52    
53 cebix 1.2 const uint32 TIME_OFFSET = 0x7c25b080; // Offset Mac->Unix time in seconds
54 cebix 1.1
55     uint32 TimerDateTime(void)
56     {
57     time_t uct_now = time(NULL);
58 cebix 1.2 long tz = timezone;
59     time_t local_now = uct_now - tz;
60     if (daylight)
61     local_now += 3600;
62 cebix 1.1 return (uint32)local_now + TIME_OFFSET;
63     }
64    
65    
66     /*
67     * Get current time
68     */
69    
70     void timer_current_time(tm_time_t &t)
71     {
72     #ifdef HAVE_CLOCK_GETTIME
73     clock_gettime(CLOCK_REALTIME, &t);
74     #else
75     gettimeofday(&t, NULL);
76     #endif
77     }
78    
79    
80     /*
81     * Add times
82     */
83    
84     void timer_add_time(tm_time_t &res, tm_time_t a, tm_time_t b)
85     {
86     #ifdef HAVE_CLOCK_GETTIME
87     res.tv_sec = a.tv_sec + b.tv_sec;
88     res.tv_nsec = a.tv_nsec + b.tv_nsec;
89     if (res.tv_nsec >= 1000000000) {
90     res.tv_sec++;
91     res.tv_nsec -= 1000000000;
92     }
93     #else
94     res.tv_sec = a.tv_sec + b.tv_sec;
95     res.tv_usec = a.tv_usec + b.tv_usec;
96     if (res.tv_usec >= 1000000) {
97     res.tv_sec++;
98     res.tv_usec -= 1000000;
99     }
100     #endif
101     }
102    
103    
104     /*
105     * Subtract times
106     */
107    
108     void timer_sub_time(tm_time_t &res, tm_time_t a, tm_time_t b)
109     {
110     #ifdef HAVE_CLOCK_GETTIME
111     res.tv_sec = a.tv_sec - b.tv_sec;
112     res.tv_nsec = a.tv_nsec - b.tv_nsec;
113     if (res.tv_nsec < 0) {
114     res.tv_sec--;
115     res.tv_nsec += 1000000000;
116     }
117     #else
118     res.tv_sec = a.tv_sec - b.tv_sec;
119     res.tv_usec = a.tv_usec - b.tv_usec;
120     if (res.tv_usec < 0) {
121     res.tv_sec--;
122     res.tv_usec += 1000000;
123     }
124     #endif
125     }
126    
127    
128     /*
129     * Compare times (<0: a < b, =0: a = b, >0: a > b)
130     */
131    
132     int timer_cmp_time(tm_time_t a, tm_time_t b)
133     {
134     #ifdef HAVE_CLOCK_GETTIME
135     if (a.tv_sec == b.tv_sec)
136     return a.tv_nsec - b.tv_nsec;
137     else
138     return a.tv_sec - b.tv_sec;
139     #else
140     if (a.tv_sec == b.tv_sec)
141     return a.tv_usec - b.tv_usec;
142     else
143     return a.tv_sec - b.tv_sec;
144     #endif
145     }
146    
147    
148     /*
149     * Convert Mac time value (>0: microseconds, <0: microseconds) to tm_time_t
150     */
151    
152     void timer_mac2host_time(tm_time_t &res, int32 mactime)
153     {
154     #ifdef HAVE_CLOCK_GETTIME
155     if (mactime > 0) {
156     // Time in milliseconds
157     res.tv_sec = mactime / 1000;
158     res.tv_nsec = (mactime % 1000) * 1000000;
159     } else {
160     // Time in negative microseconds
161     res.tv_sec = -mactime / 1000000;
162     res.tv_nsec = (-mactime % 1000000) * 1000;
163     }
164     #else
165     if (mactime > 0) {
166     // Time in milliseconds
167     res.tv_sec = mactime / 1000;
168     res.tv_usec = (mactime % 1000) * 1000;
169     } else {
170     // Time in negative microseconds
171     res.tv_sec = -mactime / 1000000;
172     res.tv_usec = -mactime % 1000000;
173     }
174     #endif
175     }
176    
177    
178     /*
179     * Convert positive tm_time_t to Mac time value (>0: microseconds, <0: microseconds)
180     * A negative input value for hosttime results in a zero return value
181     * As long as the microseconds value fits in 32 bit, it must not be converted to milliseconds!
182     */
183    
184     int32 timer_host2mac_time(tm_time_t hosttime)
185     {
186     if (hosttime.tv_sec < 0)
187     return 0;
188     else {
189     #ifdef HAVE_CLOCK_GETTIME
190     uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_nsec / 1000;
191     #else
192     uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_usec;
193     #endif
194     if (t > 0x7fffffff)
195     return t / 1000; // Time in milliseconds
196     else
197     return -t; // Time in negative microseconds
198     }
199     }