Skip to content

Commit

Permalink
fix(selector.test): updated the way we're setting the timezone for se…
Browse files Browse the repository at this point in the history
…lector test
  • Loading branch information
asalem1 committed May 7, 2020
1 parent 794c97f commit 1f2b83f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
11 changes: 9 additions & 2 deletions ui/src/dashboards/selectors/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getTimeRange,
getTimeRangeWithTimezone,
} from 'src/dashboards/selectors/index'
import moment from 'moment'

// Types
import {RangeState} from 'src/dashboards/reducers/ranges'
Expand Down Expand Up @@ -34,9 +35,15 @@ describe('Dashboards.Selector', () => {
'04c6f3976f4b8000',
'04c6f3976f4b8002',
]
const lower = '2020-05-05T10:00:00-07:00'
const upper = '2020-05-05T11:00:00-07:00'
const customTimeRange = {
lower: '2020-05-05T10:00:00-07:00',
upper: '2020-05-05T11:00:00-07:00',
lower: moment(lower)
.utcOffset(lower)
.format(),
upper: moment(upper)
.utcOffset(upper)
.format(),
type: 'custom',
} as CustomTimeRange
const ranges: RangeState = {
Expand Down
16 changes: 7 additions & 9 deletions ui/src/dashboards/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@ export const getTimeRangeWithTimezone = (state: AppState): TimeRange => {
return newTimeRange
}

// The purpose of this function is to set a user's custom time range selection
// from the local time to the same time in UTC if UTC is selected from the
// timezone dropdown. This is feature was original requested here:
// https://github.com/influxdata/influxdb/issues/17877
// Example: user selected 10-11:00am and sets the dropdown to UTC
// Query should run against 10-11:00am UTC rather than querying
// 10-11:00am local time (offset depending on timezone)
export const setTimeToUTC = (date: string): string => {
// The purpose of this function is to set a user's custom time range selection
// from the local time to the same time in UTC if UTC is selected from the
// timezone dropdown. This is feature was original requested here:
// https://github.com/influxdata/influxdb/issues/17877
// Example: user selected 10-11:00am and sets the dropdown to UTC
// Query should run against 10-11:00am UTC rather than querying
// 10-11:00am local time (offset depending on timezone)
const offset = new Date(date).getTimezoneOffset()
if (offset > 0) {
// subtract tz minute difference
return moment
.utc(date)
.subtract(offset, 'minutes')
.format()
}
if (offset < 0) {
// add tz minute difference
return moment
.utc(date)
.add(offset, 'minutes')
Expand Down

0 comments on commit 1f2b83f

Please sign in to comment.