Skip to content

Commit

Permalink
Implement Gregorian string output, correct, update & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Oct 12, 2023
1 parent 9c95b43 commit 866c826
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 185 deletions.
16 changes: 8 additions & 8 deletions src/chronology/domain/Date.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Eponym } from 'chronology/ui/Eponyms'
import { MesopotamianDate, Ur3Calendar } from './Date' // Adjust the import path
import { MesopotamianDate, Ur3Calendar } from './Date'

const king = {
orderGlobal: 1,
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('MesopotamianDate', () => {
undefined,
true
)
expect(date.toString()).toBe('12.V.10 SE (30 August 302 BCE)')
expect(date.toString()).toBe('12.V.10 SE (25 August 301 BCE PGC)')
})

it('returns the correct string representation (Seleucid, no day)', () => {
Expand All @@ -119,7 +119,7 @@ describe('MesopotamianDate', () => {
undefined,
true
)
expect(date.toString()).toBe('∅.V.10 SE (ca. 19 August 302 BCE)')
expect(date.toString()).toBe('∅.V.10 SE (ca. 14 August 301 BCE PGC)')
})

it('returns the correct string representation (Seleucid, no month)', () => {
Expand All @@ -131,7 +131,7 @@ describe('MesopotamianDate', () => {
undefined,
true
)
expect(date.toString()).toBe('12.∅.10 SE (ca. 4 May 302 BCE)')
expect(date.toString()).toBe('12.∅.10 SE (ca. 29 April 301 BCE PGC)')
})

it('returns the correct string representation (Seleucid, no year)', () => {
Expand All @@ -156,7 +156,7 @@ describe('MesopotamianDate', () => {
undefined,
false
)
expect(date.toString()).toBe('12.V.10 Darius I (11 August 512 BCE)')
expect(date.toString()).toBe('12.V.10 Darius I (5 August 511 BCE PGC)')
})

it('returns the correct string representation (Nabonassar era, no year)', () => {
Expand All @@ -169,7 +169,7 @@ describe('MesopotamianDate', () => {
undefined,
false
)
expect(date.toString()).toBe('∅.V.10 Darius I (ca. 31 July 512 BCE)')
expect(date.toString()).toBe('∅.V.10 Darius I (ca. 25 July 511 BCE PGC)')
})

it('returns the correct string representation (Nabonassar era, no month)', () => {
Expand All @@ -182,7 +182,7 @@ describe('MesopotamianDate', () => {
undefined,
false
)
expect(date.toString()).toBe('12.∅.10 Darius I (ca. 16 April 512 BCE)')
expect(date.toString()).toBe('12.∅.10 Darius I (ca. 10 April 511 BCE PGC)')
})

it('returns the correct string representation (Nabonassar era, no day)', () => {
Expand All @@ -195,7 +195,7 @@ describe('MesopotamianDate', () => {
undefined,
false
)
expect(date.toString()).toBe('∅.V.10 Darius I (ca. 31 July 512 BCE)')
expect(date.toString()).toBe('∅.V.10 Darius I (ca. 25 July 511 BCE PGC)')
})

it('returns the correct string representation (Ur III)', () => {
Expand Down
11 changes: 4 additions & 7 deletions src/chronology/domain/Date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,8 @@ export class MesopotamianDate {
isApproximate: boolean
): string {
const converter = new DateConverter()
converter.setSeBabylonianDate(year, month, day)
return this.insertDateApproximation(
converter.toJulianDateString(),
isApproximate
)
converter.setToSeBabylonianDate(year, month, day)
return this.insertDateApproximation(converter.toDateString(), isApproximate)
}

private nabonassarEraToJulianDate(
Expand All @@ -253,9 +250,9 @@ export class MesopotamianDate {
)
if (kingName) {
const converter = new DateConverter()
converter.setMesopotamianDate(kingName, year, month, day)
converter.setToMesopotamianDate(kingName, year, month, day)
return this.insertDateApproximation(
converter.toJulianDateString(),
converter.toDateString(),
isApproximate
)
}
Expand Down
28 changes: 20 additions & 8 deletions src/chronology/domain/DateConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ describe('DateConverter', () => {
weekDay: 4,
}
expect(mesopotamianDate.calendar).toEqual(expected)
expect(mesopotamianDate.toJulianDateString()).toEqual('3 April 311 BCE')
expect(mesopotamianDate.toDateString()).toEqual('29 March 310 BCE PGC')
expect(mesopotamianDate.toDateString('Julian')).toEqual(
'3 April 311 BCE PJC'
)
})

test('Set to Julian date', () => {
Expand All @@ -54,10 +57,13 @@ describe('DateConverter', () => {
}
mesopotamianDate.setToJulianDate(-560, 4, 3)
expect(mesopotamianDate.calendar).toEqual(expected)
expect(mesopotamianDate.toJulianDateString()).toEqual('3 April 561 BCE')
expect(mesopotamianDate.toDateString()).toEqual('28 March 560 BCE PGC')
expect(mesopotamianDate.toDateString('Julian')).toEqual(
'3 April 561 BCE PJC'
)
})

test('Set Mesopotamian date', () => {
test('Set to Mesopotamian date', () => {
const expected = {
gregorianYear: -580,
bcGregorianYear: 581,
Expand All @@ -77,13 +83,16 @@ describe('DateConverter', () => {
lunationNabonassar: 2052,
seBabylonianYear: -270,
}
mesopotamianDate.setMesopotamianDate('Nebuchadnezzar II', 23, 10, 14)
mesopotamianDate.setToMesopotamianDate('Nebuchadnezzar II', 23, 10, 14)
expect(mesopotamianDate.calendar).toEqual(expected)
expect(mesopotamianDate.toJulianDateString()).toEqual('3 January 581 BCE')
expect(mesopotamianDate.toDateString()).toEqual('28 December 581 BCE PGC')
expect(mesopotamianDate.toDateString('Julian')).toEqual(
'3 January 581 BCE PJC'
)
})

test('Set Seleucid date', () => {
mesopotamianDate.setSeBabylonianDate(100, 12, 26)
test('Set to Seleucid date', () => {
mesopotamianDate.setToSeBabylonianDate(100, 12, 26)
const expected = {
gregorianYear: -209,
bcGregorianYear: 210,
Expand All @@ -106,6 +115,9 @@ describe('DateConverter', () => {
mesopotamianDay: 26,
}
expect(mesopotamianDate.calendar).toEqual(expected)
expect(mesopotamianDate.toJulianDateString()).toEqual('3 April 211 BCE')
expect(mesopotamianDate.toDateString()).toEqual('30 March 210 BCE PGC')
expect(mesopotamianDate.toDateString('Julian')).toEqual(
'3 April 211 BCE PJC'
)
})
})
50 changes: 37 additions & 13 deletions src/chronology/domain/DateConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,52 @@ export default class DateConverter extends DateConverterBase {
this.setToJulianDate(-310, 4, 3)
}

toJulianDateString(): string {
const { julianDay, julianMonth, julianYear, bcJulianYear } = this.calendar
const suffix = julianYear < 0 ? ' BCE' : ' CE'
return `${julianDay} ${monthNames[julianMonth - 1]} ${
julianYear < 0 ? bcJulianYear : julianYear
}${suffix}`
toDateString(calendarType: 'Julian' | 'Gregorian' = 'Gregorian'): string {
let {
gregorianDay: day,
gregorianMonth: month,
gregorianYear: year,
bcGregorianYear: bcYear,
} = this.calendar
if (calendarType === 'Julian') {
day = this.calendar.julianDay
month = this.calendar.julianMonth
year = this.calendar.julianYear
bcYear = this.calendar.bcJulianYear
}
const suffix = year < 0 ? ' BCE' : ' CE'
return `${day} ${monthNames[month - 1]} ${
year < 0 ? bcYear : year
}${suffix}${calendarType === 'Julian' ? ' PJC' : ' PGC'}`
}

setToGregorianDate(
gregorianYear: number,
gregorianMonth: number,
gregorianDay: number
): void {
this.applyGregorianDate({ gregorianYear, gregorianMonth, gregorianDay })
this.updateBabylonianDate()
const cjdn = this.computeCjdnFromGregorianDate(
gregorianYear,
gregorianMonth,
gregorianDay
)
this.setToCjdn(cjdn)
}

setToJulianDate(
julianYear: number,
julianMonth: number,
julianDay: number
): void {
this.applyJulianDate({ julianYear, julianMonth, julianDay })
this.applyDate(
{ year: julianYear, month: julianMonth, day: julianDay },
'julian'
)
this.applyGregorianDateWhenJulian()
this.updateBabylonianDate()
}

setSeBabylonianDate(
setToSeBabylonianDate(
seBabylonianYear: number,
mesopotamianMonth: number,
mesopotamianDay: number
Expand All @@ -69,10 +87,10 @@ export default class DateConverter extends DateConverterBase {
mesopotamianMonth,
mesopotamianDay
)
this.setFromCjdn(cjdn)
this.setToCjdn(cjdn)
}

setMesopotamianDate(
setToMesopotamianDate(
ruler: string,
regnalYear: number,
mesopotamianMonth: number,
Expand All @@ -87,6 +105,12 @@ export default class DateConverter extends DateConverterBase {
mesopotamianMonth,
mesopotamianDay
)
this.setFromCjdn(cjdn)
this.setToCjdn(cjdn)
}

setToCjdn(cjdn: number): void {
this.applyDate(this.computeJulianDateFromCjnd(cjdn), 'julian')
this.applyDate(this.computeGregorianDateFromCjdn(cjdn), 'gregorian')
this.updateBabylonianDate(cjdn)
}
}
Loading

0 comments on commit 866c826

Please sign in to comment.