Skip to content

Commit 0ae4ac8

Browse files
committed
Add: ts.el
1 parent 495735e commit 0ae4ac8

File tree

2 files changed

+220
-3
lines changed

2 files changed

+220
-3
lines changed

README.org

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,100 @@ Streams could be created from any sequential input data:
558558

559559
*** Libraries :libraries:
560560

561+
**** [[https://github.com/alphapapa/ts.el][ts.el: Timestamp and date-time library]]
562+
563+
=ts= aids in parsing, formatting, and manipulating timestamps.
564+
565+
#+BEGIN_QUOTE
566+
ts is a date and time library for Emacs. It aims to be more convenient than patterns like ~(string-to-number (format-time-string "%Y"))~ by providing easy accessors, like ~(ts-year (ts-now))~.
567+
568+
To improve performance (significantly), formatted date parts are computed lazily rather than when a timestamp object is instantiated, and the computed parts are then cached for later access without recomputing. Behind the scenes, this avoids unnecessary ~(string-to-number (format-time-string...~ calls, which are surprisingly expensive.
569+
#+END_QUOTE
570+
571+
***** Examples
572+
573+
Get parts of the current date:
574+
575+
#+BEGIN_SRC elisp
576+
;; When the current date is 2018-12-08 23:09:14 -0600:
577+
(ts-year (ts-now)) ;=> 2018
578+
(ts-month (ts-now)) ;=> 12
579+
(ts-day (ts-now)) ;=> 8
580+
(ts-hour (ts-now)) ;=> 23
581+
(ts-minute (ts-now)) ;=> 9
582+
(ts-second (ts-now)) ;=> 14
583+
(ts-tz-offset (ts-now)) ;=> "-0600"
584+
585+
(ts-dow (ts-now)) ;=> 6
586+
(ts-day-abbr (ts-now)) ;=> "Sat"
587+
(ts-day-name (ts-now)) ;=> "Saturday"
588+
589+
(ts-month-abbr (ts-now)) ;=> "Dec"
590+
(ts-month-name (ts-now)) ;=> "December"
591+
592+
(ts-tz-abbr (ts-now)) ;=> "CST"
593+
#+END_SRC
594+
595+
Increment the current date:
596+
597+
#+BEGIN_SRC elisp
598+
;; By 10 years:
599+
(list :now (ts-format)
600+
:future (ts-format (ts-adjust 'year 10 (ts-now))))
601+
;;=> ( :now "2018-12-15 22:00:34 -0600"
602+
;; :future "2028-12-15 22:00:34 -0600")
603+
604+
;; By 10 years, 2 months, 3 days, 5 hours, and 4 seconds:
605+
(list :now (ts-format)
606+
:future (ts-format
607+
(ts-adjust 'year 10 'month 2 'day 3
608+
'hour 5 'second 4
609+
(ts-now))))
610+
;;=> ( :now "2018-12-15 22:02:31 -0600"
611+
;; :future "2029-02-19 03:02:35 -0600")
612+
#+END_SRC
613+
614+
What day of the week was 2 days ago?
615+
616+
#+BEGIN_SRC elisp
617+
(ts-day-name (ts-dec 'day 2 (ts-now))) ;=> "Thursday"
618+
619+
;; Or, with threading macros:
620+
(thread-last (ts-now) (ts-dec 'day 2) ts-day-name) ;=> "Thursday"
621+
(->> (ts-now) (ts-dec 'day 2) ts-day-name) ;=> "Thursday"
622+
#+END_SRC
623+
624+
Get timestamp for this time last week:
625+
626+
#+BEGIN_SRC elisp
627+
(ts-unix (ts-adjust 'day -7 (ts-now)))
628+
;;=> 1543728398.0
629+
630+
;; To confirm that the difference really is 7 days:
631+
(/ (- (ts-unix (ts-now))
632+
(ts-unix (ts-adjust 'day -7 (ts-now))))
633+
86400)
634+
;;=> 7.000000567521762
635+
636+
;; Or human-friendly as a list:
637+
(ts-human-duration
638+
(ts-difference (ts-now)
639+
(ts-dec 'day 7 (ts-now))))
640+
;;=> (:years 0 :days 7 :hours 0 :minutes 0 :seconds 0)
641+
642+
;; Or as a string:
643+
(ts-human-format-duration
644+
(ts-difference (ts-now)
645+
(ts-dec 'day 7 (ts-now))))
646+
;;=> "7 days"
647+
648+
;; Or confirm by formatting:
649+
(list :now (ts-format)
650+
:last-week (ts-format (ts-dec 'day 7 (ts-now))))
651+
;;=> ( :now "2018-12-08 23:31:37 -0600"
652+
;; :last-week "2018-12-01 23:31:37 -0600")
653+
#+END_SRC
654+
561655
**** [[https://github.com/emacs-php/emacs-datetime][emacs-datetime]]
562656
:PROPERTIES:
563657
:ID: 45613ae9-97a2-4807-b099-a6c051f1a2c4

0 commit comments

Comments
 (0)