From ea154cc23d43d049b4b5aa9dca65cc2011f3e0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Obrtl=C3=ADk?= Date: Thu, 25 May 2023 13:38:34 +0200 Subject: [PATCH] Optimizing: establish desired time window faster --- src/Abuse/Adapters/TimeLimit.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index e280b84..83ac242 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -57,8 +57,9 @@ class TimeLimit implements Adapter public function __construct(string $key, int $limit, int $seconds, Database $db) { $this->key = $key; - $time = (int) \date('U', (int) (\floor(\time() / $seconds)) * $seconds); // todo: any good Idea without time()? - $this->time = DateTime::format((new \DateTime())->setTimestamp($time)); + $currentTimestamp = time(); // todo: any good Idea without time()? + $timestampWindow = $currentTimestamp - $currentTimestamp % $seconds; + $this->time = DateTime::format((new \DateTime())->setTimestamp($timestampWindow)); $this->limit = $limit; $this->db = $db; }