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): format observation datetime to accurate local time to prevent inaccurate timestamps #2208

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

amosmachora
Copy link

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work conforms to the OpenMRS 3.0 Styleguide and design documentation.
  • My work includes tests or is validated by existing tests.

Summary

This PR formats the test timestamp in the results panel using the openmrs framework formatDatetime function that accurately handles the users locale. Since the date we had was already formatted to a certain format we first convert it to an ISO string (which was the original format) then format it correctly to the users local time removing the inconsistency.

Screenshots

Results

image

Visits

image

Related Issue

https://openmrs.atlassian.net/browse/O3-4297

Other

test?.obs?.map((entry) => {
const isoFormattedString = entry.obsDatetime.replace(' ', 'T').replace('.0', '.000').concat('Z');
const date = new Date(isoFormattedString);
return format(date, 'yyyy-MM-dd HH:mm:ss');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the framework formatDatetime here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not want to break the existing functionality. In my investigation using formatDatetime returns dates like Today 06:50 am which was not my intention. I wanted to return a string like this 2024-09-04 06:23:23.0 so that wherever the value is being used it doesn't break anything. In short I wanted to return the original date but formatted correctly,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote for consistency across the system and I vote for the formatters provided by the framework. Is there a strong reason not to go with the formatted date from the framework's function?

Copy link
Author

@amosmachora amosmachora Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @ojwanganto that date object at that point is not for display but rather to make sure it is in the correct timezone and that approach is what made it possible. using formatDateTime or any other format function from the framework did not quite fit the use case.

I am open to suggestions though.

Comment on lines +120 to +123
const formatObsDatetime = (obsDatetime: string) => {
const isoFormattedString = obsDatetime.replace(' ', 'T').replace('.0', '.000').concat('Z');
return isoFormattedString;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const formatObsDatetime = (obsDatetime: string) => {
const isoFormattedString = obsDatetime.replace(' ', 'T').replace('.0', '.000').concat('Z');
return isoFormattedString;
};
const formatObsDatetime = (obsDatetime: string) => {
return dayjs(obsDatetime).toISOString();
};

dayjs already provides this conversion to ISO format.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The obsDatetime parameter is weird. It's an already formatted date string like this: 2025-01-22 05:28:00.0 Following your suggestion rolls back the date by 3 hours (probably because I am on EAT).

obsDatetime - 2025-01-22 05:28:00.0
isoFormattedString - 2025-01-22T05:28:00.000Z 
formattedWithDayJS - 2025-01-22T02:28:00.000Z

thanks for highlighting though I have seen a use case for it on the tooltip for accuracy purposes

@denniskigen denniskigen requested a review from ibacher January 29, 2025 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants