Skip to content

Commit

Permalink
fix: update timestamps [DHIS2-14516] (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp authored Mar 1, 2023
2 parents b4e5eb2 + 70a27e7 commit b8564e1
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 3,799 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"cypress:live": "start-server-and-test 'yarn start:nobrowser' http://localhost:3000 'yarn cypress open --env networkMode=live'"
},
"dependencies": {
"@dhis2/app-runtime": "^3.2.0",
"@dhis2/app-runtime": "^3.8.0",
"@dhis2/d2-i18n": "^1.1.0",
"@dhis2/prop-types": "2.0.3",
"@dhis2/ui": "^7.2.0",
Expand Down
29 changes: 21 additions & 8 deletions src/components/JobTable/NextRun.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import moment from 'moment'
import { useTimeZoneConversion } from '@dhis2/app-runtime'
import { PropTypes } from '@dhis2/prop-types'
import i18n from '@dhis2/d2-i18n'
import { Tooltip } from '@dhis2/ui'
import React from 'react'

const formatDate = (dhis2Date) =>
`${dhis2Date
.getServerZonedISOString()
.substring(0, 19)
.split('T')
.join(' ')} (${dhis2Date.serverTimezone})`

const NextRun = ({ nextExecutionTime, enabled }) => {
const { fromServerDate } = useTimeZoneConversion()

if (!enabled || !nextExecutionTime) {
return '-'
}

const now = moment(Date.now())
const now = Date.now()

/**
* The recommendation is to run dhis2 on a server set to UTC time.
* In that case this timestamp is also UTC. If those recommendations
* weren't followed the time could be off, but there's nothing
* we can do to detect that.
* Adjust for client/sever time zone difference.
*/
const nextRun = moment.utc(nextExecutionTime)
const nextRunIsInPast = nextRun.isSameOrBefore(now, 'minute')
const nextRun = fromServerDate(nextExecutionTime)
const nextRunIsInPast = nextRun.getTime() <= now

/**
* If the time is in the past, that could mean that the task is running,
Expand All @@ -27,7 +36,11 @@ const NextRun = ({ nextExecutionTime, enabled }) => {
return i18n.t('Not scheduled')
}

return now.to(nextRun)
return (
<Tooltip content={moment(nextRun).fromNow()}>
{formatDate(nextRun)}
</Tooltip>
)
}

const { bool, string } = PropTypes
Expand Down
34 changes: 30 additions & 4 deletions src/components/JobTable/NextRun.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
import React from 'react'
import { shallow } from 'enzyme'
import { mount, shallow } from 'enzyme'
import { Tooltip } from '@dhis2/ui'
import NextRun from './NextRun'

// Z is the zone designator for the zero UTC offset
const now = new Date('2010-10-10T10:10:10.000Z').valueOf()

// The server does not specify a timezone, but is assumed to provide UTC
const past = '2009-10-10T10:10:10.000'
const future = '2011-10-10T10:10:10.000'
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone

// in this test, we are only passing timestamps to the hook (and there is no server/client time diff)
// so mock getServerZonedISOString by returning the original timestamp
jest.mock('@dhis2/app-runtime', () => ({
useTimeZoneConversion: () => ({
fromServerDate: (timestamp) => {
const dhis2Date = new Date(timestamp)
dhis2Date.getServerZonedISOString = () => timestamp
dhis2Date.serverTimezone =
Intl.DateTimeFormat().resolvedOptions().timeZone
return dhis2Date
},
}),
}))

describe('<NextRun>', () => {
it('returns the next run time for an enabled job and a future execution time', () => {
const expected = 'in a year'
const expected = `2011-10-10 10:10:10 (${timeZone})`
jest.spyOn(global.Date, 'now').mockImplementation(() => now)

const wrapper = mount(
<NextRun nextExecutionTime={future} enabled={true} />
)

expect(wrapper.contains(expected)).toEqual(true)
})

it('returns the relative time in tooltip for an enabled job and a future execution time', () => {
const expected = `in a year`
jest.spyOn(global.Date, 'now').mockImplementation(() => now)

const wrapper = shallow(
<NextRun nextExecutionTime={future} enabled={true} />
)

expect(wrapper.text()).toEqual(expect.stringMatching(expected))
expect(wrapper.find(Tooltip).prop('content')).toEqual(expected)
})

it('returns fallback message for an enabled job and a past execution time', () => {
Expand Down
Loading

1 comment on commit b8564e1

@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.