Debian Codegraph @

src/common/bnettime.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999 Rob Crittenden ([email protected])
3 * Copyright (C) 1999,2000 Ross Combs ([email protected])
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #define BNETTIME_INTERNAL_ACCESS
20 #include "common/setup_before.h"
21 #include "common/bnettime.h"
22
23 #include <cstdio>
24 #include <cstring>
25 #include <cerrno>
26 #include <ctime>
27
28 #include "compat/gettimeofday.h"
29 #include "common/eventlog.h"
30 #include "common/bn_type.h"
31 #include "common/setup_after.h"
32
33
34 namespace pvpgn
35 {
36
37 /*
38 * By comparing the hex numbers from packet dumps with the times displayed
39 * on the login screen, bnettime seems to be in units of 10E-7 seconds. It
40 * is stored in a 64bit value, and represented as two distinct numbers when
41 * it is used as a string in the protocol.
42 *
43 * examples (client in GMT timezone):
44 * 11/04/99 05:16 0x01 "29304451 3046165090"
45 * 01/03/99 11:19 0x01 "29243146 3825346784"
46 * 08/02/99 21:28 0x01 "29285677 4283311890"
47 * 12/12/99 01:55 0x01 "29312068 151587081"
48 *
49 * The top number seems to be in units of about 7 minutes because the bottom
50 * number rolls over every 2^32 10E-7 seconds, which is:
51 * (2^32)*.0000001
52 * 429.4967296 // seconds
53 * 429/60
54 * 7.15827882666666666666 // minutes
55 *
56 * The epoch was determined using a binary search looking for Jan 1, 1970
57 * (GMT) to be displayed after setting the last game time to a pair of
58 * numbers. It was determined through all 64 bits by looking for the exact
59 * number where adding one pushes it over to 00:01. It was determined to be:
60 * "27111902 3577643008"
61 *
62 * It appears that this is the same format as is used in MS-DOS to store file
63 * times. The format is a 64 bit number telling how many 100 nanosecond
64 * intervals since Jan 1, 1601.
65 *
66 * If I was clever I should be able to do this without floating point math.
67 * I don't feel like being clever though :) (Refer to the function
68 * DOSFS_UnixTimeToFileTime() in dosfs.c in the WINE source code for an
69 * example that I wish I had seen before writing this).
70 *
71 * Update... from looking at the WINE sources VAP has determined that these
72 * are actually in MS-Windows FileTime format.
73 *
74 * "In the WINE listing, you can see that
75 * FileTime = (UnixTime * 10 000 000) + 116 444 736 000 000 000 + remainder
76 * These dates are represented from 1 Jan 1601."
77 */
78
79 #define SEC_PER_USEC .0000001
80
81 #define UNIX_EPOCH 11644473600. /* seconds the Unix epoch is from the bnettime epoch */
82
83 #define SEC_PER_UPPER 429.4967296 /* (2^32)*10E-7 */
84 #define SEC_PER_LOWER .0000001
85
86 #define UPPER_PER_SEC .00232830643653869628 /* 10E7/(2^32) */
87 #define LOWER_PER_SEC 10000000.
88
89
90 extern t_bnettime secs_to_bnettime(double secs)
91 {
92 t_bnettime temp;
93
94 temp.u = (unsigned int)(secs*UPPER_PER_SEC);
95 temp.l = (unsigned int)(secs*LOWER_PER_SEC);
96
97 return temp;
98 }
99
100
101 extern double bnettime_to_secs(t_bnettime bntime)
102 {
103 return ((double)bntime.u)*SEC_PER_UPPER +
104 ((double)bntime.l)*SEC_PER_LOWER;
105 }
106
107
108 extern t_bnettime time_to_bnettime(std::time_t stdtime, unsigned int usec)
109 {
110 return secs_to_bnettime((double)stdtime + (double)usec*SEC_PER_USEC + UNIX_EPOCH);
111 }
112
113
114 extern std::time_t bnettime_to_time(t_bnettime bntime)
115 {
116 return (std::time_t)(bnettime_to_secs(bntime) - UNIX_EPOCH);
117 }
118
119
120 /* return current time as bnettime */
121 extern t_bnettime bnettime(void)
122 {
123 struct timeval tv;
124
125 if (gettimeofday(&tv, NULL) < 0)
126 {
127 eventlog(eventlog_level_error, __FUNCTION__, "could not get time (gettimeofday: {})", std::strerror(errno));
128 return time_to_bnettime(std::time(NULL), 0);
129 }
130 return time_to_bnettime((std::time_t)tv.tv_sec, tv.tv_usec);
131 }
132
133
134 /* FIXME: the string functions here should probably go in account_wrap */
135 extern char const * bnettime_get_str(t_bnettime bntime)
136 {
137 static char temp[1024];
138
139 std::sprintf(temp, "%u %u", bntime.u, bntime.l);
140
141 return temp;
142 }
143
144
145 extern int bnettime_set_str(t_bnettime * bntime, char const * timestr)
146 {
147 if (!bntime)
148 {
149 eventlog(eventlog_level_error, __FUNCTION__, "got NULL bntime");
150 return -1;
151 }
152 if (!timestr)
153 {
154 eventlog(eventlog_level_error, __FUNCTION__, "got NULL timestr");
155 return -1;
156 }
157
158 if (std::sscanf(timestr, "%u %u", &bntime->u, &bntime->l) != 2)
159 return -1;
160
161 return 0;
162 }
163
164
165 extern void bnettime_to_bn_long(t_bnettime in, bn_long * out)
166 {
167 if (!out)
168 {
169 eventlog(eventlog_level_error, __FUNCTION__, "got NULL out");
170 return;
171 }
172
173 bn_long_set_a_b(out, in.u, in.l);
174 }
175
176
177 extern void bn_long_to_bnettime(bn_long in, t_bnettime * out)
178 {
179 if (!out)
180 {
181 eventlog(eventlog_level_error, __FUNCTION__, "got NULL out");
182 return;
183 }
184
185 out->u = bn_long_get_a(in);
186 out->l = bn_long_get_b(in);
187 }
188
189
190 extern int local_tzbias(void) /* in minutes */
191 {
192 std::time_t now;
193 std::time_t test;
194 std::time_t testloc;
195 struct std::tm * temp;
196
197 now = std::time(NULL);
198
199 if (!(temp = std::gmtime(&now)))
200 return 0;
201 if ((test = std::mktime(temp)) == (std::time_t)(-1))
202 return 0;
203
204 if (!(temp = std::localtime(&now)))
205 return 0;
206 if ((testloc = std::mktime(temp)) == (std::time_t)(-1))
207 return 0;
208
209 if (testloc > test) /* std::time_t is probably unsigned... */
210 return -(int)(testloc - test) / 60;
211 return (int)(test - testloc) / 60;
212 }
213
214
215 extern t_bnettime bnettime_add_tzbias(t_bnettime bntime, int tzbias)
216 {
217 return secs_to_bnettime(bnettime_to_secs(bntime) - (double)tzbias*60.0);
218 }
219
220 }