Repository: twigphp/Twig (php)

Issue:
Feature Request: Twig\Profiler\Profile should have getStartTime() and getEndTime() methods
I'm trying to create Twig timeline into Clockwork, but right now there is no way to access the start and end timestamps in the profiler.

In the timeline it is useful to see when the template was called as it gives visual clues about the call order.


Something like:
```php
    public function getStartTime()
    {
        return isset($this->starts['wt']) ? $this->starts['wt']] : 0;
    }

    public function getEndTime()
    {
        return isset($this->ends['wt']) ? $this->ends['wt']] : 0;
    }
```

Pull request description:
Add start/end time accessors on Profile
Closes #3418

Patch:
diff --git a/CHANGELOG b/CHANGELOG
index 4d0616ce..2fc4b934 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,6 @@
 # 3.15.0 (2024-XX-XX)
 
+ * Add `Profile::getStartTime()` and `Profile::getEndTime()`
  * Fix "ignore missing" when used on an "embed" tag
  * Fix the possibility to override an aliased block (via use)
  * Add template cache hot reload
diff --git a/src/Profiler/Profile.php b/src/Profiler/Profile.php
index 2928e164..a3c6ee02 100644
--- a/src/Profiler/Profile.php
+++ b/src/Profiler/Profile.php
@@ -99,6 +99,22 @@ final class Profile implements \IteratorAggregate, \Serializable
         return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0;
     }
 
+    /**
+     * Returns the start time in microseconds.
+     */
+    public function getStartTime(): float
+    {
+        return $this->starts['wt'] ?? 0.0;
+    }
+
+    /**
+     * Returns the end time in microseconds.
+     */
+    public function getEndTime(): float
+    {
+        return $this->ends['wt'] ?? 0.0;
+    }
+
     /**
      * Returns the memory usage in bytes.
      */


Test patch:
diff --git a/tests/Profiler/ProfileTest.php b/tests/Profiler/ProfileTest.php
index 5f41fc9c..8c00e65b 100644
--- a/tests/Profiler/ProfileTest.php
+++ b/tests/Profiler/ProfileTest.php
@@ -80,6 +80,15 @@ class ProfileTest extends TestCase
         $this->assertTrue($profile->getDuration() > 0, \sprintf('Expected duration > 0, got: %f', $profile->getDuration()));
     }
 
+    public function testTimeAccessors()
+    {
+        $current = microtime(true);
+        $profile = new Profile();
+
+        $this->assertEqualsWithDelta($current, $profile->getStartTime(), 1);
+        $this->assertSame(0.0, $profile->getEndTime());
+    }
+
     public function testSerialize()
     {
         $profile = new Profile('template', 'type', 'name');