Skip to content

Commit

Permalink
Merge pull request #10 from aminography/develop
Browse files Browse the repository at this point in the history
Rollback to use threetenbp, plus adding the ability to shift Hijri dates.
  • Loading branch information
aminography authored Dec 29, 2021
2 parents 6e5aafe + 90295f2 commit 34a9500
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ compileTestKotlin {
}

dependencies {
implementation dep.threetenbp
testImplementation dep.junit.core
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ import com.aminography.primecalendar.base.BaseCalendar
import com.aminography.primecalendar.common.CalendarType
import com.aminography.primecalendar.common.DateHolder
import java.text.DateFormatSymbols
import java.util.*
import java.util.Calendar.*
import java.util.Calendar.DAY_OF_MONTH
import java.util.Calendar.DAY_OF_WEEK
import java.util.Calendar.DAY_OF_WEEK_IN_MONTH
import java.util.Calendar.DAY_OF_YEAR
import java.util.Calendar.MONTH
import java.util.Calendar.SATURDAY
import java.util.Calendar.WEEK_OF_MONTH
import java.util.Calendar.WEEK_OF_YEAR
import java.util.Calendar.YEAR
import java.util.Locale
import java.util.TimeZone


/**
Expand Down Expand Up @@ -138,6 +147,9 @@ class HijriCalendar @JvmOverloads constructor(

@Suppress("unused")
companion object {
@JvmStatic
var shiftDate: Int = 0

const val DEFAULT_FIRST_DAY_OF_WEEK = SATURDAY
const val DEFAULT_LOCALE = "ar"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package com.aminography.primecalendar.hijri

import com.aminography.primecalendar.common.DateHolder
import com.aminography.primecalendar.hijri.HijriCalendar.Companion.DEFAULT_LOCALE
import java.time.LocalDate
import java.time.chrono.HijrahDate
import java.time.temporal.ChronoField
import java.util.*
import java.util.Calendar.*
import com.aminography.primecalendar.hijri.HijriCalendar.Companion.shiftDate
import org.threeten.bp.LocalDate
import org.threeten.bp.chrono.HijrahDate
import org.threeten.bp.temporal.ChronoField
import java.util.Calendar.FRIDAY
import java.util.Calendar.MONDAY
import java.util.Calendar.SATURDAY
import java.util.Calendar.SUNDAY
import java.util.Calendar.THURSDAY
import java.util.Calendar.TUESDAY
import java.util.Calendar.WEDNESDAY
import java.util.Locale

/**
* @author aminography
Expand Down Expand Up @@ -231,14 +238,15 @@ object HijriCalendarUtils {

internal fun gregorianToHijri(gregorian: DateHolder): DateHolder {
val gregorianDate = LocalDate.of(gregorian.year, gregorian.month + 1, gregorian.dayOfMonth)
.run { plusDays(shiftDate.toLong()) }
val hijriDate = HijrahDate.from(gregorianDate)
return DateHolder(hijriDate.get(ChronoField.YEAR), hijriDate.get(ChronoField.MONTH_OF_YEAR) - 1, hijriDate.get(ChronoField.DAY_OF_MONTH))
}

internal fun hijriToGregorian(hijri: DateHolder): DateHolder {
val hijriDate = HijrahDate.of(hijri.year, hijri.month + 1, hijri.dayOfMonth)
val gregorianDate = LocalDate.from(hijriDate)
.run { minusDays(shiftDate.toLong()) }
return DateHolder(gregorianDate.get(ChronoField.YEAR), gregorianDate.get(ChronoField.MONTH_OF_YEAR) - 1, gregorianDate.get(ChronoField.DAY_OF_MONTH))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ class ExampleUnitTest {
val persian = PersianCalendar()
println(persian.longDateString)

HijriCalendar.shiftDate = 1

val hijri = HijriCalendar()
println(hijri.longDateString)

Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ enableFeaturePreview('VERSION_CATALOGS')
dependencyResolutionManagement {
versionCatalogs {
dep {
alias('threetenbp').to('org.threeten:threetenbp:1.5.2')
alias('junit-core').to('junit:junit:4.13.2')
}

Expand Down

0 comments on commit 34a9500

Please sign in to comment.