ts.el icon indicating copy to clipboard operation
ts.el copied to clipboard

[Q/FR] Support for relative dates

Open Fuco1 opened this issue 6 years ago • 3 comments

Do you plan to support relative dates with a human readable instructions, like "last year" or "next month"... I would like to have something to parse these relative to today (or an argument passed in). But also it should understand input such as "2018" or "jan 2018" to give the specified intervals.

My current use-case is to make better reports for org-clock-budget where you could specify the intervals/periods with a simple query such as "last year" or "this week" and it will give you a report. This way users can view historical reports as well as the current ones.

It feels like it might be a separate package, "period" or something for returning the intervals specified by the query.

Edit: the inspiration comes from ledger https://github.com/ledger/ledger/blob/7c0ae5b02571e21f97d45f5d091cb78af9885713/src/times.cc and how it handles relative times. Maybe I will try to port it using ts as a backend.

Fuco1 avatar Feb 26 '19 16:02 Fuco1

I would like to, yes. The easiest solution would be to call out to GNU date, which has great natural language support, but it isn't available by default on Mac or BSD. I suppose we could port one of the nice Python libs, but that would be a lot of work. On Feb 26, 2019 10:28 AM, "Matus Goljer" [email protected] wrote:

Do you plan to support relative dates with a human readable instructions, like "last year" or "next month"... I would like to have something to parse these relative to today (or an argument passed in). But also it should understand input such as "2018" or "jan 2018" to give the specified intervals.

My current use-case is to make better reports for org-clock-budget where you could specify the intervals/periods with a simple query such as "last year" or "this week" and it will give you a report. This way users can view historical reports as well as the current ones.

It feels like it might be a separate package, "period" or something for returning the intervals specified by the query.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/alphapapa/ts.el/issues/4, or mute the thread https://github.com/notifications/unsubscribe-auth/AAktFdKnQeqjCPkw2sgpEsfGSBZEKV1Hks5vRWDBgaJpZM4bSlLu .

alphapapa avatar Mar 02 '19 03:03 alphapapa

@Fuco1 FYI, I just updated this package, fixing all warnings, adding tests, adding docs, and submitting to MELPA. Please let me know if you have any feedback.

alphapapa avatar Jul 28 '19 02:07 alphapapa

I think it may be possible to support this without implementing plain-language parsing. For example, the example added in https://github.com/alphapapa/ts.el/commit/e3e32ebd4531d4c9c84016c668223dcff5792838 shows a simple function that returns the timestamps at the beginning and end of the week containing a given timestamp. It would be easy to have similar functions for week-start, year-end, etc, and compose those into "last year," "last week," etc. Year start/end would be very easy with ts-apply, since the dates and times are always the same. Something like:

(defun ts-year-span (ts)
  (let* ((beg (ts-apply :month 1 :day 1 :hour 0 :minute 0 :second 0 ts))
         (end (ts-apply :month 12 :day 31 :hour 23 :minute 59 :second 59 ts)))
    (cons beg end)))

(-let (((beg . end) (ts-year-span (ts-now))))
  (cons (ts-format beg)
        (ts-format end)))
;;=> ("2019-01-01 00:00:00 -0600" . "2019-12-31 23:59:59 -0600")

alphapapa avatar Aug 08 '19 10:08 alphapapa