guide.org 6.1 KB

Documentation

https://www.gnu.org/software/guile/manual/html_node/Time.html

General

This guide will make use of the SRFI-19 implementation in GNU Guile, which deals with time calculations. The approach is similar to that in other languages. There are time objects, time duration objects and date objects.

Prerequisites

(import (srfi srfi-19))

#

(define SECOND 1) (define MINUTE (* 60 SECOND)) (define HOUR (* 60 MINUTE))

(define org-timestamp-format "[~Y-~m-~d ~a ~H:~M]")

Objects

<>

(simple-format (current-output-port) "date: ~s\n" (current-date)) (simple-format (current-output-port) "date: ~s\n" (make-date 0 0 0 10 13 3 2021 0)) (simple-format (current-output-port) "time: ~s\n" (current-time time-utc)) (let* ([seconds 1] [minutes (* 60 seconds)] [hours (* 60 minutes)] [now (current-time time-utc)]) (simple-format (current-output-port) "duration: ~s\n" (time-difference (add-duration now (make-time time-duration 0 (* 1 hours))) now)))

date: #
date: #
time: #

Time format

For a list of format specifiers see Formatting Calendar Time.

<>

<>

<>

(define duration->time-utc (λ (duration) (make-time time-utc (time-nanosecond duration) (time-second duration))))

(define org-timestamp->time-utc (λ (timestamp-string) (let ([parsed-date (string->date timestamp-string org-timestamp-format)]) (date->time-utc parsed-date))))

(define org-time-duration (λ (org-timestamp-1 org-timestamp-2) (time-difference (org-timestamp->time-utc org-timestamp-2) (org-timestamp->time-utc org-timestamp-1))))

(define duration->org-timestamp (λ (duration) (time-utc->date (duration->time-utc duration) ;; timezone offset, assume all with no offset 0)))

(define duration->hours (λ (duration) (/ (time-second duration) HOUR)))

:results: :end:

<>

(number->string (duration->hours (org-time-duration org-timestamp-1 org-timestamp-2)))

:results: ice-9/boot-9.scm:1669:16: In procedure raise-exception: Wrong type to apply: #

Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. scheme@(guile-user) [1]> :end:

(import (srfi srfi-19))

(define SECOND 1) (define MINUTE (* 60 SECOND)) (define HOUR (* 60 MINUTE))

(define org-timestamp-format "[~Y-~m-~d ~a ~H:~M]")

(define duration->time-utc (λ (duration) (make-time time-utc (time-nanosecond duration) (time-second duration))))

(define org-timestamp->time-utc (λ (timestamp-string) (let ([parsed-date (string->date timestamp-string org-timestamp-format)]) (date->time-utc parsed-date))))

(define org-time-duration (λ (org-timestamp-1 org-timestamp-2) (time-difference (org-timestamp->time-utc org-timestamp-2) (org-timestamp->time-utc org-timestamp-1))))

(define duration->org-timestamp (λ (duration) (time-utc->date (duration->time-utc duration) ;; timezone offset, assume all with no offset 0)))

(define duration->hours (λ (duration) (/ (time-second duration) HOUR)))

(number->string (duration->hours (org-time-duration org-timestamp-1 org-timestamp-2)))

WIP Usage in org-mode

timestamp 1 timestamp 2 result
# [2021-03-14 Sun 03:50] [2021-03-14 Sun 03:55]
# [2021-03-14 Sun 03:50] [2021-03-14 Sun 03:50] nil
# [2021-03-14 Sun 03:50] [2021-03-14 Sun 03:50]

TODO Usage

<>

(simple-format (current-output-port) "formatted output: ~s" (my-org-duration-format (time-difference (current-time time-utc) (add-duration (current-time time-utc) (make-time time-duration 0 (* 1 hours))))))