php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch win32_microtime_var1.patch for Date/time related Bug #64370

Patch version 2013-03-08 07:56 UTC

Return to Bug #64370 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: ab@php.net

diff --git a/win32/globals.c b/win32/globals.c
index 1bbb3b4..b381cc1 100644
--- a/win32/globals.c
+++ b/win32/globals.c
@@ -65,8 +65,6 @@ PHP_RSHUTDOWN_FUNCTION(win32_core_globals)
 		;
 
 	closelog();
-	wg->starttime.tv_sec = 0;
-	wg->lasttime = 0;
 
 	return SUCCESS;
 }
diff --git a/win32/php_win32_globals.h b/win32/php_win32_globals.h
index 1686e5d..b2b07f6 100644
--- a/win32/php_win32_globals.h
+++ b/win32/php_win32_globals.h
@@ -38,10 +38,6 @@ struct _php_win32_core_globals {
 	char *log_header;
 	HANDLE log_source;
 
-	/* time */
-	struct timeval starttime;
-	__int64			lasttime, freq;
-
 	HKEY       registry_key;
 	HANDLE     registry_event;
 	HashTable *registry_directories;
diff --git a/win32/time.c b/win32/time.c
index 391a8a8..b779ccb 100644
--- a/win32/time.c
+++ b/win32/time.c
@@ -32,26 +32,24 @@
 #include <errno.h>
 #include "php_win32_globals.h"
 
-int getfilesystemtime(struct timeval *time_Info) 
+
+int getfilesystemtime(struct timeval *tv) 
 {
-    FILETIME ft;
-    __int64 ff;
-    ULARGE_INTEGER convFromft;
-
-    GetSystemTimeAsFileTime(&ft);   /* 100 ns blocks since 01-Jan-1641 */
-                                    /* resolution seems to be 0.01 sec */
-    /*
-     * Do not cast a pointer to a FILETIME structure to either a 
-     * ULARGE_INTEGER* or __int64* value because it can cause alignment faults on 64-bit Windows.
-     * via  http://technet.microsoft.com/en-us/library/ms724284(v=vs.85).aspx
-     */
-    convFromft.HighPart = ft.dwHighDateTime;
-    convFromft.LowPart = ft.dwLowDateTime;
-    ff = convFromft.QuadPart;
-
-    time_Info->tv_sec = (int)(ff/(__int64)10000000-(__int64)11644473600);
-    time_Info->tv_usec = (int)(ff % 10000000)/10;
-    return 0;
+	FILETIME ft;
+	unsigned __int64 ff = 0;
+
+	GetSystemTimeAsFileTime(&ft);   /* 100 ns blocks since 01-Jan-1641 */
+
+	ff |= ft.dwHighDateTime;
+	ff <<= 32;
+	ff |= ft.dwLowDateTime;
+	ff /= 10; /* convert to microseconds */
+	ff -= 11644473600000000Ui64; /* convert to unix epoch */
+
+	tv->tv_sec = (long)(ff / 1000000UL);
+	tv->tv_usec = (long)(ff % 1000000UL);
+
+	return 0;
 }
 
  
@@ -62,67 +60,62 @@ PHPAPI int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Inf
 	LARGE_INTEGER li;
 	BOOL b;
 	double dt;
-	TSRMLS_FETCH();
+	struct timeval starttime;
+	__int64 lasttime = 0, freq;
 
 	/* Get the time, if they want it */
 	if (time_Info != NULL) {
-		if (PW32G(starttime).tv_sec == 0) {
-            b = QueryPerformanceFrequency(&li);
-            if (!b) {
-                PW32G(starttime).tv_sec = -1;
-            }
-            else {
-                PW32G(freq) = li.QuadPart;
-                b = QueryPerformanceCounter(&li);
-                if (!b) {
-                    PW32G(starttime).tv_sec = -1;
-                }
-                else {
-                    getfilesystemtime(&PW32G(starttime));
-                    timer = li.QuadPart;
-                    dt = (double)timer/PW32G(freq);
-                    PW32G(starttime).tv_usec -= (int)((dt-(int)dt)*1000000);
-                    if (PW32G(starttime).tv_usec < 0) {
-                        PW32G(starttime).tv_usec += 1000000;
-                        --PW32G(starttime).tv_sec;
-                    }
-                    PW32G(starttime).tv_sec -= (int)dt;
-                }
-            }
-        }
-        if (PW32G(starttime).tv_sec > 0) {
-            b = QueryPerformanceCounter(&li);
-            if (!b) {
-                PW32G(starttime).tv_sec = -1;
-            }
-            else {
-                timer = li.QuadPart;
-                if (timer < PW32G(lasttime)) {
-                    getfilesystemtime(time_Info);
-                    dt = (double)timer/PW32G(freq);
-                    PW32G(starttime) = *time_Info;
-                    PW32G(starttime).tv_usec -= (int)((dt-(int)dt)*1000000);
-                    if (PW32G(starttime).tv_usec < 0) {
-                        PW32G(starttime).tv_usec += 1000000;
-                        --PW32G(starttime).tv_sec;
-                    }
-                    PW32G(starttime).tv_sec -= (int)dt;
-                }
-                else {
-                    PW32G(lasttime) = timer;
-                    dt = (double)timer/PW32G(freq);
-                    time_Info->tv_sec = PW32G(starttime).tv_sec + (int)dt;
-                    time_Info->tv_usec = PW32G(starttime).tv_usec + (int)((dt-(int)dt)*1000000);
-                    if (time_Info->tv_usec >= 1000000) {
-                        time_Info->tv_usec -= 1000000;
-                        ++time_Info->tv_sec;
-                    }
-                }
-            }
-        }
-        if (PW32G(starttime).tv_sec < 0) {
-            getfilesystemtime(time_Info);
-        }
+		b = QueryPerformanceFrequency(&li);
+		if (!b) {
+			starttime.tv_sec = -1;
+		} else {
+			freq = li.QuadPart;
+			b = QueryPerformanceCounter(&li);
+			if (!b) {
+				starttime.tv_sec = -1;
+			} else {
+				getfilesystemtime(&starttime);
+				timer = li.QuadPart;
+				dt = (double)timer/freq;
+				starttime.tv_usec -= (int)((dt-(int)dt)*1000000);
+				if (starttime.tv_usec < 0) {
+				starttime.tv_usec += 1000000;
+				--starttime.tv_sec;
+				}
+				starttime.tv_sec -= (int)dt;
+			}
+		}
+		if (starttime.tv_sec > 0) {
+			b = QueryPerformanceCounter(&li);
+			if (!b) {
+				starttime.tv_sec = -1;
+			} else {
+				timer = li.QuadPart;
+				if (timer < lasttime) {
+					getfilesystemtime(time_Info);
+					dt = (double)timer/freq;
+					starttime = *time_Info;
+					starttime.tv_usec -= (int)((dt-(int)dt)*1000000);
+					if (starttime.tv_usec < 0) {
+						starttime.tv_usec += 1000000;
+						--starttime.tv_sec;
+					}
+					starttime.tv_sec -= (int)dt;
+				} else {
+					lasttime = timer;
+					dt = (double)timer/freq;
+					time_Info->tv_sec = starttime.tv_sec + (int)dt;
+					time_Info->tv_usec = starttime.tv_usec + (int)((dt-(int)dt)*1000000);
+					if (time_Info->tv_usec >= 1000000) {
+						time_Info->tv_usec -= 1000000;
+						++time_Info->tv_sec;
+					}
+				}
+			}
+		}
+		if (starttime.tv_sec < 0) {
+			getfilesystemtime(time_Info);
+		}
 
 	}
 	/* Get the timezone, if they want it */
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 22:01:26 2024 UTC