properly adjust localtime according to localTimezone, if present#25
properly adjust localtime according to localTimezone, if present#25
Conversation
da28099 to
72ffccc
Compare
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
72ffccc to
45bc6d5
Compare
|
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 |
There was a problem hiding this comment.
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 withmoment(...).utcOffset(...).format()when computinglocalTime.
💡 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(); |
There was a problem hiding this comment.
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.
| const localTime = moment(data.time).utcOffset(offset).format(); | |
| const localMoment = moment(data.time).utcOffset(offset); | |
| const localTime = localMoment.toDate(); |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
@krystophv based on Slack discussion that I wasn't aware of, I've updated the Jira ticket and posted a new PR (#26).
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