Skip to content

Commit

Permalink
Removed invoke() constructors from DayOfWeek and Month (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikc5000 committed Nov 12, 2019
1 parent 820a20e commit 925e967
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 56 deletions.
4 changes: 2 additions & 2 deletions core/src/commonMain/kotlin/io/islandtime/Date.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Date(
year: Int,
monthNumber: Int,
day: Int
) : this(year, Month(monthNumber), day)
) : this(year, monthNumber.toMonth(), day)

/**
* The day of the week
Expand Down Expand Up @@ -320,7 +320,7 @@ class Date(
val dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1
yearEst += (marchMonth0 / 10).toLong()

return Date(yearEst.toInt(), Month(month), dom)
return Date(yearEst.toInt(), month, dom)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/io/islandtime/DateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DateTime(
minute: Int,
second: Int = 0,
nanosecond: Int = 0
) : this(year, Month(monthNumber), day, hour, minute, second, nanosecond)
) : this(year, monthNumber.toMonth(), day, hour, minute, second, nanosecond)

/**
* The hour of the day
Expand Down
47 changes: 30 additions & 17 deletions core/src/commonMain/kotlin/io/islandtime/DayOfWeek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ package io.islandtime
import io.islandtime.internal.DAYS_IN_WEEK
import io.islandtime.measures.IntDays

enum class DayOfWeek(val number: Int) {
MONDAY(1),
TUESDAY(2),
WEDNESDAY(3),
THURSDAY(4),
FRIDAY(5),
SATURDAY(6),
SUNDAY(7);
/**
* A day of the week.
*/
enum class DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;

/**
* The ISO day of week number.
*
* The ISO week starts on Monday (1) and ends on Sunday (7).
*/
val number: Int get() = ordinal + 1

operator fun plus(days: IntDays): DayOfWeek {
val daysToAdd = days.value % DAYS_IN_WEEK
Expand All @@ -22,15 +32,18 @@ enum class DayOfWeek(val number: Int) {
companion object {
val MIN = MONDAY
val MAX = SUNDAY

operator fun invoke(number: Int): DayOfWeek {
if (number !in MIN.number..MAX.number) {
throw DateTimeException("'$number' is not a valid day of week number")
}

return values()[number - 1]
}
}
}

fun Int.toDayOfWeek() = DayOfWeek(this)
/**
* Convert an ISO day of week number to a [DayOfWeek].
*
* The ISO week starts on Monday (1) and ends on Sunday (7).
*/
fun Int.toDayOfWeek(): DayOfWeek {
if (this !in DayOfWeek.MIN.number..DayOfWeek.MAX.number) {
throw DateTimeException("'$this' is not a valid day of week number")
}

return DayOfWeek.values()[this - 1]
}
51 changes: 29 additions & 22 deletions core/src/commonMain/kotlin/io/islandtime/Month.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,63 @@ import io.islandtime.measures.days
/**
* A month of the year.
*/
enum class Month(val number: Int) {
JANUARY(1) {
enum class Month {
JANUARY {
override val lengthInCommonYear = 31.days
override val firstDayOfCommonYear = 1
},
FEBRUARY(2) {
FEBRUARY {
override val lengthInCommonYear = 28.days
override val lengthInLeapYear = 29.days
override val firstDayOfCommonYear = 32
},
MARCH(3) {
MARCH {
override val lengthInCommonYear = 31.days
override val firstDayOfCommonYear = 60
},
APRIL(4) {
APRIL {
override val lengthInCommonYear = 30.days
override val firstDayOfCommonYear = 91
},
MAY(5) {
MAY {
override val lengthInCommonYear = 31.days
override val firstDayOfCommonYear = 121
},
JUNE(6) {
JUNE {
override val lengthInCommonYear = 30.days
override val firstDayOfCommonYear = 152
},
JULY(7) {
JULY {
override val lengthInCommonYear = 31.days
override val firstDayOfCommonYear = 182
},
AUGUST(8) {
AUGUST {
override val lengthInCommonYear = 31.days
override val firstDayOfCommonYear = 213
},
SEPTEMBER(9) {
SEPTEMBER {
override val lengthInCommonYear = 30.days
override val firstDayOfCommonYear = 244
},
OCTOBER(10) {
OCTOBER {
override val lengthInCommonYear = 31.days
override val firstDayOfCommonYear = 272
},
NOVEMBER(11) {
NOVEMBER {
override val lengthInCommonYear = 30.days
override val firstDayOfCommonYear = 305
},
DECEMBER(12) {
DECEMBER {
override val lengthInCommonYear = 31.days
override val firstDayOfCommonYear = 335
};


/**
* Get the ISO month number, from 1-12.
*/
val number: Int get() = ordinal + 1

/**
* The number of days in the month in a common year.
*/
Expand Down Expand Up @@ -147,15 +153,16 @@ enum class Month(val number: Int) {
companion object {
val MIN = JANUARY
val MAX = DECEMBER

operator fun invoke(number: Int): Month {
if (number !in MIN.number..MAX.number) {
throw DateTimeException("'$number' is not a valid month of the year")
}

return values()[number - 1]
}
}
}

fun Int.toMonth() = Month(this)
/**
* Convert an ISO month number (from 1-12) to a [Month].
*/
fun Int.toMonth(): Month {
if (this !in Month.MIN.number..Month.MAX.number) {
throw DateTimeException("'this' is not a valid month of the year")
}

return Month.values()[this - 1]
}
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/io/islandtime/OffsetDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class OffsetDateTime(
second: Int,
nanosecond: Int,
offset: UtcOffset
) : this(DateTime(year, Month(monthNumber), dayOfMonth, hour, minute, second, nanosecond), offset)
) : this(DateTime(year, monthNumber.toMonth(), dayOfMonth, hour, minute, second, nanosecond), offset)

inline val date: Date get() = dateTime.date
inline val time: Time get() = dateTime.time
Expand Down
4 changes: 2 additions & 2 deletions core/src/commonMain/kotlin/io/islandtime/YearMonth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ inline class YearMonth internal constructor(
*/
@Suppress("FunctionName")
fun YearMonth(year: Int, monthNumber: Int): YearMonth {
return YearMonth(year, Month(monthNumber))
return YearMonth(year, monthNumber.toMonth())
}

/**
Expand Down Expand Up @@ -204,4 +204,4 @@ infix fun Year.at(month: Month) = YearMonth(value, month)
/**
* Combine a year and month number to get a [YearMonth]
*/
fun Year.atMonth(number: Int) = YearMonth(value, Month(number))
fun Year.atMonth(number: Int) = YearMonth(value, number.toMonth())
6 changes: 1 addition & 5 deletions core/src/commonTest/kotlin/io/islandtime/DayOfWeekTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class DayOfWeekTest : AbstractIslandTimeTest() {
@Test
fun `DayOfWeek() returns the day of the week by number, starting with Monday`() {
assertEquals(DayOfWeek.WEDNESDAY, DayOfWeek(3))
}

@Test
fun `Int_toDayOfWeek() throws an exception when the number is out of range`() {
assertFailsWith<DateTimeException> { 0.toDayOfWeek() }
Expand All @@ -22,6 +17,7 @@ class DayOfWeekTest : AbstractIslandTimeTest() {
@Test
fun `Int_toDayOfWeek() returns the correct day`() {
assertEquals(DayOfWeek.MONDAY, 1.toDayOfWeek())
assertEquals(DayOfWeek.WEDNESDAY, 3.toDayOfWeek())
assertEquals(DayOfWeek.SUNDAY, 7.toDayOfWeek())
}

Expand Down
6 changes: 1 addition & 5 deletions core/src/commonTest/kotlin/io/islandtime/MonthTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class MonthTest : AbstractIslandTimeTest() {
@Test
fun `Month() returns a month by ISO number`() {
assertEquals(Month.APRIL, Month(4))
}

@Test
fun `Int_toMonth() throws an exception when the value isn't a valid ISO month number`() {
assertFailsWith<DateTimeException> { 0.toMonth() }
Expand All @@ -22,6 +17,7 @@ class MonthTest : AbstractIslandTimeTest() {
@Test
fun `Int_toMonth() gets a Month from an ISO month number when it's in range`() {
assertEquals(Month.JANUARY, 1.toMonth())
assertEquals(Month.APRIL, 4.toMonth())
assertEquals(Month.DECEMBER, 12.toMonth())
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/commonTest/kotlin/io/islandtime/YearMonthTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class YearMonthTest : AbstractIslandTimeTest() {

assertEquals(
YearMonth(2018, Month.APRIL),
Year(2018) at Month(4)
Year(2018) at 4.toMonth()
)
}

Expand Down

0 comments on commit 925e967

Please sign in to comment.