[GoLUG] Scheme -- guile3, may all your parentheses be in the correct order

Barry Fishman barry at ecubist.org
Sun Aug 18 11:22:02 EDT 2024


On 2024-08-18 04:30:00 GMT, David Billsbrough wrote:
> I downloaded an Ada compiler to check it out because I didn't have the
> time to be so confused by Rust.  :-)
> After seeing the Ada compiler (GNU gnat) and development tools are
> huge and the docs are non-ending
> I thought I might try checking out something a little 'smaller' like a
> 'lisp' or a 'scheme' next.  They are older
> languages so they might be easier.  Right?
>
> So ... after about three days at burning the midnight 'oil' I have a
> few small hello world and a sort of a port
> of a simple date counting utility ...  <g>
>
>   https://github.com/kc4zvw/daystogo.scm/tree/main
>
>   https://tinyurl.com/guile3-date-calc   (not really complete)
>
> Now how do you date calculations in in Scheme or even some 'racket?'

Unfortunately Guile scheme support for dates/time is one of its weakest
areas.

Scheme's common time handling is via the SRFI-19 package, which you can
set up in guile with "(use-modules (srfi srfi-19))", but replaces the
(current-time) function with it's own incompatable one.

But instead, using the default Guile's setup, if you want to deal with
day intervals, the easiest path would be to convert the Unix seconds
value from (current-time) to day numbers:

(define (as-days secs)
  (truncate (/ secs 86400)))

The dates in your .calendar file "YYYY/MM/DD" can be converted to the
current time format seconds with:

(define (date-to-secs str)
  (car (mktime (car (strptime "%Y/%m/%d" str))))

and then made into days with the "as-days" function above.

Then the calculation is just the difference between the days values.

Guile's handling of timezone info is somewhat broken, so when I worked
with Unix time seconds values from the internet (like retrieving github
timestamps) sometimes I had to adjust the times in the above functions
seem to match those I got with the Unix "date" command.


Also to get at you ~/.calendar file name, you can do something simple
like:

(define calendar-file
  (string-append (or (getenv "HOME") "/home/kc5zvw") "/.calendar"))

and avoid the any string-copy or dealing with a improbable missing
"HOME" environment variable. In general, you don't need to do
any string-copy or make-string, unless you intend to use functions
that end in "!" to modify the string, like string-set! or
substring-fill!.

-- 
Barry Fishman



More information about the GoLUG mailing list