From 10b73053d1cbef78fd325686e04f82f74d9d6e5c Mon Sep 17 00:00:00 2001 From: Jinser Kafka Date: Fri, 30 Jan 2026 17:24:08 +0800 Subject: [PATCH 01/15] feat(srfi-19): init --- goldfish/scheme/time.scm | 21 +- goldfish/srfi/srfi-19.scm | 560 +++++++++++++++++++++++++++ src/goldfish.hpp | 56 ++- tests/goldfish/liii/time-test.scm | 615 +++++++++++++++++++++++++++++- 4 files changed, 1235 insertions(+), 17 deletions(-) create mode 100644 goldfish/srfi/srfi-19.scm diff --git a/goldfish/scheme/time.scm b/goldfish/scheme/time.scm index 3630dfb9..8993c02e 100644 --- a/goldfish/scheme/time.scm +++ b/goldfish/scheme/time.scm @@ -15,15 +15,26 @@ ; (define-library (scheme time) - (export current-second current-jiffy jiffies-per-second) + (import (only (scheme base) let-values)) + (export current-second current-jiffy jiffies-per-second + get-time-of-day monotonic-nanosecond + system-clock-resolution steady-clock-resolution) (begin (define (jiffies-per-second) 1000000) - (define (current-second) (g_current-second)) + (define get-time-of-day g_get-time-of-day) + (define monotonic-nanosecond g_monotonic-nanosecond) + (define system-clock-resolution g_system-clock-resolution) + (define steady-clock-resolution g_steady-clock-resolution) + + (define (current-second) + (let-values (((sec usec) (get-time-of-day))) + (+ sec (exact->inexact (/ usec 1000000))))) (define (current-jiffy) - (round (* (current-second) (jiffies-per-second)))) + ;; NOTE: use the s7-round to ensure that a natural number is returned. + (s7-round (* (current-second) (jiffies-per-second)))))) - ) ; end of begin - ) ; end of define-library + ; end of begin + ; end of define-library diff --git a/goldfish/srfi/srfi-19.scm b/goldfish/srfi/srfi-19.scm new file mode 100644 index 00000000..38bec21b --- /dev/null +++ b/goldfish/srfi/srfi-19.scm @@ -0,0 +1,560 @@ +; +; Copyright (C) 2026 The Goldfish Scheme Authors +; +; Licensed under the Apache License, Version 2.0 (the "License"); +; you may not use this file except in compliance with the License. +; You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, software +; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +; License for the specific language governing permissions and limitations +; under the License. +; + +;; +;; SRFI-19 Implementation for Goldfish Scheme +;; +;; This is a heavily modified implementation of SRFI-19 "Time Data Types +;; and Procedures". While based on the original reference implementation, +;; nearly every function has been rewritten for performance, clarity, or +;; to adapt to Goldfish Scheme's idioms. +;; +;; ====================================================================== +;; SRFI-19: Time Data Types and Procedures. +;; +;; Copyright (C) I/NET, Inc. (2000, 2002, 2003). All Rights Reserved. +;; +;; Permission is hereby granted, free of charge, to any person obtaining +;; a copy of this software and associated documentation files (the +;; "Software"), to deal in the Software without restriction, including +;; without limitation the rights to use, copy, modify, merge, publish, +;; distribute, sublicense, and/or sell copies of the Software, and to +;; permit persons to whom the Software is furnished to do so, subject to +;; the following conditions: +;; +;; The above copyright notice and this permission notice shall be +;; included in all copies or substantial portions of the Software. +;; +;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +(define-library (srfi srfi-19) + (import (rename (scheme time) + (get-time-of-day glue:get-time-of-day) + (monotonic-nanosecond glue:monotonic-nanosecond)) + (only (srfi srfi-13) string-pad string-tokenize string-trim-right)) + (export + ;; Constants + TIME-DURATION TIME-MONOTONIC TIME-PROCESS + TIME-TAI TIME-THREAD TIME-UTC + ;; Time object and accessors + make-time time? + time-type time-nanosecond time-second + set-time-type! set-time-nanosecond! set-time-second! + copy-time + ;; Time comparison procedures + ;; Time arithmetic procedures + ;; Current time and clock resolution + current-date current-julian-day current-time time-resolution + ;; Date object and accessors + make-date date? + date-nanosecond date-second date-minute date-hour + date-day date-month date-year date-zone-offset + ;; Time/Date/Julian Day/Modified Julian Day Converters + ;; Date to String/String to Date Converters + date->string) + (begin + + ;; ==================== + ;; Constants + ;; ==================== + + (define TIME-DURATION 'time-duration) + (define TIME-MONOTONIC 'time-monotonic) + (define TIME-PROCESS 'time-process) + (define TIME-TAI 'time-tai) + (define TIME-THREAD 'time-thread) + (define TIME-UTC 'time-utc) + + (define priv:LOCALE-DECIMAL-POINT ".") + + (define priv:LOCALE-ABBR-WEEKDAY-VECTOR (vector "Sun" "Mon" "Tue" "Wed" + "Thu" "Fri" "Sat")) + (define priv:LOCALE-LONG-WEEKDAY-VECTOR (vector "Sunday" "Monday" + "Tuesday" "Wednesday" + "Thursday" "Friday" + "Saturday")) + ;; note empty string in 0th place. + (define priv:LOCALE-ABBR-MONTH-VECTOR (vector "" "Jan" "Feb" "Mar" + "Apr" "May" "Jun" "Jul" + "Aug" "Sep" "Oct" "Nov" + "Dec")) + (define priv:LOCALE-LONG-MONTH-VECTOR (vector "" "January" "February" + "March" "April" "May" + "June" "July" "August" + "September" "October" + "November" "December")) + + (define priv:LOCALE-PM "PM") + (define priv:LOCALE-AM "AM") + + ;; See `date->string` below + (define priv:LOCALE-DATE-TIME-FORMAT "~a ~b ~d ~H:~M:~S~z ~Y") + (define priv:LOCALE-SHORT-DATE-FORMAT "~m/~d/~y") + (define priv:LOCALE-TIME-FORMAT "~H:~M:~S") + (define priv:ISO-8601-DATE-TIME-FORMAT "~Y-~m-~dT~H:~M:~S~z") + + (define priv:NANO (expt 10 9)) + (define priv:SID 86400) ; seconds in a day + (define priv:SIHD 43200) ; seconds in a half day + (define priv:TAI-EPOCH-IN-JD 4881175/2) ; julian day number for 'the epoch' + + ;; ==================== + ;; Time object and accessors + ;; ==================== + + (define-record-type