scoop:scoop.std:0.1.0
src/datetime/offset_date_time.scoop
Selected production source · src/datetime/offset_date_time.scoop
1
package scoop.datetime3
import scoop.decodeTextValue5
import scoop.time.Instant6
import scoop.text.indexOf7
import scoop.text.substring9
/**10
* A local date-time paired with a fixed UTC offset. Equality compares both components, so equal11
* instants can have unequal offset date-times.12
*/13
public class OffsetDateTime public constructor(14
/**15
* The local date and time component.16
*/17
public val dateTime: LocalDateTime,18
/**19
* The fixed offset used to interpret the local component.20
*/21
public val offset: UtcOffset)22
: Equals<OffsetDateTime>, Hash, ToString {23
/**24
* Converts the local component and offset into an absolute instant.25
*/26
public fun toInstant(): Instant = dateTime.toInstant(offset)27
/**28
* Changes the offset and adjusts local fields to preserve the absolute instant.29
*/30
public fun withOffsetSameInstant(offset: UtcOffset): OffsetDateTime = OffsetDateTime(toInstant().toLocalDateTime(offset), offset)31
/**32
* Changes the offset while retaining local fields, changing the represented instant.33
*/34
public fun withOffsetSameLocal(offset: UtcOffset): OffsetDateTime = OffsetDateTime(dateTime, offset)35
/**36
* Compares the stored value components for equality.37
*/38
public override operator fun equals(other: OffsetDateTime): Boolean = dateTime == other.dateTime && offset == other.offset39
/**40
* Computes a hash consistent with component equality.41
*/42
public override fun hash(): Long = dateTime.hash() * 31L + offset.hash()43
/**44
* Formats this value as ISO local date-time followed by a UTC offset.45
*/46
public override fun toString(): String = dateTime.toString() + offset.toString()48
/**49
* Value construction and serialization helpers.50
*/51
public companion object : ValueEncoder<OffsetDateTime>, ValueDecoder<OffsetDateTime> {52
/**53
* Encodes the value as its canonical text representation.54
*/55
public override fun encode(value: OffsetDateTime, encoder: Encoder) { encoder.singleValue().writeString(value.toString()) }56
/**57
* Reads a string value and validates it using this type's parser.58
*/59
public override fun decode(decoder: Decoder): OffsetDateTime =60
decodeTextValue<OffsetDateTime>(decoder, { text -> parseOrNone(text) }, "offset date-time")62
/**63
* Parses ISO local date-time followed by a UTC offset, returning None for malformed or64
* out-of-range input.65
*/66
public fun parseOrNone(text: String): OffsetDateTime? {67
val split = text.indexOf('T')68
if (split < 0L) return None69
val chars = text.toCharArray()70
var index = split + 1L71
while (index < chars.size && chars[index] != 'Z' && chars[index] != '+' && chars[index] != '-') index += 1L72
if (index == chars.size) return None73
val local = LocalDateTime.parseOrNone(text.substring(0L, index))74
val offset = UtcOffset.parseOrNone(text.substring(index))75
if (local == None || offset == None) return None76
return Some(OffsetDateTime(local!!, offset!!))77
}78
/**79
* Parses ISO local date-time followed by a UTC offset or throws IllegalArgumentException80
* for invalid input.81
*/82
public fun parse(text: String): OffsetDateTime = when (parseOrNone(text)) {83
case Some(val value) -> value84
case None -> throw IllegalArgumentException(Some("invalid offset date-time"))85
}86
}87
}