Bladeren bron

fix: format the timestamp drawn on the result image

formatSpeedtestDataForImage() assigned the timestamp to itself, so the value
reached the image exactly as the database returned it. Every other field on
that list is passed through format(); this one reads like a placeholder that
was never filled in, and static analysis flags it as a self-assignment.

How much precision the column carries is decided by the backend, and the three
schemas shipped here disagree: MySQL's `timestamp` keeps none, PostgreSQL's
`timestamp without time zone DEFAULT now()` keeps microseconds and MSSQL's
`datetime` keeps milliseconds. A PostgreSQL deployment therefore drew
"2026-08-10 02:04:13.957789" where a MySQL one drew "2026-08-10 02:04:13".
Josef Schlehofer 2 dagen geleden
bovenliggende
commit
c136cd4cfe
1 gewijzigde bestanden met toevoegingen van 25 en 1 verwijderingen
  1. 25 1
      results/index.php

+ 25 - 1
results/index.php

@@ -41,6 +41,30 @@ function format($d)
     return number_format($d, 0, '.', '');
 }
 
+/**
+ * Drops the fractional seconds a database may keep on the timestamp column.
+ *
+ * How much precision that column carries is decided by the backend, and the
+ * three schemas shipped here disagree: MySQL declares `timestamp`, which keeps
+ * none, PostgreSQL uses `timestamp without time zone DEFAULT now()`, which
+ * keeps microseconds, and MSSQL uses `datetime`, which keeps milliseconds. The
+ * value was drawn exactly as the database returned it, so the same result reads
+ * as "2026-08-10 02:04:13" on one deployment and "2026-08-10 02:04:13.957789"
+ * on another.
+ *
+ * The fraction is matched against the seconds field it belongs to rather than
+ * the end of the string, so a value carrying a zone offset after it, such as
+ * "2026-08-10 02:04:13.957789+00", is handled as well.
+ *
+ * @param string $timestamp
+ *
+ * @return string
+ */
+function formatTimestamp($timestamp)
+{
+    return preg_replace('/(:\d{2})\.\d+/', '$1', (string) $timestamp);
+}
+
 /**
  * @param array $speedtest
  *
@@ -53,7 +77,7 @@ function formatSpeedtestDataForImage($speedtest)
     $speedtest['ul'] = format($speedtest['ul']);
     $speedtest['ping'] = format($speedtest['ping']);
     $speedtest['jitter'] = format($speedtest['jitter']);
-    $speedtest['timestamp'] = $speedtest['timestamp'];
+    $speedtest['timestamp'] = formatTimestamp($speedtest['timestamp']);
 
     $ispinfo = json_decode($speedtest['ispinfo'], true)['processedString'];
     $dash = strpos($ispinfo, '-');