All files ethiopianCalendarDateConverter.ts

77.29% Statements 177/229
57.69% Branches 15/26
60% Functions 12/20
77.29% Lines 177/229

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 2301x 1x 1x 1x 1x 1x 1x 1x 1x 1x                         1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x       3x 3x 1x 1x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x             3x 3x 3x 3x   3x 3x 3x 3x   3x 3x           3x 1x       1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                           1x 8x 8x 8x 8x 8x 8x 8x 8x     8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 8x 1x 1x 1x 1x 8x     1x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import {
  fourYears,
  oneYear,
  oneDay,
  globalTimeDifference,
  maxEurDate,
  minEurDate,
} from './dateConstants'
import { DayOfWeek, MonthEth } from './types'
 
function dayOfWeekString(day: DayOfWeek): string {
  const dayOfWeekStrings = {
    0: 'Sunday',
    1: 'Monday',
    2: 'Tuesday',
    3: 'Wednesday',
    4: 'Thursday',
    5: 'Friday',
    6: 'Saterday',
  }
  return dayOfWeekStrings[day]
}
 
function monthStringEth(month: MonthEth): string {
  const ethMonthStrings = {
    1: 'Meskerem ',
    2: 'Tikimt ',
    3: 'Hidar ',
    4: 'Tahsas ',
    5: 'Tir ',
    6: 'Yekatit ',
    7: 'Megabit ',
    8: 'Meyazya ',
    9: 'Ginbot ',
    10: 'Sene ',
    11: 'Hamle ',
    12: 'Nehase ',
    13: 'Pagume ',
  }
  return ethMonthStrings[month]
}
 
export class EthDateTime {
  year: number
  month: MonthEth
  date: number
  hour: number
  minute: number
  second: number
 
  constructor(
    yr: number,
    mon: number, // mon in human form
    date: number,
    hr = 0,
    min = 0,
    sec = 0,
  ) {
    if (date > 30) throw new Error(`Invalid Ethiopian Date: ${date}`)
    if (mon < 1 || mon > 13) throw new Error(`Invalid Ethiopian Month: ${mon}`)
 
    this.year = yr > 200 ? yr : yr + 1900
    this.month = mon as MonthEth
    this.date = date
    this.hour = hr
    this.minute = min
    this.second = sec
  }
 
  static now(): EthDateTime {
    const eurNow = new Date()
    return this.fromEuropeanDate(eurNow)
  }
 
  static fromEuropeanDate(europeanDate: Date): EthDateTime {
    return toEthiopianDateTime(europeanDate)
  }
 
  toEuropeanDate(): Date {
    return toEuropeanDate(this)
  }
 
  toTimeString = () =>
    this.hour < 13
      ? leftpadZero(this.hour) +
        ':' +
        leftpadZero(this.minute) +
        ':' +
        leftpadZero(this.second) +
        ' a.m.'
      : leftpadZero(this.hour - 12) +
        ':' +
        leftpadZero(this.minute) +
        ':' +
        leftpadZero(this.second) +
        ' p.m.'
 
  toDateString = () => monthStringEth(this.month) + this.date + ', ' + this.year
 
  toDateWithDayString = () =>
    `${dayOfWeekString(this.getDay())}, ${this.toDateString()}`
 
  toString = () => `${this.toDateString()}, ${this.toTimeString()}`
 
  toFullDateTimeString = () =>
    `${this.toString()}, ${dayOfWeekString(this.getDay())} .`
 
  getDay = () =>
    ((this.year +
      2 * this.month +
      this.date +
      yearDifferenceForEthDay(this.year)) %
      7) as DayOfWeek
}
 
function yearDifferenceForEthDay(ethYear: number) {
  return -Math.floor((2023 - ethYear) / 4)
}
 
function eurDateIsConvertible(eurDate: Date): boolean {
  return eurDate >= minEurDate && eurDate <= maxEurDate
}
 
function toEthiopianDateTime(eurDate: Date): EthDateTime {
  if (!eurDateIsConvertible(eurDate))
    throw `Out of range input year: ${eurDate.getFullYear()}`
  const difference =
    eurDate.getTime() - new Date(Date.UTC(1971, 8, 12)).getTime()
  const fourYearsPassed = Math.floor(difference / fourYears)
  let remainingYears = Math.floor(
    (difference - fourYearsPassed * fourYears) / oneYear,
  )
  if (remainingYears === 4) {
    remainingYears = 3
  }
  const remainingMonths = Math.floor(
    (difference - fourYearsPassed * fourYears - remainingYears * oneYear) /
      (30 * oneDay),
  )
  const remainingDays = Math.floor(
    (difference -
      fourYearsPassed * fourYears -
      remainingYears * oneYear -
      remainingMonths * 30 * oneDay) /
      oneDay,
  )
  let remainingHours = eurDate.getHours() // - 6 to account for traditional local time
  if (remainingHours < 0) {
    remainingHours += 24
  }
  const ethDate = new EthDateTime(
    remainingYears + 4 * fourYearsPassed + 1964,
    remainingMonths + 1,
    remainingDays + 1,
    remainingHours,
    eurDate.getMinutes(),
    eurDate.getSeconds(),
  )
  return ethDate
}
 
function toEuropeanDate(ethDate: EthDateTime): Date {
  const initialEuropean = new Date(
    new Date(
      Date.UTC(ethDate.year, ethDate.month - 1, ethDate.date),
    ).getTime() + globalTimeDifference,
  )
  if (ethDate.month === 13) {
    let maxDate
    if (ethDate.year % 4 === 3) maxDate = 6
    else maxDate = 5
    if (ethDate.date > maxDate) {
      const errMsg =
        'Pagume Only has ' +
        maxDate +
        ' days at year ' +
        ethDate.year +
        '. Please select another day.'
      throw errMsg
    }
  }
  for (let count = -8; count < 9; count++) {
    const eurDate = new Date(initialEuropean.getTime() + count * oneDay)
    const difference =
      eurDate.getTime() - new Date(Date.UTC(1971, 8, 12)).getTime()
    const fourYearsPassed = Math.floor(difference / fourYears)
    let remainingYears = Math.floor(
      (difference - fourYearsPassed * fourYears) / oneYear,
    )
    if (remainingYears === 4) {
      remainingYears = 3
    }
    const remainingMonths = Math.floor(
      (difference - fourYearsPassed * fourYears - remainingYears * oneYear) /
        (30 * oneDay),
    )
    const remainingDays = Math.floor(
      (difference -
        fourYearsPassed * fourYears -
        remainingYears * oneYear -
        remainingMonths * 30 * oneDay) /
        oneDay,
    )
    if (
      ethDate.date === remainingDays + 1 &&
      ethDate.month === remainingMonths + 1
    ) {
      if (!eurDateIsConvertible(eurDate))
        throw `Out of range input year: ${eurDate.getFullYear()}`
      return eurDate
    }
  }
  throw `Date not converted: ${ethDate.year},  ${ethDate.month},  ${ethDate.date}, `
}
 
function leftpadZero(Num: number, length = 2): string {
  return String(Num).padStart(length, '0')
}
 
export const limits = {
  ethiopianCalendarYear: {
    min: () => toEthiopianDateTime(minEurDate).year,
    max: () => toEthiopianDateTime(maxEurDate).year,
  },
  europeanCalendarDate: {
    min: minEurDate,
    max: maxEurDate,
  },
}