Scheme date and time
last modified November 25, 2026 Guile 3.0
Guile offers two layers for dates and times. The core procedures follow the
POSIX C library: the time is a count of seconds, a broken-down time is a
vector of fields, and strftime and strptime
convert between times and text. SRFI 19 adds a portable library with date
and time objects and arithmetic on them. Several examples print the current
date or time, or measure how long something takes, so their output differs
from run to run. The examples are run with GNU Guile 3.0.7 on a system in
the Central European time zone.
The current time
current-time returns the number of seconds since the Unix
epoch, January 1, 1970, 00:00 UTC. gettimeofday adds the
microseconds.
(define seconds (current-time)) (display seconds) (newline) (define now (gettimeofday)) (display now) (newline) (display (car now)) (display " s ") (display (cdr now)) (display " us") (newline) (display (quotient seconds 86400)) (display " days since 1970-01-01") (newline)
gettimeofday returns a pair of seconds and microseconds,
read with car and cdr. A day has 86,400 seconds,
so integer division gives the number of days since the epoch. The count
ignores leap seconds, as POSIX time does.
$ guile now.scm 1795627213 (1795627213 . 451432) 1795627213 s 451432 us 20782 days since 1970-01-01
Broken-down time
localtime converts seconds to a broken-down time in the local
time zone, and gmtime to one in UTC. The result is a vector
whose fields are read with the tm: procedures.
(define now (localtime (current-time)))
(display now)
(newline)
(format #t "year: ~a~%" (+ 1900 (tm:year now)))
(format #t "month: ~a~%" (+ 1 (tm:mon now)))
(format #t "day: ~a~%" (tm:mday now))
(format #t "time: ~a:~a:~a~%" (tm:hour now) (tm:min now) (tm:sec now))
(format #t "weekday: ~a~%" (tm:wday now))
(format #t "yearday: ~a~%" (+ 1 (tm:yday now)))
(format #t "zone: ~a~%" (tm:zone now))
(define epoch (gmtime 0))
(format #t "epoch: ~a-~a-~a, weekday ~a~%"
(+ 1900 (tm:year epoch)) (+ 1 (tm:mon epoch)) (tm:mday epoch)
(tm:wday epoch))
The fields follow the C struct tm: the year counts from 1900,
the month from 0, and the day of the year from 0, so the program adds the
offsets. The weekday is 0 for Sunday; January 1, 1970 was a Thursday. The
vector also holds the daylight saving flag, the offset from UTC in seconds
(west is positive, so CET is -3600), and the zone name.
$ guile broken-down.scm #(13 20 18 25 10 126 3 328 0 -3600 CET) year: 2026 month: 11 day: 25 time: 18:20:13 weekday: 3 yearday: 329 zone: CET epoch: 1970-1-1, weekday 4
Formatting with strftime
strftime formats a broken-down time with the directives of the
C library. localtime takes an optional time zone, so one
moment can be shown in several zones.
(define moment 1000000000)
(define utc (gmtime moment))
(display (strftime "%Y-%m-%d %H:%M:%S" utc))
(newline)
(display (strftime "%A, %d %B %Y" utc))
(newline)
(display (strftime "%a %b %e %I:%M %p, day %j, week %V" utc))
(newline)
(for-each
(lambda (zone)
(display (strftime "%Y-%m-%d %H:%M %Z %z" (localtime moment zone)))
(newline))
'("Europe/Bratislava" "America/New_York" "Asia/Tokyo"))
(display (strftime "%c" (localtime (current-time))))
(newline)
The fixed moment 1,000,000,000 seconds fell on September 9, 2001.
%Y, %m, and %d are the numeric date,
%A and %B the names of the weekday and the month,
%e the day padded with a space, %I and
%p the 12-hour clock, %j the day of the year, and
%V the ISO week. %Z and %z give the
zone name and offset; the zone names are those of the system time zone
database. %c is the preferred format of the locale. Only the
last line depends on the current time.
$ guile formatting.scm 2001-09-09 01:46:40 Sunday, 09 September 2001 Sun Sep 9 01:46 AM, day 252, week 36 2001-09-09 03:46 CEST +0200 2001-09-08 21:46 EDT -0400 2001-09-09 10:46 JST +0900 Wed 25 Nov 2026 06:20:13 PM CET
Parsing dates
strptime is the reverse of strftime: it reads a
date from a string according to a format. mktime turns the
broken-down time back into seconds.
(define parsed (strptime "%Y-%m-%d %H:%M" "2026-03-14 09:30"))
(display parsed)
(newline)
(define tm (car parsed))
(format #t "~a.~a.~a~%" (tm:mday tm) (+ 1 (tm:mon tm)) (+ 1900 (tm:year tm)))
(define (parse-utc fmt text)
(car (mktime (car (strptime fmt text)) "UTC")))
(define start (parse-utc "%Y-%m-%d %H:%M" "2026-03-14 09:30"))
(define end (parse-utc "%d.%m.%Y %H:%M" "16.03.2026 17:45"))
(display start)
(newline)
(define elapsed (- end start))
(format #t "~a h ~a min~%" (quotient elapsed 3600)
(quotient (remainder elapsed 3600) 60))
(define weekday (car (mktime (car (strptime "%Y-%m-%d" "2026-12-24")) "UTC")))
(display (strftime "%A" (gmtime weekday)))
(newline)
strptime returns a pair: the broken-down time and the number of
characters it read. The parsed time has no zone, so mktime is
told to read it as UTC; it returns a pair whose car is the
number of seconds. With both dates in seconds, the difference is a plain
subtraction. The last part finds the weekday of a date by formatting the
parsed time again.
$ guile parsing.scm (#(0 30 9 14 2 126 6 72 -1 0 #f) . 16) 14.3.2026 1773480600 56 h 15 min Thursday
Measuring elapsed time
current-time counts whole seconds, which is too coarse for
timing code. get-internal-real-time returns wall-clock time
and get-internal-run-time processor time, both in internal
units.
(use-modules (ice-9 format))
(define (fib n)
(if (< n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
(define (seconds-since start)
(exact->inexact
(/ (- (get-internal-real-time) start)
internal-time-units-per-second)))
(display internal-time-units-per-second)
(newline)
(define start (get-internal-real-time))
(define cpu-start (get-internal-run-time))
(display (fib 30))
(newline)
(format #t "wall: ~,3f s~%" (seconds-since start))
(usleep 200000)
(format #t "wall: ~,3f s~%" (seconds-since start))
(format #t "cpu: ~,3f s~%"
(exact->inexact (/ (- (get-internal-run-time) cpu-start)
internal-time-units-per-second)))
internal-time-units-per-second is a billion in Guile 3.0.7, so
the internal units are nanoseconds. The program times the recursive
Fibonacci function, then sleeps for 200 milliseconds with
usleep. The sleep adds wall-clock time but almost no
processor time. The ~,3f directive of
number formatting prints three
decimals. The measured times differ from run to run.
$ guile elapsed.scm 1000000000 832040 wall: 0.012 s wall: 0.212 s cpu: 0.012 s
SRFI 19 dates
SRFI 19 represents a date as an object with named fields and its own
formatting language, in which the directives start with a tilde.
make-date takes the nanosecond, second, minute, hour, day,
month, year, and the zone offset in seconds east of UTC.
(use-modules (srfi srfi-19))
(define today (current-date))
(display (date->string today "~Y-~m-~d ~H:~M:~S ~z"))
(newline)
(define launch (make-date 0 30 15 10 14 3 2026 0))
(display (date->string launch))
(newline)
(display (date->string launch "~A, ~e ~B ~Y, ~H:~M"))
(newline)
(display (date->string launch "~4"))
(newline)
(format #t "~a-~a-~a, day ~a of the year, weekday ~a~%"
(date-year launch) (date-month launch) (date-day launch)
(date-year-day launch) (date-week-day launch))
(define meeting (string->date "24.12.2026 18:00 +0100" "~d.~m.~Y ~H:~M ~z"))
(display (date->string meeting "~5 ~z"))
(newline)
(display (date->string (time-utc->date (date->time-utc meeting) 0) "~5 ~z"))
(newline)
current-date returns the date in the local zone. The launch
date is in UTC, since its offset is 0. date->string without a
format uses ~c; ~4 is the ISO 8601 date and time
with the zone, and ~5 the same without it. Unlike the C
library, SRFI 19 counts months from 1 and gives the full year.
date-week-day is 0 for Sunday. string->date
parses a date with an offset, and converting it to a time and back with
offset 0 shows the same moment in UTC.
$ guile srfi19.scm 2026-11-25 18:20:13 +0100 Sat Mar 14 10:15:30Z 2026 Saturday, 14 March 2026, 10:15 2026-03-14T10:15:30Z 2026-3-14, day 73 of the year, weekday 6 2026-12-24T18:00:00 +0100 2026-12-24T17:00:00 Z
Date arithmetic
SRFI 19 calculates with time objects. A date becomes a UTC time with
date->time-utc, two times give a duration with
time-difference, and add-duration moves a time
by a duration.
(use-modules (srfi srfi-19))
(define (utc-date year month day)
(make-date 0 0 0 0 day month year 0))
(define (days-between a b)
(let ((diff (time-difference (date->time-utc b) (date->time-utc a))))
(quotient (time-second diff) 86400)))
(define (add-days date n)
(time-utc->date
(add-duration (date->time-utc date)
(make-time time-duration 0 (* n 86400)))
0))
(define (show date)
(date->string date "~Y-~m-~d ~a"))
(define start (utc-date 2026 3 14))
(define christmas (utc-date 2026 12 24))
(format #t "~a days until ~a~%" (days-between start christmas) (show christmas))
(format #t "+100 days: ~a~%" (show (add-days start 100)))
(format #t "-100 days: ~a~%" (show (add-days start -100)))
(format #t "2028-02-28 + 1: ~a~%" (show (add-days (utc-date 2028 2 28) 1)))
(define diff (time-difference (date->time-utc christmas)
(date->time-utc start)))
(display diff)
(newline)
(display (time<? (date->time-utc start) (date->time-utc christmas)))
(newline)
days-between divides the seconds of the duration by the
length of a day. add-days builds a duration of whole days,
which may be negative, and turns the result back into a UTC date. The
dates are at midnight UTC, so no daylight saving change shifts them by an
hour. Adding a day to February 28, 2028 gives the leap day. A time object
prints with its type, nanoseconds, and seconds, and time<?
compares two times.
$ guile arithmetic.scm 285 days until 2026-12-24 Thu +100 days: 2026-06-22 Mon -100 days: 2025-12-04 Thu 2028-02-28 + 1: 2028-02-29 Tue #<time type: time-duration nanosecond: 0 second: 24624000> #t
A month calendar
The last example prints a calendar for a month, with weeks starting on Monday. It needs the number of days in the month and the weekday of the first day.
(use-modules (srfi srfi-19) (ice-9 format))
(define (leap-year? year)
(and (zero? (remainder year 4))
(or (not (zero? (remainder year 100)))
(zero? (remainder year 400)))))
(define (days-in-month year month)
(case month
((2) (if (leap-year? year) 29 28))
((4 6 9 11) 30)
(else 31)))
(define (first-of-month year month)
(make-date 0 0 0 0 1 month year 0))
(define (monday-offset year month)
(modulo (- (date-week-day (first-of-month year month)) 1) 7))
(define (print-calendar year month)
(display (date->string (first-of-month year month) " ~B ~Y"))
(newline)
(display " Mo Tu We Th Fr Sa Su")
(newline)
(let ((offset (monday-offset year month))
(last (days-in-month year month)))
(display (make-string (* 3 offset) #\space))
(do ((day 1 (+ day 1)))
((> day last))
(format #t "~3d" day)
(when (or (zero? (modulo (+ offset day) 7)) (= day last))
(newline)))
(newline)))
(print-calendar 2026 11)
(print-calendar 2028 2)
(format #t "leap years: ~a~%"
(filter leap-year? '(1900 2000 2024 2026 2028 2100)))
leap-year? applies the Gregorian rule: years divisible by 4
are leap years, except centuries not divisible by 400.
monday-offset turns the SRFI 19 weekday, where Sunday is 0,
into the number of empty columns before the first day. The
do loop prints each day in a three-character column with
~3d and ends the line after Sunday and after the last day.
$ guile calendar.scm
November 2026
Mo Tu We Th Fr Sa Su
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
February 2028
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
leap years: (2000 2024 2028)
Source
Time — GNU Guile Reference Manual
SRFI-19 — GNU Guile Reference Manual
In this article we have read the current time in seconds and
microseconds, split it into broken-down local and UTC time, formatted it
with strftime in several time zones, parsed dates with
strptime, measured elapsed and processor time, used SRFI 19
dates, added days and counted the days between dates, and printed a month
calendar. The number formatting
article covers the format directives, and the
environment and processes article
reads and sets environment variables, among them TZ, which
selects the local time zone.
Author
List all Scheme tutorials.