Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mesopotamian date #381

Merged
merged 18 commits into from
Aug 8, 2023
2 changes: 1 addition & 1 deletion src/common/BrinkmanKings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import BrinkmanKings from 'common/BrinkmanKings.json'
import { Popover } from 'react-bootstrap'
import HelpTrigger from 'common/HelpTrigger'

interface King {
export interface King {
orderGlobal: number
groupWith?: number
dynastyNumber: string
Expand Down
178 changes: 178 additions & 0 deletions src/fragmentarium/domain/Date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//import produce, { castDraft, Draft, immerable } from 'immer'
//import { immerable } from 'immer'
//import _ from 'lodash'
import { King } from 'common/BrinkmanKings'
import { MesopotamianDateDto } from 'fragmentarium/domain/FragmentDtos'
import { romanize } from 'romans'

export interface DateField {
value: string
broken?: boolean
uncertain?: boolean
}

export interface MonthField extends DateField {
intercalary?: boolean
}

export enum Ur3Calendar {
ADAB = 'Adab',
GIRSU = 'Girsu',
IRISAGRIG = 'Irisagrig',
NIPPUR = 'Nippur',
PUZRISHDAGAN = 'Puzriš-Dagan',
UMMA = 'Umma',
UR = 'Ur',
}

export class Date {
era: King | string | undefined
year: DateField
month: MonthField
day: DateField
ur3Calendar?: Ur3Calendar

constructor(
era: King | string | undefined,
year: DateField,
month: MonthField,
day: DateField,
ur3Calendar?: Ur3Calendar
) {
this.era = era
this.year = year
this.month = month
this.day = day
this.ur3Calendar = ur3Calendar
}

static fromJson(dateJSON: MesopotamianDateDto): Date {
const { era, year, month, day } = dateJSON
console.log(dateJSON, new Date(era, year, month, day))
return new Date(era, year, month, day)
}

toString(): string {
const dateParts = [
this.yearToString(),
this.monthToString(),
this.dayToString(),
]
return `${dateParts.join(
'.'
)}${this.eraToString()}${this.ur3CalendarToString()}`
}

yearToString(): string {
return this.year.value
? this.brokenAndUncertainToString('year', this.year.value)
: '∅'
}

monthToString(): string {
const intercalary = this.month.intercalary ? '²' : ''
const month = Number(this.month.value)
? romanize(Number(this.month.value))
: this.month.value
return month
? this.brokenAndUncertainToString('month', month + intercalary)
: '∅'
}

dayToString(): string {
return this.day.value
? this.brokenAndUncertainToString('day', this.day.value)
: '∅'
}

brokenAndUncertainToString(
parameter: 'year' | 'day' | 'month',
element: string
): string {
const { broken, uncertain } = this[parameter]

return `${broken ? '⸢' : ''}${element}${broken ? '⸣' : ''}${
uncertain ? '?' : ''
}`
}

eraToString(): string {
const era =
this.era === 'seleucid'
? 'SE'
: typeof this.era === 'string'
? this.era
: this.era?.name ?? ''
return era ? ' ' + era : era
}

ur3CalendarToString(): string {
return this.ur3Calendar ? `, ${this.ur3Calendar} calendar` : ''
}

toGregorian(): string {
// ToDo: WiP, implement
return ''
}
}

/*
export class Dates {
readonly dates: ReadonlyArray<Date>

constructor(dates: Date[]) {
this.dates = dates
}

static fromJson(datesJSON: readonly MesopotamianDateDto[]): Dates {
return new Dates(datesJSON.map((dateJson) => Date.fromJson(dateJson)))
}


isPresent(date: Date): boolean {
return this.dates.some(
(element) =>
JSON.stringify(element.category) === JSON.stringify(date.category)
)
}

find(date: Date): Date | undefined {
return _.find(
this.dates,
(elem) => JSON.stringify(elem.category) === JSON.stringify(date.category)
)
}

insertWithOrder(date: Date, indexLookup: readonly string[][]): Dates {
return produce(this, (draft: Draft<Dates>) => {
draft.dates = castDraft(
_.sortBy([...this.dates, date], (date) =>
JSON.stringify(indexLookup).indexOf(JSON.stringify(date.category))
)
)
})
}

delete(date: Date): Dates {
return produce(this, (draft: Draft<Dates>) => {
draft.dates = castDraft(
this.dates.filter(
(elem) => JSON.stringify(elem) !== JSON.stringify(date)
)
)
})
}

replace(date: Date): Dates {
return produce(this, (draft: Draft<Dates>) => {
const dates = _.cloneDeep(this.dates as Date[])
const index = _.findIndex(
this.dates,
(content) =>
JSON.stringify(content.category) === JSON.stringify(date.category)
)
dates.splice(index, 1, date)
draft.dates = castDraft(dates)
})
}
*/
74 changes: 74 additions & 0 deletions src/fragmentarium/domain/DateConverter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import DateConverter from './DateConverter'

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

beforeEach(() => {
babylonDate = new DateConverter()
})

test('Set modern date', () => {
babylonDate.setModernDate(-310, 3, 3)
const expected = {
year: -310,
month: 3,
day: 3,
bcYear: '311',
julianDay: 1607892,
weekDay: 1,
babylonianDay: 29,
babylonianMonth: 11,
babylonianRuler: '4 Alexander IV Aegus',
seBabylonianYear: '0',
seMacedonianYear: '1',
arsacidYear: ' ',
babylonianLunation: 5393,
babylonianMonthLength: 29,
}
expect(babylonDate.calendar).toEqual(expected)
})

test('Set Babylonian date', () => {
// WiP.
// ToDo: Update to properly test.
babylonDate.setBabylonianDate(-200, 10, 1)
const expected = {
year: -625,
month: 11,
day: 27,
bcYear: '626',
julianDay: 1493107,
weekDay: 2,
babylonianDay: 1,
babylonianMonth: 9,
babylonianRuler: '1 Interregnum',
seBabylonianYear: '-314',
seMacedonianYear: ' ',
arsacidYear: ' ',
babylonianLunation: 1507,
babylonianMonthLength: 30,
}
expect(babylonDate.calendar).toEqual(expected)
})

test('Update year', () => {
babylonDate.updateYear(100)
expect(babylonDate.calendar.year).toBe(-210)
})

test('Update month', () => {
babylonDate.updateMonth(5)
expect(babylonDate.calendar.month).toBe(8)
expect(babylonDate.calendar.year).toBe(-310)
})

test('Update day', () => {
babylonDate.updateDay(10)
expect(babylonDate.calendar.day).toBe(13)
})

test('Get current day', () => {
babylonDate.calendar.day = 25
expect(babylonDate.getCurrentDay()).toBe(25)
})
})
Loading
Loading