Skip to content

properly adjust localtime according to localTimezone, if present#25

Open
ewollesen wants to merge 1 commit intomasterfrom
eric-localtime
Open

properly adjust localtime according to localTimezone, if present#25
ewollesen wants to merge 1 commit intomasterfrom
eric-localtime

Conversation

@ewollesen
Copy link

Previously the code would add a number of minutes equal to the localTimezone. This is not correct, as JS stores dates in UTC. Adding the offset therefore did not adjust the timezone that's displayed, but rather changed the actual time value.

Using the moment library, we're able to parse a UTC time then display it in the desired timezone.

BACK-4205

@ewollesen ewollesen requested a review from darinkrauss February 9, 2026 11:09
@ewollesen ewollesen force-pushed the eric-localtime branch 2 times, most recently from da28099 to 72ffccc Compare February 9, 2026 11:22
Previously the code would add a number of minutes equal to the
localTimezone. This is not correct, as JS stores dates in UTC. Adding
the offset therefore did not adjust the timezone that's displayed, but
rather changed the actual time value.

Using the moment library, we're able to parse a UTC time then display
it in the desired timezone.

BACK-4205
@ewollesen ewollesen marked this pull request as ready for review February 9, 2026 11:43
@gniezen
Copy link
Member

gniezen commented Feb 9, 2026

I know some folks think sundial should be deprecated, but I find it quite useful as it wraps a bunch of stuff to work with time (from the moment library) to prevent these kind of Date() footguns. For example, I think you just reimplemented formatFromStorage: https://github.com/tidepool-org/sundial/blob/master/lib/datetimeWrapper.js#L247-L252

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adjusts how localTime is synthesized from Tidepool time + timezoneOffset to avoid mutating the underlying timestamp and instead represent the desired timezone via Moment’s offset handling (BACK-4205).

Changes:

  • Replace Date + manual minute adjustment with moment(...).utcOffset(...).format() when computing localTime.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

const localTime = new Date(data.time);
localTime.setUTCMinutes(localTime.getUTCMinutes() + (data.timezoneOffset || 0));
const offset = data.timezoneOffset || 0;
const localTime = moment(data.time).utcOffset(offset).format();
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

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

addLocalTime now sets localTime to a string (moment(...).format()), but the XLSX writer config treats localTime as a date field (cellFormat in config.json) and relies on ExcelJS receiving a Date to apply numFmt. As-is, localTime will be written as text in XLSX (formatting/sorting as dates won’t work). Consider either (a) keeping localTime as a Date for XLSX output (e.g., convert it to a Date in xlsxStreamWriter similarly to time/deviceTime/computerTime), or (b) if the intent is to preserve the timezone offset in the exported value, update the XLSX path/config to explicitly treat localTime as text and use a consistent string format.

Suggested change
const localTime = moment(data.time).utcOffset(offset).format();
const localMoment = moment(data.time).utcOffset(offset);
const localTime = localMoment.toDate();

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

@ewollesen This comment is largely correct. The call stack in here gets pretty convoluted:

Example Flow for localTime
Config defines: "localTime": { "cellFormat": "yyyy-mm-dd hh:mm:ss" } (config.json)
Cache stores: cellFormat['smbg']['localTime'] = "yyyy-mm-dd hh:mm:ss" (TidepoolDataTools.cache)
Processing: addLocalTime() creates a JavaScript Date object
Column setup: cellFormat('smbg', 'localTime', { bgUnits: 'mmol/L' }) returns the Excel format string
ExcelJS applies the numFmt to format the Date when writing the cell

So currently the rest of the processing is expecting that value to be a Date() and not a string and may need to be adjusted accordingly (in the config) if we want to move over to a preformatted string from the Date.

Copy link
Author

Choose a reason for hiding this comment

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

@krystophv based on Slack discussion that I wasn't aware of, I've updated the Jira ticket and posted a new PR (#26).

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