Skip to content

Commit

Permalink
fix: adjust time zone for dateLimit [DHIS2-16470] (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp authored Jan 16, 2024
1 parent 94fbaa6 commit 789a818
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/context-selection/period-selector-bar-item/use-date-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import {
useClientServerDate,
} from '../../shared/index.js'

export const getDateInTimeZone = (dateString) => {
const [yyyy, mm, dd] = dateString.split('-')
if (isNaN(Number(yyyy)) || isNaN(Number(dd)) || isNaN(Number(mm))) {
return new Date(dateString)
}
return new Date(yyyy, Number(mm) - 1, dd)
}

export const computePeriodDateLimit = ({
periodType,
serverDate,
Expand All @@ -29,7 +37,7 @@ export const computePeriodDateLimit = ({
})

if (openFuturePeriods <= 0) {
return new Date(currentPeriod.startDate)
return getDateInTimeZone(currentPeriod.startDate)
}

const followingPeriods = getAdjacentFixedPeriods({
Expand All @@ -40,7 +48,7 @@ export const computePeriodDateLimit = ({

const [lastFollowingPeriod] = followingPeriods.slice(-1)

return new Date(lastFollowingPeriod.startDate)
return getDateInTimeZone(lastFollowingPeriod.startDate)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
useMetadata,
periodTypesMapping,
} from '../../shared/index.js'
import { useDateLimit, computePeriodDateLimit } from './use-date-limit.js'
import {
useDateLimit,
computePeriodDateLimit,
getDateInTimeZone,
} from './use-date-limit.js'

export const reversedPeriodTypesMapping = Object.fromEntries(
Object.entries(periodTypesMapping).map(([key, value]) => [value, key])
Expand Down Expand Up @@ -42,6 +46,23 @@ jest.mock('../../shared/date/use-server-time-offset.js', () => ({
default: jest.fn(() => 0),
}))

describe('getDateInTimeZone', () => {
it('should return a date corrected that corresponds to the browser time zone', () => {
// note that we are setting the time zone for tests to UTC, so new Date() and getDateInTimeZone() are equivalent
const dateString = '2024-01-01'
const actual = getDateInTimeZone(dateString)
const expected = new Date(dateString)
expect(actual).toEqual(expected)
})

it('should use Date constructor if date is not in format yyyy-mm-dd', () => {
const dateString = '2024-01-01T12:00:00'
const actual = getDateInTimeZone(dateString)
const expected = new Date(dateString)
expect(actual).toEqual(expected)
})
})

describe('computePeriodDateLimit', () => {
const actualSystemTime = new Date()
jest.useFakeTimers()
Expand All @@ -51,29 +72,29 @@ describe('computePeriodDateLimit', () => {
})

it('it should return the "2022-04-01" date', () => {
const currentDate = new Date('2023-03-01')
const currentDate = getDateInTimeZone('2023-03-01')
jest.setSystemTime(currentDate)

const actual = computePeriodDateLimit({
periodType: periodTypes.FYAPR,
serverDate: currentDate,
openFuturePeriods: 0,
})
const expected = new Date('2022-04-01')
const expected = getDateInTimeZone('2022-04-01')

expect(actual).toEqual(expected)
})

it('it should return the "2023-04-01" period', () => {
const currentDate = new Date('2023-05-01')
const currentDate = getDateInTimeZone('2023-05-01')
jest.setSystemTime(currentDate)

const actual = computePeriodDateLimit({
periodType: periodTypes.FYAPR,
serverDate: currentDate,
openFuturePeriods: 0,
})
const expected = new Date('2023-04-01')
const expected = getDateInTimeZone('2023-04-01')

expect(actual).toEqual(expected)
})
Expand Down

1 comment on commit 789a818

@dhis2-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

Please sign in to comment.