Skip to content

Commit

Permalink
Fix mix between UTC and local time
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddasol committed Apr 17, 2024
1 parent 2136cdf commit c8d264a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const MissionEndTimeDisplay = ({ mission }: MissionProps) => {
return (
<>
{mission.endTime ? (
<Typography>{formatDateTime(new Date(mission.endTime), 'HH:mm:ss - dd/MM/yy')}</Typography>
<Typography>{formatDateTime(mission.endTime, 'HH:mm:ss - dd/MM/yy')}</Typography>
) : (
<Typography>-</Typography>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,23 @@ const getStartUsedAndRemainingTime = (

if (mission.endTime) {
startTime = mission.startTime
? formatDateTime(new Date(mission.startTime), 'HH:mm')
: formatDateTime(new Date(mission.endTime), 'HH:mm')
? formatDateTime(mission.startTime, 'HH:mm')
: formatDateTime(mission.endTime, 'HH:mm')

startDate = mission.startTime
? formatDateTime(new Date(mission.startTime), 'dd/MM/yyy')
: formatDateTime(new Date(mission.endTime), 'dd/MM/yyy')
? formatDateTime(mission.startTime, 'dd/MM/yyy')
: formatDateTime(mission.endTime, 'dd/MM/yyy')
usedTimeInMinutes = mission.startTime
? differenceInMinutes(new Date(mission.endTime), new Date(mission.startTime))
? differenceInMinutes(
convertUTCDateToLocalDate(mission.endTime),
convertUTCDateToLocalDate(mission.startTime)
)
: 0
remainingTime = 'N/A'
} else if (mission.startTime) {
startTime = formatDateTime(new Date(mission.startTime), 'HH:mm')
startDate = formatDateTime(new Date(mission.startTime), 'dd/MM/yyy')
usedTimeInMinutes = differenceInMinutes(Date.now(), convertUTCDateToLocalDate(new Date(mission.startTime)))
startTime = formatDateTime(mission.startTime, 'HH:mm')
startDate = formatDateTime(mission.startTime, 'dd/MM/yyy')
usedTimeInMinutes = differenceInMinutes(Date.now(), convertUTCDateToLocalDate(mission.startTime))
if (estimatedDurationInMinutes)
remainingTime = Math.max(estimatedDurationInMinutes - usedTimeInMinutes, 0) + ' ' + translatedMinutes
else remainingTime = 'N/A'
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/utils/StringFormatting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { format } from 'date-fns'

const millisecondsInADay = 8.64e7

export const convertUTCDateToLocalDate = (date: Date): Date =>
new Date(date.getTime() - date.getTimezoneOffset() * 60 * 1000)
export const convertUTCDateToLocalDate = (date: Date): Date => {
// If lastChar is Z, typescript assumes the date to be UTC
const lastChar = date.toString().slice(-1)
if (lastChar === 'Z') return new Date(date)
// If not, typescript assumes the date to be local time
return new Date(new Date(date).getTime() - new Date(date).getTimezoneOffset() * 60 * 1000)
}

const formatBackendDateTimeToDate = (date: Date) => new Date(date.toString())

Expand All @@ -30,4 +35,4 @@ export const getDeadlineInDays = (deadlineDate: Date): number =>
new Date(deadlineDate.getTime() - new Date().getTime()).getTime() / millisecondsInADay

export const formatDateTime = (dateTime: Date, dateFormat: string): string =>
format(convertUTCDateToLocalDate(new Date(dateTime)), dateFormat)
format(convertUTCDateToLocalDate(dateTime), dateFormat)

0 comments on commit c8d264a

Please sign in to comment.