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

Merging ts.el into Emacs core #29

Open
2 tasks
yantar92 opened this issue Feb 19, 2024 · 13 comments
Open
2 tasks

Merging ts.el into Emacs core #29

yantar92 opened this issue Feb 19, 2024 · 13 comments

Comments

@yantar92
Copy link

Following up alphapapa/org-ql#409

@alphapapa listed the following reasons why ts.el may be beneficial compared to the existing time API:

One purpose of the ts struct is to provide convenient accessors for elements of the timestamp without having
to decode and format the time value repeatedly; whether that's an important enough benefit to keep may be
debatable, but I did measure a significant performance benefit when I was writing it (and whether user code
typically uses that pattern is, again, another matter).

The main purpose of ts is to provide a unified API. Emacs's default time/date functions are spread across
various libraries and symbol prefixes, and various functions take different arguments and types, which becomes
bewildering. The API's using a single data type is a significant benefit, as all of the functions can be
composed, chained, etc.

  • For performance, it would be nice to provide actual benchmarks with numbers that we can present to Emacs devs.
  • For "unified API", it would be nice to provide examples demonstrating how the new API is better.
@alphapapa
Copy link
Owner

Various benchmarks from earlier in ts's development may be seen in the notes file: https://github.com/alphapapa/ts.el/blob/master/notes.org

@yantar92
Copy link
Author

yantar92 commented Feb 20, 2024 via email

@alphapapa
Copy link
Owner

Note, of course, that decoded-time- accessors did not exist in Emacs prior to ts. Those were added after ts had been released and used in the wild for some time.

That said, new API functions introduced in ts.el might be of interest for upstream.

At this point, I would expect that the appeal would be the unified API for accessing parts of and manipulating timestamps.

@yantar92
Copy link
Author

yantar92 commented Feb 21, 2024 via email

@alphapapa
Copy link
Owner

I do not see how unified API would justify ts struct (there are already too many time representations in Emacs; adding yet another one will be questioned). It may be necessary to work with `decode-time'-returned struct instead.

I did not mean to propose upstreaming the struct, just the API.

Further, I suggest providing some kind of summary comparing the existing time API and what you change/add in ts.el. This will help to see which aspects need to be improved and show why a new set of functions is needed.

Of course, it would be unlikely to be accepted otherwise. However, this is one of the most time-consuming aspects of such a proposal, the "selling" and convincing via laborious examples, ones which seem obviously beneficial to those who need them, but will be repeatedly questioned by those who don't write related code and can't imagine the benefit.

Do you agree that having a unified API for working with time values, like the one in ts, is useful, and would be a good thing to have in Emacs? Or would I need to argue for this by myself?

As well, if a proposal were accepted upstream, would you be willing to help with writing the code and/or documentation?

Thanks.

@yantar92
Copy link
Author

yantar92 commented Feb 21, 2024 via email

@alphapapa
Copy link
Owner

Here's a very simple initial example of how the ts API is easier to use to manipulate a timestamp than the current Emacs native ones:

(let ((decoded (decode-time argh-time)))
  (cl-incf (decoded-time-day decoded) 365)
  (format-time-string "%c" (encode-time decoded)))
;; "Fri Feb 21 20:28:47 2025"

(ts-format "%c" (ts-adjust 'day 365 (ts-now)))
;; "Fri Feb 21 20:34:18 2025"

Also of note is the differing values: while I don't claim that ts's results are always necessarily "correct" for this kind of thing (as taking into account leap years, leap seconds, and etc. is non-trivial, and ts doesn't even try to do this itself), it's notable that the result of changing the decoded time day and then re-encoding it is off by about 6 minutes.

In fact, there seems to be some kind of weird overflow happening, because as I write this message and experiment with code that modifies the seconds slot instead of the day slot, I notice that the time in the result doesn't change, even though it's being evaluated a few minutes later:

(let ((decoded (decode-time argh-time)))
  (cl-incf (decoded-time-second decoded) (* 60 60 24 365))
  (format-time-string "%c" (encode-time decoded)))
;; "Fri Feb 21 20:28:47 2025"

So to get a more accurate result requires working on the time value in terms of seconds and internal time values, like:

(let ((time (current-time)))
  (setf time (time-add (seconds-to-time (* 60 60 24 365)) time))
  (format-time-string "%c" time))
;; "Fri Feb 21 20:39:52 2025"

Next to which ts seems rather appealing to me:

(ts-format "%c" (ts-adjust 'day 365 (ts-now)))
;; "Fri Feb 21 20:34:18 2025"

So the idea of refactoring ts to use time values instead of the ts struct, while preserving the API, seems somewhat appealing to me.

However, another matter to keep in mind is that, since ts uses Unix timestamps internally, it helps to deal with time-zone issues, as Unix timestamps are in UTC by definition; whereas Emacs's internal time values don't have a defined time zone, so users must either ensure they are converted to UTC or carry the associated time zone separately.

What do you think? Thanks.

@yantar92
Copy link
Author

yantar92 commented Feb 23, 2024 via email

@yantar92
Copy link
Author

yantar92 commented Mar 24, 2024 via email

@yantar92
Copy link
Author

Let me know if I can help anything to move things forward.

@alphapapa
Copy link
Owner

I appreciate that. The next few weeks will be especially busy for me, and I won't have much time for Emacs-related things. Anyway, your tentative list seems good to me.

I suppose a next step might be to propose such an API to emacs-devel--that is, without the internal things that ts does regarding Unix timestamps and caching of formatted parts. If that could be agreed upon, then the implementation would be pretty simple and straightforward.

@yantar92
Copy link
Author

yantar92 commented Aug 7, 2024

It has been a few months. Just bumping the thread :)

@alphapapa
Copy link
Owner

Thanks for the ping, Ihor. Haven't had much time lately for Emacs things. Maybe I can find some soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants