Skip to content

Commit fcbcfd8

Browse files
tannevaledclaude
andauthored
Time: advance MRI 4.0.6 conformance (arithmetic, sub-nanosecond precision, timezones) (#563)
* WIP: partial time2 conformance work (agent interrupted by session limit) Committed to protect against scratchpad purge; to be finished, verified and squashed before the PR is opened. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Time: exact +/- arithmetic, timezone-object getlocal/localtime, exact Rational seconds Route Time#+/#- through a VM-aware path so a Rational/Float shift is coerced with MRI num_exact and applied to the nanosecond (fixing coercion errors and negative-Float truncation); reject Time+Time. Handle a timezone object in Time#getlocal/#localtime via #utc_to_local. Parse a Rational seconds field for Time.utc/local exactly instead of through a lossy Float64. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * gofmt wave11 test file Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Time: honour runtime TZ for local zone, fix %Z and -00:00 zone Resolve the local timezone dynamically from ENV['TZ'] (Go tzdata), so a spec's with_timezone block gets DST-aware IANA zones (EST/EDT etc.) and an unloadable name defaults to UTC. strftime %Z now emits nothing for an offset-only zone (matching MRI, not the numeric offset). A '-00:00' offset string is RFC 3339 unknown-offset UTC, so #zone reports 'UTC'. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Time: preserve exact sub-nanosecond sub-second (frac field) Carry a big.Rat 'frac' for the sub-second part below one nanosecond that Go's instant cannot hold, so Time#+/#- and Time.at(Float/Rational) keep MRI's exact sub-second in #subsec and #to_r (whole-nanosecond fields unchanged). A non-finite Time.at Float now raises FloatDomainError. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Time: Time.new(String, precision:) keeps sub-nanosecond digits precision: nil or a negative count now keeps every sub-second digit (past the ninth into frac) instead of clamping to 9, matching MRI 4.0.6; the default still keeps 9 and an explicit count truncates. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * Time: mix in Comparable Time.include?(Comparable) is now true (MRI mixes it in); Time's own #<=> drives Comparable's #between?/#clamp. Added in the post-prelude phase so the module exists. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * test: cover numFloat's Float/Rational/error branches via File.utime Removing timeOp orphaned numFloat's non-Integer branches; File.utime is its remaining caller and legitimately accepts Float/Rational second counts and rejects non-numerics, so exercise all branches there. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 191d44a commit fcbcfd8

7 files changed

Lines changed: 513 additions & 81 deletions

File tree

‎internal/vm/arith.go‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ func binary(op bytecode.Op, a, b object.Value) object.Value {
9090
return matrixOp(op, a, b)
9191
}
9292

93-
// Time arithmetic: t + secs / t - secs (shift by a Duration) and t - other
94-
// (the seconds between two instants) reach the operator fast path.
95-
if at, ok := a.(*Time); ok {
96-
return timeOp(op, at, b)
93+
// Time reaches the VM-less path only for an operator other than + / - (those
94+
// are intercepted in binaryOp, which has the VM needed for MRI's num_exact
95+
// coercion). Every other arithmetic operator on a Time is undefined.
96+
if _, ok := a.(*Time); ok {
97+
return raise("NoMethodError", "undefined method '%s' for an instance of Time", arithOpName(op))
9798
}
9899

99100
// Date arithmetic: d + n / d - n (shift by a whole number of days) and
@@ -413,6 +414,14 @@ func (vm *VM) binaryOp(op bytecode.Op, a, b object.Value) object.Value {
413414
if _, isSB := a.(*SafeBufferVal); isSB {
414415
return vm.send(a, arithOpName(op), []object.Value{b}, nil)
415416
}
417+
// Time arithmetic (t + n / t - n, and t - other) needs a live VM: MRI's
418+
// num_exact coercion of the shift amount (Integer/Rational kept exact, a
419+
// Float taken through its exact #to_r, any other object via #to_r, while a
420+
// String or nil is rejected) and the nanosecond-exact result both require
421+
// dispatch the VM-less binary path cannot do.
422+
if at, ok := a.(*Time); ok && (op == bytecode.OpAdd || op == bytecode.OpSub) {
423+
return vm.timeArith(op, at, b)
424+
}
416425
// Numeric coercion protocol: when a built-in number is combined with a
417426
// non-numeric object that answers #coerce, MRI calls other.coerce(self)
418427
// — expecting a [a2, b2] pair of same-type numbers — then re-runs the

‎internal/vm/file_ops_internal_test.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ func TestFileMetaOps(t *testing.T) {
2727
cases := []struct{ src, want string }{
2828
// chmod returns the number of paths; multiple paths sum.
2929
{`p File.chmod(0o600, "` + f + `")`, "1\n"},
30-
// utime returns the count; accepts a Time and an Integer second count.
30+
// utime returns the count; accepts a Time, and Integer/Float/Rational
31+
// second counts (each coerced to whole Unix seconds).
3132
{`p File.utime(Time.now, Time.now, "` + f + `")`, "1\n"},
3233
{`p File.utime(0, 0, "` + f + `")`, "1\n"},
34+
{`p File.utime(Rational(3, 2), 2.0, "` + f + `")`, "1\n"},
3335
// chown with nil ids leaves them unchanged and still reports the count.
3436
{`p File.chown(nil, nil, "` + f + `")`, "1\n"},
3537
{`p File.lchown(nil, nil, "` + f + `")`, "1\n"},
@@ -47,6 +49,10 @@ func TestFileMetaOps(t *testing.T) {
4749
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
4850
}
4951
}
52+
// A non-numeric utime second count is a TypeError (numFloat rejects it).
53+
if got := runFSErr(t, `File.utime(:bad, 0, "`+f+`")`); got != "TypeError" {
54+
t.Errorf("utime(:bad): got %q want TypeError", got)
55+
}
5056
}
5157

5258
// TestFileMetaOpsErrors drives the chmod/chown/lchown/utime error branches via

0 commit comments

Comments
 (0)