carrismetropolitana / website

Carris Metropolitana Website
https://beta.carrismetropolitana.pt
3 stars 2 forks source link

Wrong time #13

Closed ricardojorgerm closed 6 months ago

ricardojorgerm commented 6 months ago

https://github.com/carrismetropolitana/website/blob/4686b1056bfef56e2ad3f3a00f31acf3da8cbfa0/nextjs/components/FrontendLinesContentPatternPathStopRealtime/FrontendLinesContentPatternPathStopRealtime.js#L89

The issue with the code is that you are formatting the time using the toFormat method, but you are not actually converting the dateTimeObject to the 'Europe/Lisbon' time zone.

To fix this, you need to chain the setZone method before formatting the time. Here's the corrected code:

const dateTimeObject = DateTime.fromSeconds(item.scheduled_arrival_unix, { zone: 'UTC' });
return `${dateTimeObject.setZone('Europe/Lisbon').toFormat('HH:mm')}`;

Explanation:

  1. DateTime.fromSeconds(item.scheduled_arrival_unix, { zone: 'UTC' }) creates a DateTime object from the Unix timestamp, assuming it represents a UTC time.
  2. setZone('Europe/Lisbon') converts the DateTime object to the 'Europe/Lisbon' time zone.
  3. toFormat('HH:mm') formats the time in the 'HH:mm' format (hours and minutes).

By chaining setZone before toFormat, you ensure that the time is converted to the desired time zone before formatting it.

Also, note that you don't need to specify the time zone in the toFormat method since you have already set the time zone using setZone. The toFormat('HH:mm') will format the time according to the time zone set by setZone.

With these changes, the code should correctly return the time in the 'Europe/Lisbon' time zone.

joao-vasconcelos commented 6 months ago

Muito obrigado @ricardojorgerm <3