File: wtime.cpp

package info (click to toggle)
0ad 0.0.23.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,412 kB
  • sloc: cpp: 245,162; ansic: 200,249; javascript: 19,244; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (622 lines) | stat: -rw-r--r-- 13,552 bytes parent folder | download | duplicates (2)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/* Copyright (C) 2010 Wildfire Games.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * emulate POSIX time functionality on Windows.
 */

// note: clock_gettime et al. have been removed. callers should use the
// WHRT directly, rather than needlessly translating s -> ns -> s,
// which costs time and accuracy.

#include "precompiled.h"
#include "lib/sysdep/os/win/wposix/wtime.h"

#include "lib/sysdep/os/win/wposix/wposix_internal.h"
#include "lib/sysdep/os/win/whrt/whrt.h"

WINIT_REGISTER_MAIN_INIT(wtime_Init);	// whrt -> wtime

// NT system time and FILETIME are hectonanoseconds since Jan. 1, 1601 UTC.
// SYSTEMTIME is a struct containing month, year, etc.

static const long _1e3 = 1000;
static const long _1e6 = 1000000;
static const long _1e7 = 10000000;
static const i64  _1e9 = 1000000000;


//
// FILETIME -> time_t routines; used by wposix filetime_to_time_t wrapper.
//

// hectonanoseconds between Windows and POSIX epoch
static const u64 posix_epoch_hns = 0x019DB1DED53E8000;

// this function avoids the pitfall of casting FILETIME* to u64*,
// which is not safe due to differing alignment guarantees!
// on some platforms, that would result in an exception.
static u64 u64_from_FILETIME(const FILETIME* ft)
{
	return u64_from_u32(ft->dwHighDateTime, ft->dwLowDateTime);
}


// convert UTC FILETIME to seconds-since-1970 UTC:
// we just have to subtract POSIX epoch and scale down to units of seconds.
//
// used by wfilesystem.
//
// note: RtlTimeToSecondsSince1970 isn't officially documented,
// so don't use that.
time_t wtime_utc_filetime_to_time_t(FILETIME* ft)
{
	u64 hns = u64_from_FILETIME(ft);
	u64 s = (hns - posix_epoch_hns) / _1e7;
	return (time_t)(s & 0xFFFFFFFF);
}


//-----------------------------------------------------------------------------

// system clock at startup [nanoseconds since POSIX epoch]
// note: the HRT starts at 0; any increase by the time we get here
// just makes our notion of the start time more accurate)
static i64 stInitial_ns;

static void LatchInitialSystemTime()
{
	FILETIME ft;
	GetSystemTimeAsFileTime(&ft);
	const u64 hns = u64_from_FILETIME(&ft);
	stInitial_ns = (hns - posix_epoch_hns) * 100;
}

// return nanoseconds since POSIX epoch.
// algorithm: add current HRT value to the startup system time
static i64 CurrentSystemTime_ns()
{
	const i64 ns = stInitial_ns + i64(whrt_Time() * _1e9);
	return ns;
}

static timespec TimespecFromNs(i64 ns)
{
	timespec ts;
	ts.tv_sec = (time_t)((ns / _1e9) & 0xFFFFFFFF);
	ts.tv_nsec = (long)(ns % _1e9);
	return ts;
}

static size_t MsFromTimespec(const timespec& ts)
{
	i64 ms = ts.tv_sec;	// avoid overflow
	ms *= _1e3;
	ms += ts.tv_nsec / _1e6;
	return size_t(ms);
}


//-----------------------------------------------------------------------------

int clock_gettime(clockid_t clock, struct timespec* ts)
{
	ENSURE(clock == CLOCK_REALTIME || clock == CLOCK_MONOTONIC);

	const i64 ns = CurrentSystemTime_ns();
	*ts = TimespecFromNs(ns);
	return 0;
}


int clock_getres(clockid_t clock, struct timespec* ts)
{
	ENSURE(clock == CLOCK_REALTIME || clock == CLOCK_MONOTONIC);

	const double resolution = whrt_Resolution();
	const i64 ns = i64(resolution * 1e9);
	*ts = TimespecFromNs(ns);
	return 0;
}


int nanosleep(const struct timespec* rqtp, struct timespec* /* rmtp */)
{
	const DWORD ms = (DWORD)MsFromTimespec(*rqtp);
	if(ms)
		Sleep(ms);
	return 0;
}


unsigned sleep(unsigned sec)
{
	// warn if overflow would result (it would be insane to ask for
	// such lengthy sleep timeouts, but still)
	ENSURE(sec < std::numeric_limits<size_t>::max()/1000);

	const DWORD ms = sec * 1000;
	if(ms)
		Sleep(ms);
	return sec;
}


int usleep(useconds_t us)
{
	ENSURE(us < _1e6);

	const DWORD ms = us/1000;
	if(ms)
		Sleep(ms);
	return 0;
}


//-----------------------------------------------------------------------------
// strptime from NetBSD

/*
* Copyright (c) 1999 Kungliga Tekniska Hogskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of KTH nor the names of its contributors may be
*    used to endorse or promote products derived from this software without
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

static const char *abb_weekdays[] = {
	"Sun",
	"Mon",
	"Tue",
	"Wed",
	"Thu",
	"Fri",
	"Sat",
	NULL
};

static const char *full_weekdays[] = {
	"Sunday",
	"Monday",
	"Tuesday",
	"Wednesday",
	"Thursday",
	"Friday",
	"Saturday",
	NULL
};

static const char *abb_month[] = {
	"Jan",
	"Feb",
	"Mar",
	"Apr",
	"May",
	"Jun",
	"Jul",
	"Aug",
	"Sep",
	"Oct",
	"Nov",
	"Dec",
	NULL
};

static const char *full_month[] = {
	"January",
	"February",
	"Mars",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December",
	NULL,
};

static const char *ampm[] = {
	"am",
	"pm",
	NULL
};

/*
* Try to match `*buf' to one of the strings in `strs'.  Return the
* index of the matching string (or -1 if none).  Also advance buf.
*/

static int
match_string (const char **buf, const char **strs)
{
	int i = 0;

	for (i = 0; strs[i] != NULL; ++i) {
		size_t len = strlen (strs[i]);

		if (strncasecmp (*buf, strs[i], len) == 0) {
			*buf += len;
			return i;
		}
	}
	return -1;
}

/*
* tm_year is relative this year */

const int tm_year_base = 1900;

/*
* Return TRUE iff `year' was a leap year.
*/

static int
is_leap_year (int year)
{
	return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
}

/*
* Return the weekday [0,6] (0 = Sunday) of the first day of `year'
*/

static int
first_day (int year)
{
	int ret = 4;

	for (; year > 1970; --year)
		ret = (ret + 365 + is_leap_year (year) ? 1 : 0) % 7;
	return ret;
}

/*
* Set `timeptr' given `wnum' (week number [0, 53])
*/

static void
set_week_number_sun (struct tm *timeptr, int wnum)
{
	int fday = first_day (timeptr->tm_year + tm_year_base);

	timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
	if (timeptr->tm_yday < 0) {
		timeptr->tm_wday = fday;
		timeptr->tm_yday = 0;
	}
}

/*
* Set `timeptr' given `wnum' (week number [0, 53])
*/

static void
set_week_number_mon (struct tm *timeptr, int wnum)
{
	int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;

	timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
	if (timeptr->tm_yday < 0) {
		timeptr->tm_wday = (fday + 1) % 7;
		timeptr->tm_yday = 0;
	}
}

/*
* Set `timeptr' given `wnum' (week number [0, 53])
*/

static void
set_week_number_mon4 (struct tm *timeptr, int wnum)
{
	int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
	int offset = 0;

	if (fday < 4)
		offset += 7;

	timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
	if (timeptr->tm_yday < 0) {
		timeptr->tm_wday = fday;
		timeptr->tm_yday = 0;
	}
}

/*
*
*/

char *
strptime (const char *buf, const char *format, struct tm *timeptr)
{
	char c;

	for (; (c = *format) != '\0'; ++format) {
		char *s;
		int ret;

		if (isspace (c)) {
			while (isspace (*buf))
				++buf;
		} else if (c == '%' && format[1] != '\0') {
			c = *++format;
			if (c == 'E' || c == 'O')
				c = *++format;
			switch (c) {
		case 'A' :
			ret = match_string (&buf, full_weekdays);
			if (ret < 0)
				return NULL;
			timeptr->tm_wday = ret;
			break;
		case 'a' :
			ret = match_string (&buf, abb_weekdays);
			if (ret < 0)
				return NULL;
			timeptr->tm_wday = ret;
			break;
		case 'B' :
			ret = match_string (&buf, full_month);
			if (ret < 0)
				return NULL;
			timeptr->tm_mon = ret;
			break;
		case 'b' :
		case 'h' :
			ret = match_string (&buf, abb_month);
			if (ret < 0)
				return NULL;
			timeptr->tm_mon = ret;
			break;
		case 'C' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_year = (ret * 100) - tm_year_base;
			buf = s;
			break;
		case 'c' :
			abort ();
		case 'D' :		/* %m/%d/%y */
			s = strptime (buf, "%m/%d/%y", timeptr);
			if (s == NULL)
				return NULL;
			buf = s;
			break;
		case 'd' :
		case 'e' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_mday = ret;
			buf = s;
			break;
		case 'H' :
		case 'k' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_hour = ret;
			buf = s;
			break;
		case 'I' :
		case 'l' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			if (ret == 12)
				timeptr->tm_hour = 0;
			else
				timeptr->tm_hour = ret;
			buf = s;
			break;
		case 'j' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_yday = ret - 1;
			buf = s;
			break;
		case 'm' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_mon = ret - 1;
			buf = s;
			break;
		case 'M' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_min = ret;
			buf = s;
			break;
		case 'n' :
			if (*buf == '\n')
				++buf;
			else
				return NULL;
			break;
		case 'p' :
			ret = match_string (&buf, ampm);
			if (ret < 0)
				return NULL;
			if (timeptr->tm_hour == 0) {
				if (ret == 1)
					timeptr->tm_hour = 12;
			} else
				timeptr->tm_hour += 12;
			break;
		case 'r' :		/* %I:%M:%S %p */
			s = strptime (buf, "%I:%M:%S %p", timeptr);
			if (s == NULL)
				return NULL;
			buf = s;
			break;
		case 'R' :		/* %H:%M */
			s = strptime (buf, "%H:%M", timeptr);
			if (s == NULL)
				return NULL;
			buf = s;
			break;
		case 'S' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_sec = ret;
			buf = s;
			break;
		case 't' :
			if (*buf == '\t')
				++buf;
			else
				return NULL;
			break;
		case 'T' :		/* %H:%M:%S */
		case 'X' :
			s = strptime (buf, "%H:%M:%S", timeptr);
			if (s == NULL)
				return NULL;
			buf = s;
			break;
		case 'u' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_wday = ret - 1;
			buf = s;
			break;
		case 'w' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_wday = ret;
			buf = s;
			break;
		case 'U' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			set_week_number_sun (timeptr, ret);
			buf = s;
			break;
		case 'V' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			set_week_number_mon4 (timeptr, ret);
			buf = s;
			break;
		case 'W' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			set_week_number_mon (timeptr, ret);
			buf = s;
			break;
		case 'x' :
			s = strptime (buf, "%Y:%m:%d", timeptr);
			if (s == NULL)
				return NULL;
			buf = s;
			break;
		case 'y' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			if (ret < 70)
				timeptr->tm_year = 100 + ret;
			else
				timeptr->tm_year = ret;
			buf = s;
			break;
		case 'Y' :
			ret = strtol (buf, &s, 10);
			if (s == buf)
				return NULL;
			timeptr->tm_year = ret - tm_year_base;
			buf = s;
			break;
		case 'Z' :
			abort ();
		case '\0' :
			--format;
			/* FALLTHROUGH */
		case '%' :
			if (*buf == '%')
				++buf;
			else
				return NULL;
			break;
		default :
			if (*buf == '%' || *++buf == c)
				++buf;
			else
				return NULL;
			break;
			}
		} else {
			if (*buf == c)
				++buf;
			else
				return NULL;
		}
	}
	return (char *)buf;
}


//-----------------------------------------------------------------------------

static Status wtime_Init()
{
	LatchInitialSystemTime();
	return INFO::OK;
}