Skip to content

A free open source IT asset/license management system

Fixed #19765 - `last_seen` not honoring APP_TIMEZONE

snipe93aa388ab2f6
diff --git a/app/SyncAdapters/SyncsHostFromRecord.php b/app/SyncAdapters/SyncsHostFromRecord.php
index 399dc067e0..acadf9fc3d 100644
--- a/app/SyncAdapters/SyncsHostFromRecord.php
+++ b/app/SyncAdapters/SyncsHostFromRecord.php
@@ -735,6 +735,27 @@ trait SyncsHostFromRecord
/**
* Extract the normalized value for a given field from the record.
+ *
+ * `last_seen` passes through a timezone rotation to app.timezone before
+ * the naive string is produced. Vendor adapters typically parse their
+ * API response with `Carbon::parse` on an ISO-8601 string carrying a
+ * `Z` suffix, which yields a Carbon in UTC. `toDateTimeString()` on
+ * that instance would write UTC wall clock into a column every other
+ * Snipe-IT write populates with app-timezone wall clock, and the view
+ * layer would then interpret that UTC wall clock as if it were in
+ * app.timezone (future-dated on anything west of UTC, past-dated on
+ * anything east).
+ *
+ * This is part of a much bigger yuck that we'll eventually have to solve
+ * with a fugly migration and then putting everything into UTC properly.
+ * For now, this is a stopgap to keep the sync from writing
+ * future-dated last_seen values, so while it's still "wrong", it's at
+ * least "wrong in the same way as everything else" and doesn't
+ * break the view layer.
+ *
+ * Laravel pulled APP_TIMEZONE out of the config in 10.x but still
+ * honors it in 11.x, so this is a workaround until we can get a proper
+ * fix in place. See #19765.
*/
private static function recordValueFor(HostInventoryRecord $record, string $field): mixed
{
@@ -747,7 +768,7 @@ trait SyncsHostFromRecord
'ip' => $record->primaryIp,
'os' => $record->os,
'os_version' => $record->osVersion,
- 'last_seen' => $record->lastSeen?->toDateTimeString(),
+ 'last_seen' => $record->lastSeen?->copy()->timezone(config('app.timezone'))->toDateTimeString(),
default => null,
};
}
diff --git a/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php b/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php
index 13340b9382..1d5013b565 100644
--- a/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php
+++ b/tests/Feature/SyncAdapters/Kandji/KandjiAdapterTest.php
@@ -109,6 +109,47 @@ class KandjiAdapterTest extends TestCase
iterator_to_array($adapter->pull());
}
+ public function test_last_seen_is_stored_in_app_timezone_not_vendor_utc(): void
+ {
+ // Issue #19765: Kandji returns `last_check_in` as ISO-8601 UTC with a
+ // Z suffix. Pre-fix, Carbon::parse honored the Z and the shared
+ // writer called ->toDateTimeString() on the UTC Carbon, so the UTC
+ // wall clock went into asset_external_sources.last_seen. On an
+ // instance with APP_TIMEZONE set to a non-UTC value, the view then
+ // interpreted that stored string as app-timezone wall clock,
+ // which read 7 hours in the future on PDT.
+ config(['app.timezone' => 'America/Los_Angeles']);
+
+ $adapter = $this->configuredKandjiAdapter();
+
+ Http::fake([
+ '*/api/v1/devices*' => Http::sequence()
+ ->push([
+ $this->kandjiDevice(
+ id: 'tz-1',
+ device_name: 'pacific-mbp',
+ serial_number: 'TZ-SN-1',
+ last_check_in: '2026-01-15T22:11:37.000Z',
+ ),
+ ])
+ ->push([]),
+ ]);
+
+ foreach ($adapter->pull() as $record) {
+ SyncAdapter::syncFromRecord($record);
+ }
+
+ // 22:11:37 UTC on 2026-01-15 is 14:11:37 the same day in Pacific
+ // standard time. The stored naive string must match the Pacific
+ // wall clock, same as every other naive datetime Snipe-IT writes
+ // under the same config.
+ $this->assertDatabaseHas('asset_external_sources', [
+ 'source' => 'kandji',
+ 'external_id' => 'tz-1',
+ 'last_seen' => '2026-01-15 14:11:37',
+ ]);
+ }
+
private function configuredKandjiAdapter(): KandjiAdapter
{
$instance = SyncAdapterInstance::where('slug', 'kandji')->firstOrFail();