Scoop API

scoop:scoop.std:0.1.0

src/datetime/local_time.scoop

Selected production source · src/datetime/local_time.scoop

1package scoop.datetime
3import scoop.decodeTextValue
5import scoop.time.Duration
7/**
8 * An immutable time of day with nanosecond precision. Hours are 0..23, minutes/seconds 0..59, and
9 * nanoseconds 0..999999999; leap seconds are not represented.
10 */
11public class LocalTime public constructor(
12 /**
13 * The hour of day in 0..23.
14 */
15 public val hour: Int,
16 /**
17 * The minute within the hour in 0..59.
18 */
19 public val minute: Int,
20 /**
21 * The second within the minute in 0..59.
22 */
23 public val second: Int = 0,
24 /**
25 * The fractional nanosecond within the second in 0..999999999.
26 */
27 public val nanosecond: Int = 0
28) : Equals<LocalTime>, Compare<LocalTime>, Hash, ToString, Add<Duration, LocalTime>, Sub<Duration, LocalTime> {
29 init {
30 if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59 ||
31 nanosecond < 0 || nanosecond >= 1000000000) throw IllegalArgumentException(Some("invalid time"))
32 }
33 /**
34 * Returns nanoseconds since midnight in 0..<86400000000000.
35 */
36 public fun toNanosecondOfDay(): Long =
37 (hour.toLong() * 3600L + minute.toLong() * 60L + second.toLong()) * 1000000000L + nanosecond.toLong()
38 /**
39 * Adds a duration and wraps across midnight, discarding whole days.
40 */
41 public override operator fun plus(other: Duration): LocalTime =
42 LocalTime.fromNanosecondOfDay(shiftedTimeOfDay(this, other, false)._2)
43 /**
44 * Subtracts a duration and wraps across midnight, discarding whole days.
45 */
46 public override operator fun minus(other: Duration): LocalTime =
47 LocalTime.fromNanosecondOfDay(shiftedTimeOfDay(this, other, true)._2)
48 /**
49 * Compares the stored value components for equality.
50 */
51 public override operator fun equals(other: LocalTime): Boolean =
52 hour == other.hour && minute == other.minute && second == other.second && nanosecond == other.nanosecond
53 /**
54 * Orders values by their chronological or numeric value; only the result sign is significant.
55 */
56 public override operator fun compareTo(other: LocalTime): Long = toNanosecondOfDay().compareTo(other.toNanosecondOfDay())
57 /**
58 * Computes a hash consistent with component equality.
59 */
60 public override fun hash(): Long = toNanosecondOfDay().hash()
61 /**
62 * Formats this value as ISO local time.
63 */
64 public override fun toString(): String = formatLocalTime(this)
66 /**
67 * Value construction and serialization helpers.
68 */
69 public companion object : ValueEncoder<LocalTime>, ValueDecoder<LocalTime> {
70 /**
71 * Encodes the value as its canonical text representation.
72 */
73 public override fun encode(value: LocalTime, encoder: Encoder) { encoder.singleValue().writeString(value.toString()) }
74 /**
75 * Reads a string value and validates it using this type's parser.
76 */
77 public override fun decode(decoder: Decoder): LocalTime =
78 decodeTextValue<LocalTime>(decoder, { text -> parseOrNone(text) }, "time")
80 /**
81 * Constructs a time from nanoseconds since midnight in 0..<86400000000000.
82 */
83 public fun fromNanosecondOfDay(value: Long): LocalTime {
84 if (value < 0L || value >= 86400000000000L) throw IllegalArgumentException(Some("invalid nanosecond of day"))
85 val seconds = value / 1000000000L
86 return LocalTime((seconds / 3600L).toInt(), (seconds / 60L % 60L).toInt(),
87 (seconds % 60L).toInt(), (value % 1000000000L).toInt())
88 }
89 /**
90 * Parses ISO local time, returning None for malformed or out-of-range input.
91 */
92 public fun parseOrNone(text: String): LocalTime? = parseLocalTime(text)
93 /**
94 * Parses ISO local time or throws IllegalArgumentException for invalid input.
95 */
96 public fun parse(text: String): LocalTime = when (parseOrNone(text)) {
97 case Some(val value) -> value
98 case None -> throw IllegalArgumentException(Some("invalid time"))
99 }
100 }
103internal fun shiftedTimeOfDay(time: LocalTime, amount: Duration, subtract: Boolean): (Long, Long) {
104 val days = amount.floorSeconds / 86400L
105 val remainder = (amount.floorSeconds % 86400L) * 1000000000L + amount.nanosecondsOfSecond.toLong()
106 val nanos = time.toNanosecondOfDay() + (if (subtract) -remainder else remainder)
107 val carry = calendarFloorDiv(nanos, 86400000000000L)
108 return ((if (subtract) -days else days) + carry, nanos - carry * 86400000000000L)