scoop:scoop.std:0.1.0
src/datetime/year_month.scoop
Selected production source · src/datetime/year_month.scoop
1
package scoop.datetime3
import scoop.decodeTextValue5
/**6
* An immutable Gregorian year and month in the same supported year range as LocalDate.7
*/8
public class YearMonth public constructor(9
/**10
* The proleptic Gregorian year, including year zero.11
*/12
public val year: Int,13
/**14
* The one-based month number in 1..12.15
*/16
public val monthNumber: Int)17
: Equals<YearMonth>, Compare<YearMonth>, Hash, ToString {18
init {19
if (!validCalendarDate(year, monthNumber, 1)) throw IllegalArgumentException(Some("invalid year-month"))20
}21
public constructor(year: Int, month: Month) : this(year, month.number) {}22
/**23
* The corresponding Gregorian Month value.24
*/25
public val month: Month get() = Month.fromNumber(monthNumber)26
/**27
* The number of days in this month, including leap-year rules.28
*/29
public val lengthOfMonth: Int get() = calendarMonthLength(year, monthNumber)30
/**31
* Whether the year is leap under proleptic Gregorian rules.32
*/33
public val isLeapYear: Boolean get() = calendarLeapYear(year)34
/**35
* Constructs a date in this month, rejecting an invalid day number.36
*/37
public fun atDay(dayOfMonth: Int): LocalDate = LocalDate(year, monthNumber, dayOfMonth)38
/**39
* Shifts by signed calendar months, rejecting unsupported years.40
*/41
public fun plusMonths(value: Long): YearMonth {42
val target = shiftedCalendarMonth(year, monthNumber, value)43
return YearMonth(target._1, target._2)44
}45
/**46
* Shifts by signed calendar years, retaining the month and rejecting unsupported years.47
*/48
public fun plusYears(value: Long): YearMonth {49
checkCalendarYearShift(year, value)50
return YearMonth((year.toLong() + value).toInt(), monthNumber)51
}52
/**53
* Compares the stored value components for equality.54
*/55
public override operator fun equals(other: YearMonth): Boolean = year == other.year && monthNumber == other.monthNumber56
/**57
* Orders values by their chronological or numeric value; only the result sign is significant.58
*/59
public override operator fun compareTo(other: YearMonth): Long =60
if (year != other.year) year.compareTo(other.year) else monthNumber.compareTo(other.monthNumber)61
/**62
* Computes a hash consistent with component equality.63
*/64
public override fun hash(): Long = year.hash() * 31L + monthNumber.hash()65
/**66
* Formats this value as ISO year-month.67
*/68
public override fun toString(): String = formatCalendarYear(year) + "-" + calendarTwoDigits(monthNumber)70
/**71
* Value construction and serialization helpers.72
*/73
public companion object : ValueEncoder<YearMonth>, ValueDecoder<YearMonth> {74
/**75
* Encodes the value as its canonical text representation.76
*/77
public override fun encode(value: YearMonth, encoder: Encoder) { encoder.singleValue().writeString(value.toString()) }78
/**79
* Reads a string value and validates it using this type's parser.80
*/81
public override fun decode(decoder: Decoder): YearMonth =82
decodeTextValue<YearMonth>(decoder, { text -> parseOrNone(text) }, "year-month")84
/**85
* Parses ISO year-month, returning None for malformed or out-of-range input.86
*/87
public fun parseOrNone(text: String): YearMonth? = when (parseCalendarFields(text, false)) {88
case Some(val parts) -> Some(YearMonth(parts._1, parts._2))89
case None -> None90
}91
/**92
* Parses ISO year-month or throws IllegalArgumentException for invalid input.93
*/94
public fun parse(text: String): YearMonth = when (parseOrNone(text)) {95
case Some(val value) -> value96
case None -> throw IllegalArgumentException(Some("invalid year-month"))97
}98
}99
}