date functions added

This commit is contained in:
VaclavT 2021-05-20 23:03:03 +02:00
parent 75a6e186a6
commit 5dd336d382
3 changed files with 33 additions and 0 deletions

View File

@ -70,6 +70,16 @@
=> "2021-03-13 19:53:01"`|
|`(str-to-date string format)`|Converst string to time of secs since epoch. |`>>> (str-to-date "2021-03-13 19:53:01" "%Y-%m-%d %H:%M:%S") => 1615665181`|
|`(date-add date amount unit)`|Add number of units to date. A unit is one of 'year', 'month', 'day', 'hour', 'minute' or 'second'|`>>> (date-to-str (date-add (str-to-date "2021-03-13 19:53:01" "%Y-%m-%d %H:%M:%S") 10 "day") "%Y-%m-%d %H:%M:%S") => "2021-03-23 20:53:01"`|
|`start-of-day datetime`||`>>> (start-of-day (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1620864000`|
|`end-of-day datetime`||`>>> (end-of-day (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1620950399`|
|`start-of-month datetime`||`>>> (start-of-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1619827200`|
|`start-of-next-month datetime`||`>>> (end-of-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1622505599`|
|`end-of-next-month datetime`||`>>> (start-of-next-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1622505600`|
|`end-of-month datetime`||`>>> (end-of-next-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1625097599`|
|`start-of-prev-month datetime`||`>>> (start-of-prev-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1617235200`|
|`end-of-prev-month datetime`||`>>> (end-of-prev-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1619827199`|
|`start-of-year datetime`||`>>> (start-of-year (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1609459200`|
|`end-of-year datetime`||`>>> (end-of-year (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S")) => 1640995199`|
|`(debug ..)`|||
|`(display ..)`|||
|`(string-replace source substr replacement)`|Replace a substring with a replacement string in a source string|`>>> (string-replace "abcdefg" "de" "DE") => "abcDEfg"`|

View File

@ -135,6 +135,16 @@
(quick-sort-by l (lambda (a b) (> a b))))
(defun start-of-day (datetime) (str-to-date (+ (date-to-str datetime "%Y-%m-%d") " " "00:00:00") "%Y-%m-%d %H:%M:%S"))
(defun end-of-day (datetime) (str-to-date (+ (date-to-str datetime "%Y-%m-%d") " " "23:59:59") "%Y-%m-%d %H:%M:%S"))
(defun start-of-month (datetime) (str-to-date (+ (date-to-str datetime "%Y-%m") "-01 00:00:00") "%Y-%m-%d %H:%M:%S"))
(defun start-of-next-month (datetime) (date-add (start-of-month datetime) 1 "month"))
(defun end-of-next-month (datetime) (date-add (end-of-month datetime) 1 "month"))
(defun end-of-month (datetime) (date-add (date-add (start-of-month datetime) 1 "month") -1 "second"))
(defun start-of-prev-month (datetime) (date-add (start-of-month datetime) -1 "month"))
(defun end-of-prev-month (datetime) (date-add (end-of-month datetime) -1 "month"))
(defun start-of-year (datetime) (str-to-date (+ (date-to-str datetime "%Y") "-01-01 00:00:00") "%Y-%m-%d %H:%M:%S"))
(defun end-of-year (datetime) (str-to-date (+ (date-to-str datetime "%Y") "-12-31 23:59:59") "%Y-%m-%d %H:%M:%S"))
; from list of lists creates csv string

View File

@ -96,6 +96,19 @@
(ut::define-test "result of (!= nil nil)" '(ut::assert-false (!= nil nil)))
(ut::define-test "result of (start-of-day)" '(ut::assert-equal 1620864000 (start-of-day (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-day)" '(ut::assert-equal 1620950399 (end-of-day (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (start-of-month)" '(ut::assert-equal 1619827200 (start-of-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-month)" '(ut::assert-equal 1622505599 (end-of-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (start-of-next-month)" '(ut::assert-equal 1622505600 (start-of-next-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-next-month)" '(ut::assert-equal 1625097599 (end-of-next-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (start-of-prev-month)" '(ut::assert-equal 1617235200 (start-of-prev-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-prev-month)" '(ut::assert-equal 1619827199 (end-of-prev-month (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (start-of-year)" '(ut::assert-equal 1609459200 (start-of-year (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
(ut::define-test "result of (end-of-year)" '(ut::assert-equal 1640995199 (end-of-year (str-to-date "2021-05-13 10:32:12" "%Y-%m-%d %H:%M:%S"))))
;(ut::define-test "result of " '(ut::assert-true )
(ut::run-tests)