1 |
/* |
2 |
* timer_beos.cpp - Time Manager emulation, BeOS 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 <KernelKit.h> |
22 |
|
23 |
#include "sysdeps.h" |
24 |
#include "timer.h" |
25 |
|
26 |
#define DEBUG 0 |
27 |
#include "debug.h" |
28 |
|
29 |
|
30 |
/* |
31 |
* Return microseconds since boot (64 bit) |
32 |
*/ |
33 |
|
34 |
void Microseconds(uint32 &hi, uint32 &lo) |
35 |
{ |
36 |
D(bug("Microseconds\n")); |
37 |
bigtime_t time = system_time(); |
38 |
hi = time >> 32; |
39 |
lo = time; |
40 |
} |
41 |
|
42 |
|
43 |
/* |
44 |
* Return local date/time in Mac format (seconds since 1.1.1904) |
45 |
*/ |
46 |
|
47 |
const uint32 TIME_OFFSET = 0x7c25b080; // Offset Mac->BeOS time in seconds |
48 |
|
49 |
uint32 TimerDateTime(void) |
50 |
{ |
51 |
time_t utc_now = time(NULL); |
52 |
long tz = timezone; |
53 |
time_t local_now = utc_now - tz; |
54 |
if (daylight) |
55 |
local_now += 3600; |
56 |
return (uint32)local_now + TIME_OFFSET; |
57 |
} |
58 |
|
59 |
|
60 |
/* |
61 |
* Get current time |
62 |
*/ |
63 |
|
64 |
void timer_current_time(tm_time_t &t) |
65 |
{ |
66 |
t = system_time(); |
67 |
} |
68 |
|
69 |
|
70 |
/* |
71 |
* Add times |
72 |
*/ |
73 |
|
74 |
void timer_add_time(tm_time_t &res, tm_time_t a, tm_time_t b) |
75 |
{ |
76 |
res = a + b; |
77 |
} |
78 |
|
79 |
|
80 |
/* |
81 |
* Subtract times |
82 |
*/ |
83 |
|
84 |
void timer_sub_time(tm_time_t &res, tm_time_t a, tm_time_t b) |
85 |
{ |
86 |
res = a - b; |
87 |
} |
88 |
|
89 |
|
90 |
/* |
91 |
* Compare times (<0: a < b, =0: a = b, >0: a > b) |
92 |
*/ |
93 |
|
94 |
int timer_cmp_time(tm_time_t a, tm_time_t b) |
95 |
{ |
96 |
tm_time_t r = a - b; |
97 |
return r < 0 ? -1 : (r > 0 ? 1 : 0); |
98 |
} |
99 |
|
100 |
|
101 |
/* |
102 |
* Convert Mac time value (>0: microseconds, <0: microseconds) to tm_time_t |
103 |
*/ |
104 |
|
105 |
void timer_mac2host_time(tm_time_t &res, int32 mactime) |
106 |
{ |
107 |
if (mactime > 0) |
108 |
res = mactime * 1000; // Time in milliseconds |
109 |
else |
110 |
res = -mactime; // Time in negative microseconds |
111 |
} |
112 |
|
113 |
|
114 |
/* |
115 |
* Convert positive tm_time_t to Mac time value (>0: microseconds, <0: microseconds) |
116 |
* A negative input value for hosttime results in a zero return value |
117 |
* As long as the microseconds value fits in 32 bit, it must not be converted to milliseconds! |
118 |
*/ |
119 |
|
120 |
int32 timer_host2mac_time(tm_time_t hosttime) |
121 |
{ |
122 |
if (hosttime < 0) |
123 |
return 0; |
124 |
else if (hosttime > 0x7fffffff) |
125 |
return hosttime / 1000; // Time in milliseconds |
126 |
else |
127 |
return -hosttime; // Time in negative microseconds |
128 |
} |