Skip to content

Commit

Permalink
Uddate converter & tests (WiP)
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Oct 16, 2023
1 parent 866c826 commit 29d7879
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 90 deletions.
181 changes: 102 additions & 79 deletions src/chronology/domain/DateConverter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,88 @@
import DateConverter from 'chronology/domain/DateConverter'

const dateJulianEraBegin = {
bcJulianYear: 311,
cjdn: 1607923,
gregorianYear: -309,
bcGregorianYear: 310,
gregorianMonth: 3,
gregorianDay: 29,
julianDay: 3,
julianMonth: 4,
julianYear: -310,
lunationNabonassar: 5395,
mesopotamianDay: 1,
mesopotamianMonth: 1,
mesopotamianMonthLength: 29,
regnalYear: 1,
ruler: 'Seleucus I Nicator',
seBabylonianYear: 1,
seMacedonianYear: 1,
weekDay: 4,
}

const dateNebuchadnezzarIIY43M12D28 = {
gregorianYear: -559,
bcGregorianYear: 560,
gregorianMonth: 3,
gregorianDay: 28,
julianYear: -560,
julianMonth: 4,
julianDay: 3,
bcJulianYear: 561,
cjdn: 1516611,
weekDay: 7,
mesopotamianDay: 28,
mesopotamianMonth: 12,
mesopotamianMonthLength: 30,
ruler: 'Nebuchadnezzar II',
regnalYear: 43,
seBabylonianYear: -250,
lunationNabonassar: 2302,
}

const dateNebuchadnezzarIIY23M10D14 = {
gregorianYear: -580,
bcGregorianYear: 581,
gregorianMonth: 12,
gregorianDay: 28,
julianYear: -580,
bcJulianYear: 581,
julianMonth: 1,
julianDay: 3,
cjdn: 1509215,
weekDay: 3,
mesopotamianDay: 14,
mesopotamianMonth: 10,
mesopotamianMonthLength: 29,
ruler: 'Nebuchadnezzar II',
regnalYear: 23,
lunationNabonassar: 2052,
seBabylonianYear: -270,
}

const dateSeleucidY100M12D26 = {
gregorianYear: -209,
bcGregorianYear: 210,
gregorianMonth: 3,
gregorianDay: 30,
julianYear: -210,
julianMonth: 4,
julianDay: 3,
lunationNabonassar: 6631,
ruler: 'Antiochus III [the Great]',
regnalYear: 11,
bcJulianYear: 211,
cjdn: 1644448,
seBabylonianYear: 100,
seMacedonianYear: 101,
seArsacidYear: 36,
mesopotamianMonthLength: 29,
weekDay: 3,
mesopotamianMonth: 12,
mesopotamianDay: 26,
}

describe('DateConverter', () => {
let mesopotamianDate: DateConverter

Expand All @@ -8,53 +91,31 @@ describe('DateConverter', () => {
})

test('Check initial state', () => {
const expected = {
bcJulianYear: 311,
cjdn: 1607923,
gregorianYear: -309,
bcGregorianYear: 310,
gregorianMonth: 3,
gregorianDay: 29,
julianDay: 3,
julianMonth: 4,
julianYear: -310,
lunationNabonassar: 5395,
mesopotamianDay: 1,
mesopotamianMonth: 1,
mesopotamianMonthLength: 29,
regnalYear: 1,
ruler: 'Seleucus I Nicator',
seBabylonianYear: 1,
seMacedonianYear: 1,
weekDay: 4,
}
const expected = dateJulianEraBegin
expect(mesopotamianDate.calendar).toEqual(expected)
expect(mesopotamianDate.toDateString()).toEqual('29 March 310 BCE PGC')
expect(mesopotamianDate.toDateString('Julian')).toEqual(
'3 April 311 BCE PJC'
)
})

// ToDo:
// - Implement & test
// test('Set to Gregorian date', () => {})
// - Add test to cylcle through all types of conversions (using `calendar`params from prev. iteration) and get the same initial result.

test('Set to Gregorian date', () => {
const expected = dateNebuchadnezzarIIY43M12D28
mesopotamianDate.setToGregorianDate(-560, 3, 28) // Change to -559
expect(mesopotamianDate.calendar).toEqual(expected)
expect(mesopotamianDate.toDateString()).toEqual('28 March 560 BCE PGC')
expect(mesopotamianDate.toDateString('Julian')).toEqual(
'3 April 561 BCE PJC'
)
})

test('Set to Julian date', () => {
const expected = {
gregorianYear: -559,
bcGregorianYear: 560,
gregorianMonth: 3,
gregorianDay: 28,
julianYear: -560,
julianMonth: 4,
julianDay: 3,
bcJulianYear: 561,
cjdn: 1516611,
weekDay: 7,
mesopotamianDay: 28,
mesopotamianMonth: 12,
mesopotamianMonthLength: 30,
ruler: 'Nebuchadnezzar II',
regnalYear: 43,
seBabylonianYear: -250,
lunationNabonassar: 2302,
}
const expected = dateNebuchadnezzarIIY43M12D28
mesopotamianDate.setToJulianDate(-560, 4, 3)
expect(mesopotamianDate.calendar).toEqual(expected)
expect(mesopotamianDate.toDateString()).toEqual('28 March 560 BCE PGC')
Expand All @@ -64,25 +125,7 @@ describe('DateConverter', () => {
})

test('Set to Mesopotamian date', () => {
const expected = {
gregorianYear: -580,
bcGregorianYear: 581,
gregorianMonth: 12,
gregorianDay: 28,
julianYear: -580,
bcJulianYear: 581,
julianMonth: 1,
julianDay: 3,
cjdn: 1509215,
weekDay: 3,
mesopotamianDay: 14,
mesopotamianMonth: 10,
mesopotamianMonthLength: 29,
ruler: 'Nebuchadnezzar II',
regnalYear: 23,
lunationNabonassar: 2052,
seBabylonianYear: -270,
}
const expected = dateNebuchadnezzarIIY23M10D14
mesopotamianDate.setToMesopotamianDate('Nebuchadnezzar II', 23, 10, 14)
expect(mesopotamianDate.calendar).toEqual(expected)
expect(mesopotamianDate.toDateString()).toEqual('28 December 581 BCE PGC')
Expand All @@ -93,27 +136,7 @@ describe('DateConverter', () => {

test('Set to Seleucid date', () => {
mesopotamianDate.setToSeBabylonianDate(100, 12, 26)
const expected = {
gregorianYear: -209,
bcGregorianYear: 210,
gregorianMonth: 3,
gregorianDay: 30,
julianYear: -210,
julianMonth: 4,
julianDay: 3,
lunationNabonassar: 6631,
ruler: 'Antiochus III [the Great]',
regnalYear: 11,
bcJulianYear: 211,
cjdn: 1644448,
seBabylonianYear: 100,
seMacedonianYear: 101,
seArsacidYear: 36,
mesopotamianMonthLength: 29,
weekDay: 3,
mesopotamianMonth: 12,
mesopotamianDay: 26,
}
const expected = dateSeleucidY100M12D26
expect(mesopotamianDate.calendar).toEqual(expected)
expect(mesopotamianDate.toDateString()).toEqual('30 March 210 BCE PGC')
expect(mesopotamianDate.toDateString('Julian')).toEqual(
Expand Down
6 changes: 3 additions & 3 deletions src/chronology/domain/DateConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ export default class DateConverter extends DateConverterBase {
gregorianMonth: number,
gregorianDay: number
): void {
const cjdn = this.computeCjdnFromGregorianDate(
const cjdn = this.computeCjdnFromGregorianDate({
gregorianYear,
gregorianMonth,
gregorianDay
)
gregorianDay,
})
this.setToCjdn(cjdn)
}

Expand Down
23 changes: 15 additions & 8 deletions src/chronology/domain/DateConverterBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,23 @@ export default class DateConverterBase {
}

//ToDo: Check, (update), use!
computeCjdnFromGregorianDate(
gregorianYear: number,
gregorianMonth: number,
computeCjdnFromGregorianDate({
gregorianYear,
gregorianMonth,
gregorianDay,
}: {
gregorianYear: number
gregorianMonth: number
gregorianDay: number
): number {
const { quotient: a1, remainder: m1 } = divmod(gregorianMonth - 3, 12)
const { quotient: c1, remainder: a2 } = divmod(gregorianYear + a1, 100)
}): number {
const alpha1 = Math.floor((gregorianMonth - 3) / 12)
const m1 = gregorianMonth + alpha1
const a1 = gregorianYear + alpha1
return (
Math.floor((146097 * c1) / 4) +
Math.floor((36525 * a2) / 100) +
365 * a1 +
Math.floor(a1 / 4) -
Math.floor(a1 / 100) +
Math.floor(a1 / 400) +
Math.floor((153 * m1 + 2) / 5) +
gregorianDay +
1721119
Expand Down

0 comments on commit 29d7879

Please sign in to comment.