Skip to content

srfi 18: time->seconds does not round to nearest second, returns exact#1140

Merged
ashinn merged 1 commit into
ashinn:masterfrom
mxork:srfi-18-no-round-time-to-seconds
Jul 13, 2026
Merged

srfi 18: time->seconds does not round to nearest second, returns exact#1140
ashinn merged 1 commit into
ashinn:masterfrom
mxork:srfi-18-no-round-time-to-seconds

Conversation

@mxork

@mxork mxork commented Jul 10, 2026

Copy link
Copy Markdown

The implementation of time->seconds rounds the representation of the time? to the nearest second. The srfi seems pretty clear that this isn't the intended behavior:

(time->seconds time)                                  ;procedure
Converts the time object time into an exact or inexact real number representing the number of seconds elapsed since some implementation dependent reference point.

    (time->seconds (current-time))  ==>  955039784.928075   ;; <------

It also makes the srfi's interface unusable for timeouts <1s, which is goofy.

My change does the obvious thing. It uses an exact (rational) representation for microseconds, because I think that (seconds->time (time->seconds T)) should equal T. That said, using an inexact? to match current-second from (scheme time) seems tasteful as well.

@ashinn

ashinn commented Jul 13, 2026

Copy link
Copy Markdown
Owner

Thanks! Unless there is precedent for using exact rationals, I think it's better to use inexact here for better compatibility. Chibi can also be compiled without rational support.

@mxork mxork force-pushed the srfi-18-no-round-time-to-seconds branch from fc8bd30 to 8cdb2d1 Compare July 13, 2026 21:34
@mxork

mxork commented Jul 13, 2026

Copy link
Copy Markdown
Author

Inexact does seem like the right thing, doesn't it. Patched, and this output looks sensible:

(import (scheme small) (srfi 18))

(define now (car (current-time)))
(define rt (seconds->time (time->seconds now)))

(display now)
(newline)
(display rt)

; output:
; #<timeval 140171138348800>
; #<timeval 140171138352096>

@ashinn ashinn merged commit 05e4ba4 into ashinn:master Jul 13, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants