Skip to content

Commit

Permalink
Merge pull request #1414 from tidepool-org/WEB-2853-use-dexcom-timezo…
Browse files Browse the repository at this point in the history
…ne-offset

[WEB-2853] Use nearest Etc/GMT timezone from latestUpload.timezoneOffset if timezone is not available
  • Loading branch information
clintonium-119 authored Aug 7, 2024
2 parents 99a5455 + 6e3bbfe commit 29270c4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
20 changes: 17 additions & 3 deletions app/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import _ from 'lodash';
import sundial from 'sundial';
import moment from 'moment';
import { format } from 'd3-format';

import { MGDL_UNITS, MMOLL_UNITS, MGDL_PER_MMOLL } from './constants';
Expand Down Expand Up @@ -330,9 +331,22 @@ utils.getTimePrefsForDataProcessing = (latestUpload, queryParams) => {
setNewTimePrefs(queryParams.timezone, false);
console.log('Displaying in timezone from query params:', queryParams.timezone);
}
else if (!_.isEmpty(latestUpload) && !_.isEmpty(latestUpload.timezone)) {
setNewTimePrefs(latestUpload.timezone);
console.log('Defaulting to display in timezone of most recent upload at', latestUpload.normalTime, latestUpload.timezone);
else if (!_.isEmpty(latestUpload) && (!_.isEmpty(latestUpload.timezone) || _.isFinite(latestUpload.timezoneOffset))) {
let timezone = latestUpload.timezone;

// If timezone is empty, set to the nearest Etc/GMT timezone using the timezoneOffset
if (_.isEmpty(timezone)) {
// GMT offsets signs in Etc/GMT timezone names are reversed from the actual offset
const offsetSign = Math.sign(latestUpload.timezoneOffset) === -1 ? '+' : '-';
const offsetDuration = moment.duration(Math.abs(latestUpload.timezoneOffset), 'minutes');
let offsetHours = offsetDuration.hours();
const offsetMinutes = offsetDuration.minutes();
if (offsetMinutes >= 30) offsetHours += 1;
timezone = `Etc/GMT${offsetSign}${offsetHours}`;
}

setNewTimePrefs(timezone);
console.log('Defaulting to display in timezone of most recent upload at', latestUpload.normalTime, timezone);
}
else if (browserTimezone) {
setNewTimePrefs(browserTimezone);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"node": "20.8.0"
},
"packageManager": "[email protected]",
"version": "1.81.0-web-3005-web-3306-web-3007-web-3008-loop-stats.1",
"version": "1.82.0-web-2853-use-dexcom-timezone-offset.1",
"private": true,
"scripts": {
"test": "TZ=UTC NODE_ENV=test NODE_OPTIONS='--max-old-space-size=4096' yarn karma start",
Expand Down
75 changes: 74 additions & 1 deletion test/unit/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ describe('utils', () => {
});

context('Timezone provided from most recent upload', () => {
it('should set a valid timezone from a query param', () => {
it('should set a valid timezone from `latestUpload.timezone`', () => {
expect(utils.getTimePrefsForDataProcessing(latestUpload, queryParams)).to.eql({
timezoneAware: true,
timezoneName: 'US/Pacific',
Expand Down Expand Up @@ -493,6 +493,79 @@ describe('utils', () => {
});
});

context('Timezone offset provided from most recent upload', () => {
it('should set a valid timezone from `latestUpload.timezoneOffset`', () => {
expect(utils.getTimePrefsForDataProcessing({
...latestUpload,
timezone: undefined,
timezoneOffset: -420,
}, queryParams)).to.eql({
timezoneAware: true,
timezoneName: 'Etc/GMT+7',
});

// should round to the nearest hour
expect(utils.getTimePrefsForDataProcessing({
...latestUpload,
timezone: undefined,
timezoneOffset: -(420 + 29),
}, queryParams)).to.eql({
timezoneAware: true,
timezoneName: 'Etc/GMT+7',
});

expect(utils.getTimePrefsForDataProcessing({
...latestUpload,
timezone: undefined,
timezoneOffset: -(420 + 30),
}, queryParams)).to.eql({
timezoneAware: true,
timezoneName: 'Etc/GMT+8',
});
});

it('should fall back to browser time when given an invalid timezone', () => {
const dataWithInvalidTimezone = {
type: 'upload',
time: '2018-02-10T00:00:00.000Z',
timezoneOffset: -1000, // Too large: will not match an Etc/GMT timezone
};

const DateTimeFormatStub = sinon.stub(Intl, 'DateTimeFormat').returns({
resolvedOptions: () => {
return { timeZone: 'Europe/Budapest' };
},
});

expect(utils.getTimePrefsForDataProcessing(dataWithInvalidTimezone, queryParams)).to.eql({
timezoneAware: true,
timezoneName: 'Europe/Budapest',
});

DateTimeFormatStub.restore();
});

it('should fall back to timezone-naive display time when given an invalid timezone and cannot determine timezone from browser', () => {
const dataWithInvalidTimezone = {
type: 'upload',
time: '2018-02-10T00:00:00.000Z',
timezoneOffset: -1000, // Too large: will not match an Etc/GMT timezone
};

const DateTimeFormatStub = sinon.stub(Intl, 'DateTimeFormat').returns({
resolvedOptions: () => {
return { timeZone: undefined };
},
});

expect(utils.getTimePrefsForDataProcessing(dataWithInvalidTimezone, queryParams)).to.eql({
timezoneAware: false,
});

DateTimeFormatStub.restore();
});
});

context('Timezone not provided from query params or most recent upload', () => {
it('should fall back to browser timezone if available', () => {
const DateTimeFormatStub = sinon.stub(Intl, 'DateTimeFormat').returns({
Expand Down

0 comments on commit 29270c4

Please sign in to comment.