when-am-i / files / src / utils / julianDay.test.ts
julianDay.test.ts
| 1 | import { describe, it, expect } from 'vitest'; |
| 2 | import { |
| 3 | gregorianToJulianDay, |
| 4 | julianDayToGregorian, |
| 5 | julianDayToJulian, |
| 6 | parseGregorianDate, |
| 7 | } from './julianDay'; |
| 8 | |
| 9 | describe('gregorianToJulianDay', () => { |
| 10 | it('matches the J2000 epoch (2000-01-01 = JDN 2451545)', () => { |
| 11 | expect(gregorianToJulianDay({ year: 2000, month: 1, day: 1 })).toBe(2451545); |
| 12 | }); |
| 13 | |
| 14 | it('orders dates monotonically across the BCE/CE boundary', () => { |
| 15 | const bce = gregorianToJulianDay({ year: -1, month: 12, day: 31 }); // 2 BCE |
| 16 | const ce = gregorianToJulianDay({ year: 1, month: 1, day: 1 }); // 1 CE |
| 17 | expect(bce).toBeLessThan(ce); |
| 18 | }); |
| 19 | }); |
| 20 | |
| 21 | describe('julianDayToGregorian', () => { |
| 22 | it('round-trips with the forward conversion', () => { |
| 23 | for (const date of [ |
| 24 | { year: 2000, month: 1, day: 1 }, |
| 25 | { year: 1582, month: 10, day: 15 }, |
| 26 | { year: 1, month: 1, day: 1 }, |
| 27 | { year: -44, month: 3, day: 15 }, |
| 28 | ]) { |
| 29 | expect(julianDayToGregorian(gregorianToJulianDay(date))).toEqual(date); |
| 30 | } |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | describe('julianDayToJulian', () => { |
| 35 | it('2000-01-01 Gregorian is 1999-12-19 in the Julian calendar (13-day gap)', () => { |
| 36 | const jdn = gregorianToJulianDay({ year: 2000, month: 1, day: 1 }); |
| 37 | expect(julianDayToJulian(jdn)).toEqual({ year: 1999, month: 12, day: 19 }); |
| 38 | }); |
| 39 | |
| 40 | it('1582-10-15 Gregorian is 1582-10-05 Julian (the 10-day reform gap)', () => { |
| 41 | const jdn = gregorianToJulianDay({ year: 1582, month: 10, day: 15 }); |
| 42 | expect(julianDayToJulian(jdn)).toEqual({ year: 1582, month: 10, day: 5 }); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | describe('parseGregorianDate', () => { |
| 47 | it('parses a normal date', () => { |
| 48 | expect(parseGregorianDate('1750-01-01')).toEqual({ year: 1750, month: 1, day: 1 }); |
| 49 | }); |
| 50 | |
| 51 | it('parses a negative (BCE) year', () => { |
| 52 | expect(parseGregorianDate('-0044-03-15')).toEqual({ year: -44, month: 3, day: 15 }); |
| 53 | }); |
| 54 | |
| 55 | it('throws on a malformed string', () => { |
| 56 | expect(() => parseGregorianDate('not-a-date')).toThrow(); |
| 57 | }); |
| 58 | }); |