Skip to content

Commit

Permalink
Simplify second fraction parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mooreryan committed Jan 11, 2025
1 parent 583932e commit 367a73d
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions src/gleam/time/timestamp.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,7 @@ fn parse_second_fraction_as_nanoseconds(from bytes: BitArray) {
do_parse_second_fraction_as_nanoseconds(
from: <<byte, remaining_bytes:bits>>,
acc: 0,
pow: nanoseconds_per_second,
k: 0,
power: nanoseconds_per_second,
)
}
// bytes starts with a ".", which should introduce a fraction, but it does
Expand All @@ -427,36 +426,32 @@ fn parse_second_fraction_as_nanoseconds(from bytes: BitArray) {
fn do_parse_second_fraction_as_nanoseconds(
from bytes: BitArray,
acc acc: Int,
pow pow: Int,
k k: Int,
power power: Int,
) -> Result(#(Int, BitArray), a) {
// Each digit place to the left in the fractional second is 10x fewer
// nanoseconds.
let power = power / 10

case bytes {
<<byte, remaining_bytes:bytes>>
if byte_zero <= byte && byte <= byte_nine && power < 1
-> {
// We already have the max precision for nanoseconds. Truncate any
// remaining digits.
do_parse_second_fraction_as_nanoseconds(
from: remaining_bytes,
acc:,
power:,
)
}
<<byte, remaining_bytes:bytes>> if byte_zero <= byte && byte <= byte_nine -> {
// Each digit place to the left in the fractional second is 10x fewer
// nanoseconds.
let pow = pow / 10

case int.compare(pow, 1) {
order.Lt -> {
// We already have the max precision for nanoseconds. Truncate any
// remaining digits.
do_parse_second_fraction_as_nanoseconds(
from: remaining_bytes,
acc:,
pow:,
k: k + 1,
)
}
order.Gt | order.Eq -> {
let digit = byte - 0x30
do_parse_second_fraction_as_nanoseconds(
from: remaining_bytes,
acc: acc + digit * pow,
pow:,
k: k + 1,
)
}
}
// We have not yet reached the precision limit. Parse the next digit.
let digit = byte - 0x30
do_parse_second_fraction_as_nanoseconds(
from: remaining_bytes,
acc: acc + digit * power,
power:,
)
}
_ -> Ok(#(acc, bytes))
}
Expand Down

0 comments on commit 367a73d

Please sign in to comment.