Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add weeks-in-year, extract leap-year-p, add two convenience functions #106

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/local-time.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -651,17 +651,28 @@ In other words:
(values (1+ month-minus-one)
(+ year year-offset))))

(defun leap-year-p (year)
"Returns T if the given year is a leap year."
(or (and (zerop (mod year 4))
(plusp (mod year 100)))
(zerop (mod year 400))))

(defun days-in-month (month year)
"Returns the number of days in the given month of the specified year."
(let ((normal-days (aref +rotated-month-days-without-leap-day+
(mod (+ month 9) 12))))
(if (and (= month 2)
(or (and (zerop (mod year 4))
(plusp (mod year 100)))
(zerop (mod year 400))))
(leap-year-p year))
(1+ normal-days) ; February on a leap year
normal-days)))

(defun weeks-in-year (year)
"Returns the number of weeks in the given year."
(if (or (= 4 (timestamp-day-of-week (encode-timestamp 0 0 0 0 1 1 year)))
(= 4 (timestamp-day-of-week (encode-timestamp 0 0 0 0 31 12 year))))
53
52))

;; TODO scan all uses of FIX-OVERFLOW-IN-DAYS and decide where it's ok to silently fix and where should be and error reported
(defun %fix-overflow-in-days (day month year)
"In case the day number is higher than the maximal possible for the given month/year pair, returns the last day of the month."
Expand Down Expand Up @@ -1167,6 +1178,14 @@ The value of this variable should have the methods `local-time::clock-now', and
"Returns a timestamp representing the present day."
(clock-today *clock*))

(defun yesterday ()
"Returns a timestamp representing the day before today."
(timestamp- (today) 1 :day))

(defun tomorrow ()
"Returns a timestamp representing the day after today."
(timestamp+ (today) 1 :day))

(defgeneric clock-now (clock)
(:documentation "Returns a timestamp for the current time given a clock."))

Expand Down
4 changes: 4 additions & 0 deletions src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#:adjust-timestamp
#:adjust-timestamp!
#:timestamp-whole-year-difference
#:leap-year-p
#:days-in-month
#:weeks-in-year
#:timestamp-
#:timestamp+
#:timestamp-difference
Expand Down Expand Up @@ -66,6 +68,8 @@
#:reread-timezone-repository
#:now
#:today
#:yesterday
#:tomorrow
#:enable-read-macros
#:+utc-zone+
#:+gmt-zone+
Expand Down