Skip to content

Commit

Permalink
draft / expose function to convert calendar dates to iso8601
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed May 23, 2024
1 parent 4e50063 commit 5e37778
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/utils/convert-date.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertFromIso8601 } from './convert-date'
import { convertFromIso8601, convertToIso8601 } from './convert-date'

describe('date conversion from gregorian', () => {
describe('to ethiopic', () => {
Expand Down Expand Up @@ -37,9 +37,21 @@ describe('date conversion from gregorian', () => {

// gregorian, ethiopic and nepali

describe('date conversion from', () => {
describe('date conversion to gregorian', () => {
describe('ethiopic to gregorian', () => {
it('should convert a date', () => {})
it('should convert a date', () => {
const result = convertToIso8601('2016-09-15', 'ethiopic')
expect(result.year).toEqual(2024)
expect(result.month).toEqual(5)
expect(result.day).toEqual(23)
})
// it('should handle year / eraYear differences', () => {
// const result = convertFromIso8601('2016-09-15', 'ethiopic')
// expect(result.eraYear).toEqual(2016)
// expect(result.year).toEqual(7516)
// expect(result.month).toEqual(9)
// expect(result.day).toEqual(15)
// })
it('should convert a date if "ethiopian" is passed instad of "ethiopic"', () => {})

Check failure on line 55 in src/utils/convert-date.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected empty arrow function
})
describe('nepali to gregorian', () => {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/convert-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ export const convertFromIso8601: ConvertDateFn = (date, userCalendar) => {

return Temporal.PlainDate.from(date).withCalendar(calendar)
}

export const convertToIso8601: ConvertDateFn = (date, userCalendar) => {
const calendar = getCustomCalendarIfExists(
dhis2CalendarsMap[userCalendar] ?? userCalendar
) as SupportedCalendar

const result = Temporal.Calendar.from(calendar).dateFromFields({
year: 2016,
month: 9,
day: 23,
})

return result
}

0 comments on commit 5e37778

Please sign in to comment.