diff --git a/src/db-utils.lisp b/src/db-utils.lisp index e4687fa..16cc15b 100644 --- a/src/db-utils.lisp +++ b/src/db-utils.lisp @@ -273,9 +273,6 @@ nil (default T), start a new connection" (db:maybe-build-all-tables) (progn ,@body))) -(defun local-time-obj-now () - (local-time:now)) - ; db -> application (defun encode-datetime-string (d &optional (fallback nil)) "Encode a datetime string from db" diff --git a/src/db.lisp b/src/db.lisp index 390e406..ae5f396 100644 --- a/src/db.lisp +++ b/src/db.lisp @@ -874,7 +874,7 @@ to the corresponding id in table +table-account+" (let* ((last-inserted (last-in-history prompt))) (when (or (null last-inserted) (not (string= input (getf last-inserted :input)))) - (let* ((now (prepare-for-db (local-time-obj-now))) + (let* ((now (prepare-for-db (misc:local-time-obj-now))) (insert-query (make-insert :input-history (:prompt :input :date-added) (prompt input now)))) @@ -921,7 +921,7 @@ than `max-id'" (defun threshold-time (days-in-the-past) "Returns a time object `days-in-the-past' days in the past" - (local-time:adjust-timestamp (local-time-obj-now) + (local-time:adjust-timestamp (misc:local-time-obj-now) (offset :day (- (abs days-in-the-past))))) (defun purge-by-date-added (table threshold) @@ -1335,7 +1335,7 @@ than (swconf:config-purge-history-days-offset) days in the past" (assert status-id) (let ((actual-username (clean-chars username)) (actual-acct (clean-chars account-name)) - (now (prepare-for-db (local-time-obj-now)))) + (now (prepare-for-db (misc:local-time-obj-now)))) (when (not (mentioned-username->acct actual-username status-id :add-mention-prefix nil)) @@ -2816,7 +2816,7 @@ account that wrote the status identified by `status-id'" (:= :timeline timeline)))))) (defmacro with-db-current-timestamp ((timestamp) &body body) - `(let ((,timestamp (prepare-for-db (local-time-obj-now)))) + `(let ((,timestamp (prepare-for-db (misc:local-time-obj-now)))) ,@body)) (defun add-to-status-ignored (status-id folder timeline) @@ -3587,3 +3587,22 @@ Note: `status-id' must identify at least a row in the database." (defun strip-folder-prefix (maybe-folder) (strip-prefix +folder-tag-prefix+ maybe-folder)) + +(defun gemini-history-rows (days-from days-to prompt) + "Return past data included in ( days-from, days-to] +So if asking for data today and yesterday +(days-from = 1 and days-to = 0), will return the data from today only. +Rows are ordered from the most recent to the oldest." + (let ((actual-days-from (misc:modify-timestamp + (misc:modify-timestamp (misc:time-n-days-ago days-from) + :set :hour 23) + :set :minute 59))) + (let* ((from (prepare-for-db actual-days-from)) + (to (prepare-for-db (misc:time-n-days-ago days-to))) + (query (select :* + (from :input-history) + (where (:and (:<= :date-added to) + (:> :date-added from) + (:= :prompt prompt))) + (order-by (:desc :date-added))))) + (fetch-all (query query))))) diff --git a/src/gemini-viewer.lisp b/src/gemini-viewer.lisp index 72cfee8..115a56a 100644 --- a/src/gemini-viewer.lisp +++ b/src/gemini-viewer.lisp @@ -123,7 +123,7 @@ :initarg :download-iri :accessor download-iri) (start-time - :initform (db-utils:local-time-obj-now) + :initform (misc:local-time-obj-now) :initarg :start-time :accessor start-time) (download-stream @@ -263,7 +263,7 @@ (stream-status stream-status) (download-iri download-iri)) object (setf thread (make-thread function)) - (setf start-time (db-utils:local-time-obj-now)) + (setf start-time (misc:local-time-obj-now)) (setf download-iri (gemini-parser:make-gemini-iri host path :scheme scheme diff --git a/src/misc-utils.lisp b/src/misc-utils.lisp index b71e492..3fa1725 100644 --- a/src/misc-utils.lisp +++ b/src/misc-utils.lisp @@ -990,8 +990,11 @@ to the array" (truncate (max 0 (num:safe-parse-number year))))) +(defun local-time-obj-now () + (local-time:now)) + (defun current-year () - (local-time:timestamp-year (db-utils:local-time-obj-now))) + (local-time:timestamp-year (local-time-obj-now))) (defun extract-year-from-timestamp (ts) (local-time:timestamp-year ts)) @@ -1003,6 +1006,16 @@ to the array" (with-output-to-string (stream) (local-time:format-timestring stream local-time-object :format format-control-list))) +(defun time-n-days-ago (days-ago) + (let ((now (local-time-obj-now))) + (local-time:timestamp- now days-ago :day))) + +(defun modify-timestamp (object operation units new-value) + (assert (member operation '(:set :offset))) + (if (eq operation :set) + (local-time:adjust-timestamp! object (:set units new-value)) + (local-time:adjust-timestamp! object (:offset units new-value)))) + ;; threads (defmacro with-lock-held ((lock) &body body) diff --git a/src/package.lisp b/src/package.lisp index 4c3b421..fad48a3 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -265,6 +265,9 @@ :time-daylight-p-of :time-zone-of :year->timestamp + :local-time-obj-now + :time-n-days-ago + :modify-timestamp :current-year :extract-year-from-timestamp :format-time @@ -892,7 +895,6 @@ :count-all :query :query->sql - :local-time-obj-now :decode-date-string :decode-datetime-string :encode-datetime-string @@ -1218,7 +1220,8 @@ :get-parent-status-row :has-folder-prefix-p :add-folder-prefix - :strip-folder-prefix)) + :strip-folder-prefix + :gemini-history-rows)) (defpackage :date-formatter (:use