xref: /PHP-8.4/win32/time.c (revision ec2655f8)
1 /*****************************************************************************
2  *                                                                           *
3  * DH_TIME.C                                                                 *
4  *                                                                           *
5  * Freely redistributable and modifiable.  Use at your own risk.             *
6  *                                                                           *
7  * Copyright 1994 The Downhill Project                                       *
8  *
9  * Modified by Shane Caraveo for use with PHP
10  *
11  *****************************************************************************/
12 
13 /* Include stuff ************************************************************ */
14 
15 #include <config.w32.h>
16 
17 #include "time.h"
18 #include "unistd.h"
19 #include "signal.h"
20 #include <windows.h>
21 #include <winbase.h>
22 #include <mmsystem.h>
23 #include <errno.h>
24 #include "php_win32_globals.h"
25 
getfilesystemtime(struct timeval * tv)26 static zend_always_inline void getfilesystemtime(struct timeval *tv)
27 {/*{{{*/
28 	FILETIME ft;
29 	unsigned __int64 ff = 0;
30 	ULARGE_INTEGER fft;
31 
32 	GetSystemTimePreciseAsFileTime(&ft);
33 
34 	/*
35 	 * Do not cast a pointer to a FILETIME structure to either a
36 	 * ULARGE_INTEGER* or __int64* value because it can cause alignment faults on 64-bit Windows.
37 	 * via  http://technet.microsoft.com/en-us/library/ms724284(v=vs.85).aspx
38 	 */
39 	fft.HighPart = ft.dwHighDateTime;
40 	fft.LowPart = ft.dwLowDateTime;
41 	ff = fft.QuadPart;
42 
43 	ff /= 10Ui64; /* convert to microseconds */
44 	ff -= 11644473600000000Ui64; /* convert to unix epoch */
45 
46 	tv->tv_sec = (long)(ff / 1000000Ui64);
47 	tv->tv_usec = (long)(ff % 1000000Ui64);
48 }/*}}}*/
49 
gettimeofday(struct timeval * time_Info,struct timezone * timezone_Info)50 PHPAPI int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
51 {/*{{{*/
52 	/* Get the time, if they want it */
53 	if (time_Info != NULL) {
54 		getfilesystemtime(time_Info);
55 	}
56 	/* Get the timezone, if they want it */
57 	if (timezone_Info != NULL) {
58 		_tzset();
59 		timezone_Info->tz_minuteswest = _timezone;
60 		timezone_Info->tz_dsttime = _daylight;
61 	}
62 	/* And return */
63 	return 0;
64 }/*}}}*/
65 
usleep(unsigned int useconds)66 PHPAPI int usleep(unsigned int useconds)
67 {/*{{{*/
68 	HANDLE timer;
69 	LARGE_INTEGER due;
70 
71 	due.QuadPart = -(10 * (__int64)useconds);
72 
73 	timer = CreateWaitableTimer(NULL, TRUE, NULL);
74 	SetWaitableTimer(timer, &due, 0, NULL, NULL, 0);
75 	WaitForSingleObject(timer, INFINITE);
76 	CloseHandle(timer);
77 	return 0;
78 }/*}}}*/
79 
nanosleep(const struct timespec * rqtp,struct timespec * rmtp)80 PHPAPI int nanosleep( const struct timespec * rqtp, struct timespec * rmtp )
81 {/*{{{*/
82 	if (rqtp->tv_nsec > 999999999) {
83 		/* The time interval specified 1,000,000 or more microseconds. */
84 		errno = EINVAL;
85 		return -1;
86 	}
87 	return usleep( rqtp->tv_sec * 1000000 + rqtp->tv_nsec / 1000  );
88 }/*}}}*/
89