scoop:scoop.std:0.1.0
src/datetime/local_date_time.scoop
Selected production source · src/datetime/local_date_time.scoop
1
package scoop.datetime3
import scoop.decodeTextValue5
import scoop.time.Duration6
import scoop.time.Instant7
import scoop.text.indexOf8
import scoop.text.substring10
/**11
* An immutable local date and time without a time zone. Conversion to an Instant requires an12
* explicit UTC offset.13
*/14
public class LocalDateTime public constructor(15
/**16
* The calendar-date component.17
*/18
public val date: LocalDate,19
/**20
* The local-time component.21
*/22
public val time: LocalTime)23
: Equals<LocalDateTime>, Compare<LocalDateTime>, Hash, ToString, Add<Duration, LocalDateTime>, Sub<Duration, LocalDateTime> {24
public constructor(year: Int, monthNumber: Int, dayOfMonth: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0)25
: this(LocalDate(year, monthNumber, dayOfMonth), LocalTime(hour, minute, second, nanosecond)) {}26
/**27
* The proleptic Gregorian year, including year zero.28
*/29
public val year: Int get() = date.year30
/**31
* The one-based month number in 1..12.32
*/33
public val monthNumber: Int get() = date.monthNumber34
/**35
* The one-based day within the validated month.36
*/37
public val dayOfMonth: Int get() = date.dayOfMonth38
/**39
* The hour of day in 0..23.40
*/41
public val hour: Int get() = time.hour42
/**43
* The minute within the hour in 0..59.44
*/45
public val minute: Int get() = time.minute46
/**47
* The second within the minute in 0..59.48
*/49
public val second: Int get() = time.second50
/**51
* The fractional nanosecond within the second in 0..999999999.52
*/53
public val nanosecond: Int get() = time.nanosecond54
/**55
* Adds the supplied elapsed or calendar amount. Calendar components are applied before time56
* components; fixed-duration overflow carries into the date.57
*/58
public override operator fun plus(other: Duration): LocalDateTime = shifted(other, false)59
/**60
* Subtracts the supplied elapsed or calendar amount. Calendar components are applied before61
* time components; fixed-duration overflow carries into the date.62
*/63
public override operator fun minus(other: Duration): LocalDateTime = shifted(other, true)64
private fun shifted(amount: Duration, subtract: Boolean): LocalDateTime {65
val parts = shiftedTimeOfDay(time, amount, subtract)66
return LocalDateTime(date.plusDays(parts._1), LocalTime.fromNanosecondOfDay(parts._2))67
}68
/**69
* Adds the supplied elapsed or calendar amount. Calendar components are applied before time70
* components; fixed-duration overflow carries into the date.71
*/72
public operator fun plus(other: DatePeriod): LocalDateTime = LocalDateTime(date + other, time)73
/**74
* Subtracts the supplied elapsed or calendar amount. Calendar components are applied before75
* time components; fixed-duration overflow carries into the date.76
*/77
public operator fun minus(other: DatePeriod): LocalDateTime = LocalDateTime(date - other, time)78
/**79
* Adds the supplied elapsed or calendar amount. Calendar components are applied before time80
* components; fixed-duration overflow carries into the date.81
*/82
public operator fun plus(other: DateTimePeriod): LocalDateTime = (this + other.date) + other.time83
/**84
* Subtracts the supplied elapsed or calendar amount. Calendar components are applied before85
* time components; fixed-duration overflow carries into the date.86
*/87
public operator fun minus(other: DateTimePeriod): LocalDateTime = (this - other.date) - other.time88
/**89
* Interprets this local date-time at the explicitly supplied fixed UTC offset.90
*/91
public fun toInstant(offset: UtcOffset): Instant =92
Instant.fromComponents(localEpochSeconds(this) - offset.totalSeconds.toLong(), nanosecond)93
/**94
* Compares the stored value components for equality.95
*/96
public override operator fun equals(other: LocalDateTime): Boolean = date == other.date && time == other.time97
/**98
* Orders values by their chronological or numeric value; only the result sign is significant.99
*/100
public override operator fun compareTo(other: LocalDateTime): Long =101
if (date != other.date) date.compareTo(other.date) else time.compareTo(other.time)102
/**103
* Computes a hash consistent with component equality.104
*/105
public override fun hash(): Long = date.hash() * 31L + time.hash()106
/**107
* Formats this value as ISO local date-time with a T separator.108
*/109
public override fun toString(): String = date.toString() + "T" + time.toString()111
/**112
* Value construction and serialization helpers.113
*/114
public companion object : ValueEncoder<LocalDateTime>, ValueDecoder<LocalDateTime> {115
/**116
* Encodes the value as its canonical text representation.117
*/118
public override fun encode(value: LocalDateTime, encoder: Encoder) { encoder.singleValue().writeString(value.toString()) }119
/**120
* Reads a string value and validates it using this type's parser.121
*/122
public override fun decode(decoder: Decoder): LocalDateTime =123
decodeTextValue<LocalDateTime>(decoder, { text -> parseOrNone(text) }, "local date-time")125
/**126
* Parses ISO local date-time with a T separator, returning None for malformed or127
* out-of-range input.128
*/129
public fun parseOrNone(text: String): LocalDateTime? {130
val split = text.indexOf('T')131
if (split < 0L) return None132
val date = LocalDate.parseOrNone(text.substring(0L, split))133
val time = LocalTime.parseOrNone(text.substring(split + 1L))134
if (date == None || time == None) return None135
return Some(LocalDateTime(date!!, time!!))136
}137
/**138
* Parses ISO local date-time with a T separator or throws IllegalArgumentException for139
* invalid input.140
*/141
public fun parse(text: String): LocalDateTime = when (parseOrNone(text)) {142
case Some(val value) -> value143
case None -> throw IllegalArgumentException(Some("invalid local date-time"))144
}145
}146
}148
internal fun localEpochSeconds(value: LocalDateTime): Long =149
value.date.toEpochDays() * 86400L + value.time.toNanosecondOfDay() / 1000000000L