Browse Source

Remove usage of invalid timespec

Also add a comment to clarify why the subtraction of timespec values
should work.
Joel Schumacher 5 years ago
parent
commit
1c5b8431ca
1 changed files with 5 additions and 5 deletions
  1. 5 5
      src/modules/timer/Timer.cpp

+ 5 - 5
src/modules/timer/Timer.cpp

@@ -24,6 +24,7 @@
 #include "common/delay.h"
 #include "Timer.h"
 
+#include <iostream>
 #if defined(LOVE_WINDOWS)
 #include <windows.h>
 #elif defined(LOVE_MACOSX) || defined(LOVE_IOS)
@@ -134,11 +135,10 @@ double Timer::getTime()
 {
 	static const timespec start = getTimeAbsolute();
 	const timespec now = getTimeAbsolute();
-	const timespec rel = timespec {
-		now.tv_sec - start.tv_sec,
-		now.tv_nsec - start.tv_nsec
-	};
-	return (double) rel.tv_sec + (double) rel.tv_nsec / 1.0e9;
+	// tv_sec and tv_nsec should be signed on POSIX, so we are fine in just subtracting here.
+	const long sec = now.tv_sec - start.tv_sec;
+	const long nsec = now.tv_nsec - start.tv_nsec;
+	return (double) sec + (double) nsec / 1.0e9;
 }
 
 #elif defined(LOVE_MACOSX) || defined(LOVE_IOS)