time.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /* Include stuff ************************************************************ */
  13. #include <config.w32.h>
  14. #include "time.h"
  15. #include "unistd.h"
  16. #include "signal.h"
  17. #include <windows.h>
  18. #include <winbase.h>
  19. #include <mmsystem.h>
  20. #include <errno.h>
  21. #include "php_win32_globals.h"
  22. typedef VOID (WINAPI *MyGetSystemTimeAsFileTime)(LPFILETIME lpSystemTimeAsFileTime);
  23. static MyGetSystemTimeAsFileTime timefunc = NULL;
  24. #ifdef PHP_EXPORTS
  25. static zend_always_inline MyGetSystemTimeAsFileTime get_time_func(void)
  26. {/*{{{*/
  27. MyGetSystemTimeAsFileTime timefunc = NULL;
  28. HMODULE hMod = GetModuleHandle("kernel32.dll");
  29. if (hMod) {
  30. /* Max possible resolution <1us, win8/server2012 */
  31. timefunc = (MyGetSystemTimeAsFileTime)GetProcAddress(hMod, "GetSystemTimePreciseAsFileTime");
  32. }
  33. if(!timefunc) {
  34. /* 100ns blocks since 01-Jan-1641 */
  35. timefunc = (MyGetSystemTimeAsFileTime) GetSystemTimeAsFileTime;
  36. }
  37. return timefunc;
  38. }/*}}}*/
  39. void php_win32_init_gettimeofday(void)
  40. {/*{{{*/
  41. timefunc = get_time_func();
  42. }/*}}}*/
  43. #endif
  44. static zend_always_inline int getfilesystemtime(struct timeval *tv)
  45. {/*{{{*/
  46. FILETIME ft;
  47. unsigned __int64 ff = 0;
  48. ULARGE_INTEGER fft;
  49. timefunc(&ft);
  50. /*
  51. * Do not cast a pointer to a FILETIME structure to either a
  52. * ULARGE_INTEGER* or __int64* value because it can cause alignment faults on 64-bit Windows.
  53. * via http://technet.microsoft.com/en-us/library/ms724284(v=vs.85).aspx
  54. */
  55. fft.HighPart = ft.dwHighDateTime;
  56. fft.LowPart = ft.dwLowDateTime;
  57. ff = fft.QuadPart;
  58. ff /= 10Ui64; /* convert to microseconds */
  59. ff -= 11644473600000000Ui64; /* convert to unix epoch */
  60. tv->tv_sec = (long)(ff / 1000000Ui64);
  61. tv->tv_usec = (long)(ff % 1000000Ui64);
  62. return 0;
  63. }/*}}}*/
  64. PHPAPI int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
  65. {/*{{{*/
  66. /* Get the time, if they want it */
  67. if (time_Info != NULL) {
  68. getfilesystemtime(time_Info);
  69. }
  70. /* Get the timezone, if they want it */
  71. if (timezone_Info != NULL) {
  72. _tzset();
  73. timezone_Info->tz_minuteswest = _timezone;
  74. timezone_Info->tz_dsttime = _daylight;
  75. }
  76. /* And return */
  77. return 0;
  78. }/*}}}*/
  79. PHPAPI int usleep(unsigned int useconds)
  80. {/*{{{*/
  81. HANDLE timer;
  82. LARGE_INTEGER due;
  83. due.QuadPart = -(10 * (__int64)useconds);
  84. timer = CreateWaitableTimer(NULL, TRUE, NULL);
  85. SetWaitableTimer(timer, &due, 0, NULL, NULL, 0);
  86. WaitForSingleObject(timer, INFINITE);
  87. CloseHandle(timer);
  88. return 0;
  89. }/*}}}*/
  90. PHPAPI int nanosleep( const struct timespec * rqtp, struct timespec * rmtp )
  91. {/*{{{*/
  92. if (rqtp->tv_nsec > 999999999) {
  93. /* The time interval specified 1,000,000 or more microseconds. */
  94. errno = EINVAL;
  95. return -1;
  96. }
  97. return usleep( rqtp->tv_sec * 1000000 + rqtp->tv_nsec / 1000 );
  98. }/*}}}*/
  99. /*
  100. * Local variables:
  101. * tab-width: 4
  102. * c-basic-offset: 4
  103. * End:
  104. * vim600: sw=4 ts=4 fdm=marker
  105. * vim<600: sw=4 ts=4
  106. */