Scoop API

scoop:scoop.std:0.1.0

src/datetime/local_date.scoop

Selected production source · src/datetime/local_date.scoop

1package scoop.datetime
3import scoop.decodeTextValue
5/**
6 * An immutable proleptic Gregorian date in years -999999999..999999999. Construction validates
7 * month/day combinations; it carries no time zone.
8 */
9public class LocalDate public constructor(
10 /**
11 * The proleptic Gregorian year, including year zero.
12 */
13 public val year: Int,
14 /**
15 * The one-based month number in 1..12.
16 */
17 public val monthNumber: Int,
18 /**
19 * The one-based day within the validated month.
20 */
21 public val dayOfMonth: Int)
22 : Equals<LocalDate>, Compare<LocalDate>, Hash, ToString, Add<DatePeriod, LocalDate>, Sub<DatePeriod, LocalDate> {
23 init {
24 if (!validCalendarDate(year, monthNumber, dayOfMonth)) throw IllegalArgumentException(Some("invalid date"))
25 }
26 public constructor(year: Int, month: Month, dayOfMonth: Int) : this(year, month.number, dayOfMonth) {}
27 /**
28 * The corresponding Gregorian Month value.
29 */
30 public val month: Month get() = Month.fromNumber(monthNumber)
31 /**
32 * The year/month portion of this date.
33 */
34 public val yearMonth: YearMonth get() = YearMonth(year, monthNumber)
35 /**
36 * The one-based ordinal day in this year.
37 */
38 public val dayOfYear: Int get() = (toEpochDays() - calendarEpochDay(year, 1, 1) + 1L).toInt()
39 /**
40 * The ISO weekday calculated from this date.
41 */
42 public val dayOfWeek: DayOfWeek get() {
43 val shifted = toEpochDays() + 3L
44 return DayOfWeek.fromIsoDayNumber((shifted - calendarFloorDiv(shifted, 7L) * 7L + 1L).toInt())
45 }
46 /**
47 * Returns signed days since 1970-01-01.
48 */
49 public fun toEpochDays(): Long = calendarEpochDay(year, monthNumber, dayOfMonth)
50 /**
51 * Shifts by a signed number of calendar days; unsupported results throw ArithmeticException.
52 */
53 public fun plusDays(value: Long): LocalDate = shiftedDays(value, false)
54 private fun shiftedDays(value: Long, subtract: Boolean): LocalDate {
55 val current = toEpochDays()
56 val lower = LocalDate.MIN.toEpochDays() - current
57 val upper = LocalDate.MAX.toEpochDays() - current
58 if (value < (if (subtract) -upper else lower) || value > (if (subtract) -lower else upper)) throw ArithmeticException()
59 return LocalDate.fromEpochDays(if (subtract) current - value else current + value)
60 }
61 /**
62 * Shifts by calendar months, clamping the day to the destination month's last valid day.
63 */
64 public fun plusMonths(value: Long): LocalDate {
65 val target = shiftedCalendarMonth(year, monthNumber, value)
66 val last = calendarMonthLength(target._1, target._2)
67 return LocalDate(target._1, target._2, if (dayOfMonth > last) last else dayOfMonth)
68 }
69 /**
70 * Shifts by calendar years, clamping February 29 when needed.
71 */
72 public fun plusYears(value: Long): LocalDate {
73 checkCalendarYearShift(year, value)
74 return plusMonths(value * 12L)
75 }
76 /**
77 * Returns other minus this in whole calendar days.
78 */
79 public fun daysUntil(other: LocalDate): Long = other.toEpochDays() - toEpochDays()
80 /**
81 * Adds combined years/months first, clamping the day, then adds period days.
82 */
83 public override operator fun plus(other: DatePeriod): LocalDate =
84 plusMonths(other.years.toLong() * 12L + other.months.toLong()).shiftedDays(other.days, false)
85 /**
86 * Subtracts combined years/months first, clamping the day, then subtracts period days.
87 */
88 public override operator fun minus(other: DatePeriod): LocalDate =
89 plusMonths(-other.years.toLong() * 12L - other.months.toLong()).shiftedDays(other.days, true)
90 /**
91 * Compares the stored value components for equality.
92 */
93 public override operator fun equals(other: LocalDate): Boolean =
94 year == other.year && monthNumber == other.monthNumber && dayOfMonth == other.dayOfMonth
95 /**
96 * Orders values by their chronological or numeric value; only the result sign is significant.
97 */
98 public override operator fun compareTo(other: LocalDate): Long = toEpochDays().compareTo(other.toEpochDays())
99 /**
100 * Computes a hash consistent with component equality.
101 */
102 public override fun hash(): Long = (year.hash() * 31L + monthNumber.hash()) * 31L + dayOfMonth.hash()
103 /**
104 * Formats this value as ISO calendar date.
105 */
106 public override fun toString(): String = yearMonth.toString() + "-" + calendarTwoDigits(dayOfMonth)
108 /**
109 * Value construction and serialization helpers.
110 */
111 public companion object : ValueEncoder<LocalDate>, ValueDecoder<LocalDate> {
112 /**
113 * Encodes the value as its canonical text representation.
114 */
115 public override fun encode(value: LocalDate, encoder: Encoder) { encoder.singleValue().writeString(value.toString()) }
116 /**
117 * Reads a string value and validates it using this type's parser.
118 */
119 public override fun decode(decoder: Decoder): LocalDate =
120 decodeTextValue<LocalDate>(decoder, { text -> parseOrNone(text) }, "date")
122 /**
123 * The earliest or smallest supported value.
124 */
125 public val MIN: LocalDate = LocalDate(-999999999, 1, 1)
126 /**
127 * The latest or largest supported value.
128 */
129 public val MAX: LocalDate = LocalDate(999999999, 12, 31)
130 /**
131 * Constructs a date from signed days since 1970-01-01; unsupported values throw.
132 */
133 public fun fromEpochDays(value: Long): LocalDate {
134 if (value < MIN.toEpochDays() || value > MAX.toEpochDays()) throw IllegalArgumentException(Some("invalid epoch day"))
135 val parts = calendarDateFromDay(value)
136 return LocalDate(parts._1, parts._2, parts._3)
137 }
138 /**
139 * Parses ISO calendar date, returning None for malformed or out-of-range input.
140 */
141 public fun parseOrNone(text: String): LocalDate? = when (parseCalendarFields(text, true)) {
142 case Some(val parts) -> Some(LocalDate(parts._1, parts._2, parts._3))
143 case None -> None
144 }
145 /**
146 * Parses ISO calendar date or throws IllegalArgumentException for invalid input.
147 */
148 public fun parse(text: String): LocalDate = when (parseOrNone(text)) {
149 case Some(val value) -> value
150 case None -> throw IllegalArgumentException(Some("invalid date"))
151 }
152 }