From 6622cc8431e4324d4414b74aeb521a9bfad2587b Mon Sep 17 00:00:00 2001
From: Manuel Pitz <manuel.pitz@eonerc.rwth-aachen.de>
Date: Wed, 6 Jul 2022 19:14:56 +0200
Subject: [PATCH] fix: correct time alignment for phasor rate calculation

---
 lib/hooks/pmu.cpp | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/lib/hooks/pmu.cpp b/lib/hooks/pmu.cpp
index 76d86af58..f5915aadb 100644
--- a/lib/hooks/pmu.cpp
+++ b/lib/hooks/pmu.cpp
@@ -197,16 +197,12 @@ Hook::Reason PmuHook::process(struct Sample *smp)
 				phasorStatus = Status::INVALID;
 		}
 
-		double m = pow(10, floor(log10(phasorRate) + 1));
-		if (phasorRate <= 1) // For less then 1 phasor per second
-			m = pow(10, floor(log10(phasorRate)));
-    		double nextRunDouble = (floor(time_to_double(&smp->ts.origin)*m)/m) + 1.0 / phasorRate;
-		double r = fmod(nextRunDouble, 1 / phasorRate);
-		if( (r > 1 / (4 * phasorRate)) && (r < 3 / (4 * phasorRate)))
-			nextRunDouble += (1.0 / phasorRate) - fmod(nextRunDouble, 1 / phasorRate);
-		nextRun = time_from_double(nextRunDouble);
+		// Align time tag
+		double currentTimeTag = time_to_double(&smp->ts.origin);
+		double alignedTime = currentTimeTag - fmod(currentTimeTag, 1 / phasorRate);
+		nextRun = time_from_double(alignedTime + 1 / phasorRate);
 
-		unsigned tsPos = 0;
+		size_t tsPos = 0;
 		if (timeAlignType == TimeAlign::RIGHT)
 			tsPos = windowSize;
 		else if (timeAlignType == TimeAlign::LEFT)