scoop:scoop.std:0.1.0
src/datetime/utc_offset.scoop
Selected production source · src/datetime/utc_offset.scoop
1
package scoop.datetime3
import scoop.decodeTextValue5
/**6
* A fixed signed offset from UTC, at one-second resolution and within 18 hours in either7
* direction. This is not a time-zone rule database.8
*/9
public class UtcOffset private constructor(10
/**11
* Signed seconds east of UTC.12
*/13
public val totalSeconds: Int)14
: Equals<UtcOffset>, Compare<UtcOffset>, Hash, ToString {15
/**16
* Compares the stored value components for equality.17
*/18
public override operator fun equals(other: UtcOffset): Boolean = totalSeconds == other.totalSeconds19
/**20
* Orders values by their chronological or numeric value; only the result sign is significant.21
*/22
public override operator fun compareTo(other: UtcOffset): Long = totalSeconds.compareTo(other.totalSeconds)23
/**24
* Computes a hash consistent with component equality.25
*/26
public override fun hash(): Long = totalSeconds.hash()27
/**28
* Formats this value as Z or a signed HH:MM offset with optional seconds.29
*/30
public override fun toString(): String {31
if (totalSeconds == 0) return "Z"32
val magnitude = if (totalSeconds < 0) -totalSeconds else totalSeconds33
val base = (if (totalSeconds < 0) "-" else "+") + calendarTwoDigits(magnitude / 3600) + ":" +34
calendarTwoDigits(magnitude / 60 % 60)35
return if (magnitude % 60 == 0) base else base + ":" + calendarTwoDigits(magnitude % 60)36
}37
/**38
* Value construction and serialization helpers.39
*/40
public companion object : ValueEncoder<UtcOffset>, ValueDecoder<UtcOffset> {41
/**42
* Encodes the value as its canonical text representation.43
*/44
public override fun encode(value: UtcOffset, encoder: Encoder) { encoder.singleValue().writeString(value.toString()) }45
/**46
* Reads a string value and validates it using this type's parser.47
*/48
public override fun decode(decoder: Decoder): UtcOffset =49
decodeTextValue<UtcOffset>(decoder, { text -> parseOrNone(text) }, "UTC offset")51
/**52
* The zero UTC offset.53
*/54
public val ZERO: UtcOffset = UtcOffset(0)55
/**56
* Constructs a fixed offset in -64800..64800 seconds.57
*/58
public fun fromTotalSeconds(value: Int): UtcOffset {59
if (value < -64800 || value > 64800) throw IllegalArgumentException(Some("offset exceeds 18 hours"))60
return UtcOffset(value)61
}62
/**63
* Combines offset components of consistent sign, rejecting totals beyond 18 hours.64
*/65
public fun fromHoursMinutesSeconds(hours: Int = 0, minutes: Int = 0, seconds: Int = 0): UtcOffset {66
if (hours < -18 || hours > 18 || minutes < -59 || minutes > 59 || seconds < -59 || seconds > 59 ||67
((hours < 0 || minutes < 0 || seconds < 0) && (hours > 0 || minutes > 0 || seconds > 0))) {68
throw IllegalArgumentException(Some("invalid offset components"))69
}70
return fromTotalSeconds(hours * 3600 + minutes * 60 + seconds)71
}72
/**73
* Parses Z or a signed HH:MM offset with optional seconds, returning None for malformed or74
* out-of-range input.75
*/76
public fun parseOrNone(text: String): UtcOffset? {77
if (text == "Z") return Some(ZERO)78
val chars = text.toCharArray()79
if ((chars.size != 6L && chars.size != 9L) || (chars[0L] != '+' && chars[0L] != '-') || chars[3L] != ':') return None80
val hours = calendarParseTwo(chars, 1L)81
val minutes = calendarParseTwo(chars, 4L)82
var seconds = 083
if (chars.size == 9L) {84
if (chars[6L] != ':') return None85
seconds = calendarParseTwo(chars, 7L)86
}87
if (hours < 0 || hours > 18 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59) return None88
val total = hours * 3600 + minutes * 60 + seconds89
if (total > 64800) return None90
return Some(UtcOffset(if (chars[0L] == '-') -total else total))91
}92
/**93
* Parses Z or a signed HH:MM offset with optional seconds or throws94
* IllegalArgumentException for invalid input.95
*/96
public fun parse(text: String): UtcOffset = when (parseOrNone(text)) {97
case Some(val value) -> value98
case None -> throw IllegalArgumentException(Some("invalid UTC offset"))99
}100
}101
}