scoop:scoop.std:0.1.0
src/time/duration.scoop
Selected production source · src/time/duration.scoop
1
package scoop.time3
import scoop.decodeTextValue5
/**6
* A signed elapsed duration with nanosecond precision, normalized as floor seconds plus7
* nonnegative fractional nanoseconds. Arithmetic throws on representational overflow.8
*/9
public class Duration private constructor(10
/**11
* The whole-second component rounded toward negative infinity.12
*/13
public val floorSeconds: Long,14
/**15
* The nonnegative fractional component in 0..999999999.16
*/17
public val nanosecondsOfSecond: Int18
)19
: Equals<Duration>, Compare<Duration>, Hash, ToString, Add<Duration, Duration>, Sub<20
Duration,21
Duration22
>,23
Mul<Long, Duration>, Div<Long, Duration>, UnaryMinus<Duration> {24
/**25
* Compares the stored value components for equality.26
*/27
public override operator fun equals(other: Duration): Boolean =28
floorSeconds == other.floorSeconds && nanosecondsOfSecond == other.nanosecondsOfSecond29
/**30
* Orders values by their chronological or numeric value; only the result sign is significant.31
*/32
public override operator fun compareTo(other: Duration): Long =33
if (floorSeconds != other.floorSeconds)34
floorSeconds.compareTo(other.floorSeconds)35
else36
nanosecondsOfSecond.compareTo(other.nanosecondsOfSecond)37
/**38
* Computes a hash consistent with component equality.39
*/40
public override fun hash(): Long = floorSeconds.hash() * 31L + nanosecondsOfSecond.hash()41
/**42
* Adds another duration with checked representational overflow.43
*/44
public override operator fun plus(other: Duration): Duration {45
var seconds = floorSeconds + other.floorSeconds46
var nanos = nanosecondsOfSecond + other.nanosecondsOfSecond47
if (nanos >= 1000000000) {48
seconds += 1L49
nanos -= 100000000050
}51
// Include the carry before checking whether the final signed result overflowed.52
if (((floorSeconds xor seconds) and (other.floorSeconds xor seconds)) < 0L)53
throw ArithmeticException()54
return Duration(seconds, nanos)55
}56
/**57
* Subtracts another duration with checked representational overflow.58
*/59
public override operator fun minus(other: Duration): Duration {60
var seconds = floorSeconds - other.floorSeconds61
var nanos = nanosecondsOfSecond - other.nanosecondsOfSecond62
if (nanos < 0) {63
seconds -= 1L64
nanos += 100000000065
}66
// A fractional borrow can bring an overflowing intermediate back into range.67
if (((floorSeconds xor other.floorSeconds) and (floorSeconds xor seconds)) < 0L)68
throw ArithmeticException()69
return Duration(seconds, nanos)70
}71
/**72
* Negates the duration, throwing if the result cannot be represented.73
*/74
public override operator fun unaryMinus(): Duration =75
Duration.fromParts(durationParts(this).negated())76
/**77
* Multiplies by a signed integer with checked overflow.78
*/79
public override operator fun times(other: Long): Duration =80
Duration.fromParts(multiplyDurationParts(durationParts(this), other))81
/**82
* Divides by a signed integer, truncating subnanosecond remainder toward zero; zero divisor or83
* overflow throws.84
*/85
public override operator fun div(other: Long): Duration =86
Duration.fromParts(divideDurationParts(durationParts(this), other))87
/**88
* Returns the nonnegative magnitude, throwing if it cannot be represented.89
*/90
public val absoluteValue: Duration get() =91
if (floorSeconds < 0L)92
-this93
else94
this95
/**96
* Returns whole seconds truncated toward zero; a result outside Long range throws97
* ArithmeticException.98
*/99
public val inWholeSeconds: Long get() =100
if (floorSeconds < 0L && nanosecondsOfSecond != 0)101
floorSeconds + 1L102
else103
floorSeconds104
/**105
* Returns whole minutes truncated toward zero; a result outside Long range throws106
* ArithmeticException.107
*/108
public val inWholeMinutes: Long get() = inWholeSeconds / 60L109
/**110
* Returns whole hours truncated toward zero; a result outside Long range throws111
* ArithmeticException.112
*/113
public val inWholeHours: Long get() = inWholeSeconds / 3600L114
/**115
* Returns whole days truncated toward zero; a result outside Long range throws116
* ArithmeticException.117
*/118
public val inWholeDays: Long get() = inWholeSeconds / 86400L119
/**120
* Returns whole milliseconds truncated toward zero; a result outside Long range throws121
* ArithmeticException.122
*/123
public val inWholeMilliseconds: Long get() = durationWholeUnits(durationParts(this), 1000uL)124
/**125
* Returns whole microseconds truncated toward zero; a result outside Long range throws126
* ArithmeticException.127
*/128
public val inWholeMicroseconds: Long get() = durationWholeUnits(durationParts(this), 1000000uL)129
/**130
* Returns whole nanoseconds truncated toward zero; a result outside Long range throws131
* ArithmeticException.132
*/133
public val inWholeNanoseconds: Long get() =134
durationWholeUnits(durationParts(this), 1000000000uL)135
/**136
* Returns whole seconds truncated toward zero and a signed nanosecond remainder.137
*/138
public fun toComponents(): (Long, Int) =139
(140
inWholeSeconds,141
if (floorSeconds < 0L && nanosecondsOfSecond != 0)142
nanosecondsOfSecond - 1000000000143
else144
nanosecondsOfSecond145
)146
/**147
* Formats the signed duration in ISO duration notation.148
*/149
public fun toIsoString(): String = formatDuration(durationParts(this))150
/**151
* Formats this value as ISO duration.152
*/153
public override fun toString(): String = toIsoString()155
/**156
* Value construction and serialization helpers.157
*/158
public companion object: ValueEncoder<Duration>, ValueDecoder<Duration> {159
/**160
* Encodes the value as its canonical text representation.161
*/162
public override fun encode(value: Duration, encoder: Encoder) {163
encoder.singleValue().writeString(value.toString())164
}165
/**166
* Reads a string value and validates it using this type's parser.167
*/168
public override fun decode(decoder: Decoder): Duration =169
decodeTextValue<Duration>(decoder, { text -> parseOrNone(text) }, "duration")171
/**172
* A zero-length duration.173
*/174
public val ZERO: Duration = Duration(0L, 0)175
/**176
* Combines whole seconds and an arbitrary signed nanosecond adjustment, normalizing with177
* checked overflow.178
*/179
public fun seconds(value: Long, nanosecondAdjustment: Long = 0L): Duration {180
val parts = normalizeDuration(value, nanosecondAdjustment)181
return Duration(parts._1, parts._2)182
}183
/**184
* Constructs a duration from signed milliseconds with checked overflow.185
*/186
public fun milliseconds(value: Long): Duration =187
seconds(value / 1000L, (value % 1000L) * 1000000L)188
/**189
* Constructs a duration from signed microseconds with checked overflow.190
*/191
public fun microseconds(value: Long): Duration =192
seconds(value / 1000000L, (value % 1000000L) * 1000L)193
/**194
* Constructs a duration from signed nanoseconds with checked overflow.195
*/196
public fun nanoseconds(value: Long): Duration = seconds(0L, value)197
/**198
* Constructs a duration from signed minutes with checked overflow.199
*/200
public fun minutes(value: Long): Duration = seconds(value) * 60L201
/**202
* Constructs a duration from signed hours with checked overflow.203
*/204
public fun hours(value: Long): Duration = seconds(value) * 3600L205
/**206
* Constructs a duration from signed days with checked overflow.207
*/208
public fun days(value: Long): Duration = seconds(value) * 86400L209
/**210
* Parses ISO duration, returning None for malformed or out-of-range input.211
*/212
public fun parseOrNone(text: String): Duration? = parseDuration(text)213
/**214
* Parses ISO duration or throws IllegalArgumentException for invalid input.215
*/216
public fun parse(text: String): Duration =217
when (parseOrNone(text)) {218
case Some(val value) -> value219
case None -> throw IllegalArgumentException(Some("invalid duration"))220
}221
internal fun fromParts(parts: DurationParts): Duration {222
val limit =223
if (parts.negative)224
0x8000000000000000uL225
else226
0x7fffffffffffffffuL227
if (228
parts.seconds > limit || (parts.seconds == 0x8000000000000000uL && parts.nanos != 0)229
)230
throw ArithmeticException()231
if (!parts.negative)232
return Duration(parts.seconds.toLong(), parts.nanos)233
if (parts.nanos == 0)234
return Duration((-parts.seconds).toLong(), 0)235
return Duration((-parts.seconds - 1uL).toLong(), 1000000000 - parts.nanos)236
}237
}238
}