Skip to content

Commit

Permalink
Change: (ts-fill) Add ZONE argument
Browse files Browse the repository at this point in the history
Will help with testing ts-fill, see
<#18> (though this argument
should have been added originally).
  • Loading branch information
alphapapa committed Jul 5, 2021
1 parent ea2c26b commit d1a8ff2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
5 changes: 3 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ For a more interesting example, does a timestamp fall within the previous calend
+ ~copy-ts (TS)~ :: Return copy of timestamp struct ~TS~.
+ ~ts-difference (A B)~ :: Return difference in seconds between timestamps ~A~ and ~B~.
+ ~ts-diff~ :: Alias for ~ts-difference~.
+ ~ts-fill (TS)~ :: Return ~TS~ having filled all slots from its Unix timestamp. This is non-destructive.
+ ~ts-fill (TS &optional ZONE)~ :: Return ~TS~ having filled all slots from its Unix timestamp. This is non-destructive. ~ZONE~ is passed to ~format-time-string~, which see.
+ ~ts-now~ :: Return ~ts~ struct set to now.
+ ~ts-p (STRUCT)~ ::
+ ~ts-reset (TS)~ :: Return ~TS~ with all slots cleared except ~unix~. Non-destructive. The same as:
Expand All @@ -392,7 +392,8 @@ For a more interesting example, does a timestamp fall within the previous calend

** 0.3-pre

Nothing new yet.
*Added*
+ Function ~ts-fill~ accepts a ~ZONE~ argument, like that passed to ~format-time-string~, which see.

** 0.2

Expand Down
38 changes: 22 additions & 16 deletions test/test.el
Original file line number Diff line number Diff line change
Expand Up @@ -430,22 +430,28 @@
expected-difference))))

(ert-deftest ts-fill ()
(let ((ts (ts-fill (make-ts :unix 1625426636.7569551))))
(should (equal (ts-year ts) 2021))
(should (equal (ts-month-num ts) 7))
(should (equal (ts-day-of-month-num ts) 4))
(should (equal (ts-day-of-week-num ts) 0))
(should (equal (ts-hour ts) 14))
(should (equal (ts-minute ts) 23))
(should (equal (ts-second ts) 56))
(should (equal (ts-month-name ts) "July"))
(should (equal (ts-month-abbr ts) "Jul"))
(should (equal (ts-day-abbr ts) "Sun"))
(should (equal (ts-day-name ts) "Sunday"))
(should (equal (ts-day-of-year ts) 185))
(should (equal (ts-week-of-year ts) 26))
(should (equal (ts-tz-abbr ts) "CDT"))
(should (equal (ts-tz-offset ts) "-0500"))))
(let ((zones '("America/Chicago" (-18000 "CDT")))
ts)
(dolist (zone zones)
(setf ts (ts-fill (make-ts :unix 1625426636.7569551) zone))
(should (equal (ts-year ts) 2021))
(should (equal (ts-month-num ts) 7))
(should (equal (ts-day-of-month-num ts) 4))
(should (equal (ts-day-of-week-num ts) 0))
(should (equal (ts-hour ts) 14))
(should (equal (ts-minute ts) 23))
(should (equal (ts-second ts) 56))
(should (equal (ts-month-name ts) "July"))
(should (equal (ts-month-abbr ts) "Jul"))
(should (equal (ts-day-abbr ts) "Sun"))
(should (equal (ts-day-name ts) "Sunday"))
(should (equal (ts-day-of-year ts) 185))
(should (equal (ts-week-of-year ts) 26))
(should (equal (ts-tz-abbr ts) "CDT"))
(should (equal (ts-tz-offset ts) "-0500"))))
;; If ZONE is the plain integer -18000, the tz-abbr ("%z" to `format-time-string')
;; is "-05" rather than the zone abbreviation, so we test it separately.
(should (equal (ts-tz-abbr (ts-fill (make-ts :unix 1625426636.7569551) -18000)) "-05")))

(ert-deftest ts-now ()
"Ensure `ts-now' returns what appears to be the current time."
Expand Down
7 changes: 4 additions & 3 deletions ts.el
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,13 @@ to `make-ts'."
('integer `(string-to-number ,val))
(_ val))))))
;; MAYBE: Construct the record manually? Probably not worth it, but might eke out a bit more speed.
`(defun ts-fill (ts)
`(defun ts-fill (ts &optional zone)
"Return TS having filled all slots from its Unix timestamp.
This is non-destructive."
This is non-destructive. ZONE is passed to `format-time-string',
which see."
;; MAYBE: Use `decode-time' instead of `format-time-string'? It provides most of the values we need. Should benchmark.
(let ((time-values (save-match-data
(split-string (format-time-string ,format-string (ts-unix ts)) "\f"))))
(split-string (format-time-string ,format-string (ts-unix ts) zone) "\f"))))
(make-ts :unix (ts-unix ts) ,@value-conversions)))))
(ts-define-fill)

Expand Down

0 comments on commit d1a8ff2

Please sign in to comment.