Skip to content

Commit

Permalink
Add time to detail page
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoern-hempel committed Jun 14, 2024
1 parent b2b342d commit a52a0a7
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/components/layout/Location/LocationInformation.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, {useEffect, useState} from "react";
import {useTranslation} from "react-i18next";

/* Import configuration. */
Expand All @@ -19,6 +19,20 @@ type LocationInformationProps = {
number: number
}

const getTimeWithOffset = (offset: string) =>
{
const now = new Date();
const [hoursOffset, minutesOffset] = offset.split(':').map(Number);
const totalOffsetMinutes = (hoursOffset * 60) + (Math.sign(hoursOffset) * minutesOffset);

// Calculate the current time in UTC
const utcTime = new Date(now.getTime() + (now.getTimezoneOffset() * 60000));
// Apply the offset to get the target time
const targetTime = new Date(utcTime.getTime() + (totalOffsetMinutes * 60000));

return targetTime.toTimeString().split(' ')[0];
}

/**
* This is the LocationInformation part.
*/
Expand All @@ -27,6 +41,21 @@ const LocationInformation = ({location, number}: LocationInformationProps) =>
/* Import translation. */
const { t } = useTranslation();

const [time, setTime] = useState<string>('');

useEffect(() => {
if (location.hasTimezone()) {
const offset = location.getTimezone()?.getOffset() ?? '00:00';
setTime(getTimeWithOffset(offset));

const intervalId = setInterval(() => {
setTime(getTimeWithOffset(offset));
}, 1000);

return () => clearInterval(intervalId);
}
}, [location]);

const classNamesRow1 = ClassNames.Row1;
const classNamesRow2 = ClassNames.Row2;

Expand Down Expand Up @@ -83,7 +112,9 @@ const LocationInformation = ({location, number}: LocationInformationProps) =>
colSpan={2}
title={location.getTimezone()?.getCoordinate().getDMS() ?? ''}
>{location.getTimezone()?.getTimezone()}&nbsp;
<code>{location.getTimezone()?.getOffset()}</code></td>
<code>{location.getTimezone()?.getOffset()}</code><br />
<span>{time}</span>
</td>
</tr> : <></>
}
{
Expand Down

0 comments on commit a52a0a7

Please sign in to comment.