Scoop API

scoop:scoop.std:0.1.0

src/datetime/date_period.scoop

Selected production source · src/datetime/date_period.scoop

1package scoop.datetime
3/**
4 * Calendar years, months, and days. Components are retained separately; applying a period combines
5 * years/months before days and may clamp an end-of-month date.
6 */
7public struct DatePeriod(val years: Int = 0, val months: Int = 0, val days: Long = 0L) : Equals<DatePeriod>, Hash {
8 /**
9 * Compares the stored value components for equality.
10 */
11 public override operator fun equals(other: DatePeriod): Boolean = years == other.years && months == other.months && days == other.days
12 /**
13 * Computes a hash consistent with component equality.
14 */
15 public override fun hash(): Long = (years.hash() * 31L + months.hash()) * 31L + days.hash()
17 /**
18 * Value construction and serialization helpers.
19 */
20 public companion object : ValueEncoder<DatePeriod>, ValueDecoder<DatePeriod> {
21 /**
22 * Encodes the named components as keyed fields without normalizing the period.
23 */
24 public override fun encode(value: DatePeriod, encoder: Encoder) {
25 val fields = encoder.keyed()
26 Int.encode(value.years, fields.field("years"))
27 Int.encode(value.months, fields.field("months"))
28 Long.encode(value.days, fields.field("days"))
29 fields.end()
30 }
31 /**
32 * Decodes the required named components and reconstructs the period.
33 */
34 public override fun decode(decoder: Decoder): DatePeriod {
35 val fields = decoder.keyed()
36 val years = Int.decode(fields.required("years"))
37 val months = Int.decode(fields.required("months"))
38 val days = Long.decode(fields.required("days"))
39 fields.end()
40 return DatePeriod(years, months, days)
41 }
42 }