Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(approval-status-tag): fix time-ago messages #87

Merged
merged 2 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/shared/approval-status/approval-status-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { useServerDateTimeAsLocal } from './use-server-date-time-as-local.js'

const ApprovalStatusTag = ({ approvalStatus, approvedAt, approvedBy }) => {
const approvalDateTime = useServerDateTimeAsLocal(approvedAt)
const approvalDateTime = approvedAt && useServerDateTimeAsLocal(approvedAt)
const {
icon: Icon,
displayName,
Expand Down
7 changes: 5 additions & 2 deletions src/shared/approval-status/use-server-date-time-as-local.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { useConfig } from '@dhis2/app-runtime'

const msPerHr = 1000 * 60 * 60

export const useServerDateTimeAsLocal = dateTime => {
const { systemInfo } = useConfig()
const timestamp = new Date(dateTime).getTime()
const localNow = new Date()
const nowAtServerTimeZone = new Date(
localNow.toLocaleString('en-US', {
timeZone: systemInfo.serverTimeZoneId,
})
)
const timestamp = new Date(dateTime).getTime()
const timeOffset = localNow.getTime() - nowAtServerTimeZone.getTime()
const timeOffsetRoundedToHours = Math.round(timeOffset / msPerHr) * msPerHr

return new Date(timestamp + timeOffset)
return new Date(timestamp + timeOffsetRoundedToHours)
}