Skip to content

Commit 4725ada

Browse files
authored
UHF-12774: Add 25 hours to valid_to timestamp (#1268)
* Handle timezones * Add 24 hours to valid_to timestamp Valid to from api is only a date. We must assume that the item is valid to the end of the date.
1 parent d59c369 commit 4725ada

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

public/modules/custom/helfi_kymp_content/src/MobileNoteDataService.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,28 @@ protected function transformFeature(array $feature): MobileNoteData {
213213
$properties = $feature['properties'] ?? [];
214214
$geometry = $feature['geometry'] ?? [];
215215

216-
$item = [
217-
'id' => $featureId,
218-
'address' => $properties['osoite'] ?? '',
219-
'reason' => $properties['merkinSyy']['value'] ?? '',
220-
'valid_from' => $this->dateToTimestamp($properties['voimassaoloAlku'] ?? NULL),
221-
'valid_to' => $this->dateToTimestamp($properties['voimassaoloLoppu'] ?? NULL),
222-
'time_range' => $properties['kello'] ?? '',
223-
'created_at' => $this->dateTimeToTimestamp($properties['luontipvm'] ?? NULL),
224-
'updated_at' => $this->dateTimeToTimestamp($properties['paivityspvm'] ?? NULL),
225-
'address_info' => $properties['osoitteenlisatieto'] ?? '',
226-
'sign_type' => $properties['merkinLaatu']['value'] ?? '',
227-
'additional_text' => $properties['lisakilvenTeksti'] ?? '',
228-
'notes' => $properties['huomautukset'] ?? '',
229-
'phone' => $properties['puhelinnumero'] ?? '',
230-
];
216+
try {
217+
$item = [
218+
'id' => $featureId,
219+
'address' => $properties['osoite'] ?? '',
220+
'reason' => $properties['merkinSyy']['value'] ?? '',
221+
'valid_from' => $this->dateToTimestamp($properties['voimassaoloAlku'] ?? NULL),
222+
// We only get date string from mobilenote. We
223+
// add 24 hours to get the end timestamp.
224+
'valid_to' => $this->dateToTimestamp($properties['voimassaoloLoppu'] ?? NULL) + 86400,
225+
'time_range' => $properties['kello'] ?? '',
226+
'created_at' => $this->dateTimeToTimestamp($properties['luontipvm'] ?? NULL),
227+
'updated_at' => $this->dateTimeToTimestamp($properties['paivityspvm'] ?? NULL),
228+
'address_info' => $properties['osoitteenlisatieto'] ?? '',
229+
'sign_type' => $properties['merkinLaatu']['value'] ?? '',
230+
'additional_text' => $properties['lisakilvenTeksti'] ?? '',
231+
'notes' => $properties['huomautukset'] ?? '',
232+
'phone' => $properties['puhelinnumero'] ?? '',
233+
];
234+
}
235+
catch (\DateException $e) {
236+
throw new \InvalidArgumentException("MobileNote: Invalid date", previous: $e);
237+
}
231238

232239
// Convert geometry to GeoJSON object.
233240
if (!empty($geometry['coordinates'])) {
@@ -252,17 +259,15 @@ protected function transformFeature(array $feature): MobileNoteData {
252259
*
253260
* @return int|null
254261
* The timestamp or NULL.
262+
*
263+
* @throws \DateException
255264
*/
256265
protected function dateToTimestamp(?string $dateString): ?int {
257266
if (empty($dateString)) {
258-
return NULL;
259-
}
260-
try {
261-
return (new \DateTime($dateString))->getTimestamp();
262-
}
263-
catch (\DateMalformedStringException) {
264-
return NULL;
267+
throw new \DateMalformedStringException('Empty date string');
265268
}
269+
270+
return (new \DateTime($dateString, new \DateTimeZone('Europe/Helsinki')))->getTimestamp();
266271
}
267272

268273
/**
@@ -279,9 +284,9 @@ protected function dateTimeToTimestamp(?string $dateTimeString): ?int {
279284
return NULL;
280285
}
281286
try {
282-
return (new \DateTime($dateTimeString))->getTimestamp();
287+
return (new \DateTime($dateTimeString, new \DateTimeZone('Europe/Helsinki')))->getTimestamp();
283288
}
284-
catch (\DateMalformedStringException) {
289+
catch (\DateException) {
285290
return NULL;
286291
}
287292
}

public/modules/custom/helfi_kymp_content/tests/src/Kernel/MobileNoteDataServiceTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,11 @@ public function testGetMobileNoteData(): void {
134134
$this->assertContains('Mannerheimvägen', $item['street_names']);
135135
$this->assertCount(2, $item['street_names']);
136136

137-
// Verify date conversion.
138-
$this->assertEquals(strtotime('2026-01-20'), $item['valid_from']);
137+
// Verify date conversion (Europe/Helsinki timezone).
138+
$tz = new \DateTimeZone('Europe/Helsinki');
139+
$this->assertEquals((new \DateTime('2026-01-20', $tz))->getTimestamp(), $item['valid_from']);
140+
// valid_to should be the date + 24 hours (86400 seconds).
141+
$this->assertEquals((new \DateTime('2026-01-21', $tz))->getTimestamp() + 86400, $item['valid_to']);
139142

140143
// Verify EPSG:3879 to WGS84 coordinate conversion.
141144
$coords = $item['geometry']->coordinates[0];
@@ -163,6 +166,8 @@ public function testGetMobileNoteDataNoEnrich(): void {
163166
'properties' => [
164167
'osoite' => 'Test Street 2',
165168
'merkinSyy' => ['value' => 'Test Reason'],
169+
'voimassaoloAlku' => '2026-01-20',
170+
'voimassaoloLoppu' => '2026-01-21',
166171
],
167172
],
168173
],

public/modules/custom/helfi_kymp_content/tests/src/Kernel/MobileNoteDataSourceTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ public function testDatasource(): void {
9999
$this->assertEquals('Test Reason', $item['reason']);
100100
$this->assertEquals('Extra', $item['additional_text']);
101101

102-
// Verify date conversion.
103-
$this->assertEquals(strtotime('2026-01-20'), $item['valid_from']);
102+
// Verify date conversion (Europe/Helsinki timezone).
103+
$tz = new \DateTimeZone('Europe/Helsinki');
104+
$this->assertEquals((new \DateTime('2026-01-20', $tz))->getTimestamp(), $item['valid_from']);
104105

105106
// Verify EPSG:3879 to WGS84 coordinate conversion.
106107
$coords = $item['geometry']->coordinates[0];

0 commit comments

Comments
 (0)