Replace iso-to-ts -> parse-timestring, ts-adjust -> adjust-timestamp

And split-string with uiop:split-string
This commit is contained in:
contrapunctus 2022-04-04 01:10:35 +05:30
parent e4ff298ba4
commit a04447f745

View File

@ -397,8 +397,8 @@ Use =org-babel= (=org-babel-tangle= / =org-babel-tangle-file=), /not/ =literate-
(:use :cl :trivia)
(:import-from :uiop
:xdg-config-home :xdg-data-home
:strcat)
(:import-from :local-time :parse-timestring)
:strcat :split-string)
(:import-from :local-time :parse-timestring :now :adjust-timestamp)
(:export
;; customizable variables
:*user-configuration-file* :*user-data-file*
@ -480,9 +480,10 @@ TIME must be a string in the form \"HH:MM:SS\"
TIMESTAMP must be a time string in the ISO-8601 format.
Return value is a ts struct (see `ts.el')."
(let-match (((list h m s) (mapcar #'string-to-number
(split-string time ":"))))
(ts-apply :hour h :minute m :second s (iso-to-ts timestamp))))
(let-match (((list h m s)
(mapcar #'parse-integer (split-string time :separator ":"))))
(adjust-timestamp (parse-timestring timestamp)
(set :hour h) (set :minute m) (set :sec s))))
#+END_SRC
**** tests
@ -546,7 +547,7 @@ Return value is a ts struct (see `ts.el')."
"Return HASH-TABLE with PLIST added as the latest interval.
If REPLACE is non-nil, replace the last interval with PLIST."
(let* ((date (->> (getf plist :start)
(iso-to-ts )
(parse-timestring )
(ts-format "%F" )))
(events-today (gethash date hash-table)))
(--> (if replace (-drop-last 1 events-today) events-today)
@ -593,7 +594,7 @@ treated as though their time is 00:00:00."
(start (date-ts start))
(end (date-ts end)))
(maphash (lambda (key value)
(when (ts-in start end (iso-to-ts key))
(when (ts-in start end (parse-timestring key))
(puthash key value subset)))
hash-table)
subset))
@ -653,34 +654,6 @@ active backend.")
#+END_SRC
** Time functions
*** iso-to-ts :function:
#+BEGIN_SRC lisp
(defun iso-to-ts (timestamp)
"Convert TIMESTAMP to a TS struct. (see `ts.el')
TIMESTAMP must be an ISO-8601 timestamp, as handled by
`parse-timestring'."
(let-match (((list second minute hour day month year dow _dst utcoff)
(decode-time
(parse-timestring timestamp))))
(ts-update
(make-ts :hour hour :minute minute :second second
:day day :month month :year year
:dow dow :tz-offset utcoff))))
#+END_SRC
**** tests
#+BEGIN_SRC lisp :tangle chronometrist-tests.el :load test
(ert-deftest iso-to-ts ()
(should (ts= (iso-to-ts "2021-01-01")
(make-ts :year 2021 :month 1 :day 1
:hour 0 :minute 0 :second 0)))
(should (ts= (iso-to-ts "2021-01-01T01:01:01")
(make-ts :year 2021 :month 1 :day 1
:hour 1 :minute 1 :second 1))))
#+END_SRC
*** plists-to-durations :function:
#+BEGIN_SRC lisp
(defun plists-to-durations (plists)
@ -737,9 +710,9 @@ Return a list in the form
;; time-zone-spanning events
;; The time on which the first provided day starts (according to `*day-start-time*')
(let* ((stop-ts (iso-to-ts stop-time))
(let* ((stop-ts (parse-timestring stop-time))
(first-day-start (apply-time day-start-time start-time))
(next-day-start (ts-adjust 'hour 24 first-day-start)))
(next-day-start (adjust-timestamp first-day-start (offset :hour 24))))
;; Does the event stop time exceed the next day start time?
(when (ts< next-day-start stop-ts)
(let ((split-time (ts-format "%FT%T%z" next-day-start)))
@ -792,10 +765,10 @@ SECONDS must be a positive integer."
(defun interval (plist)
"Return the period of time covered by EVENT as a time value.
EVENT should be a plist (see `*user-data-file*')."
(let* ((start-ts (iso-to-ts (getf plist :start)))
(let* ((start-ts (parse-timestring (getf plist :start)))
(stop-iso (getf plist :stop))
;; Add a stop time if it does not exist.
(stop-ts (if stop-iso (iso-to-ts stop-iso) (ts-now))))
(stop-ts (if stop-iso (parse-timestring stop-iso) (now))))
(ts-diff stop-ts start-ts)))
#+END_SRC
@ -838,7 +811,7 @@ SECONDS is less than 60, return a blank string."
*** iso-to-date :function:
#+BEGIN_SRC lisp
(defun iso-to-date (timestamp)
(cl-first (split-string timestamp "T")))
(cl-first (split-string timestamp :separator "T")))
#+END_SRC
** Backends