GNU bug report logs - #78678
[PATCH v1] world-clock: Sort entries based on a user-provided function

Previous Next

Package: emacs;

Reported by: "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>

Date: Tue, 3 Jun 2025 06:32:06 UTC

Severity: normal

Tags: patch

Done: Eli Zaretskii <eliz <at> gnu.org>

Full log


Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH v1] world-clock: Sort entries based on a user-provided function
Date: Tue, 03 Jun 2025 02:30:59 -0400
[Message part 1 (text/plain, inline)]
Tags: patch


Hello all,

Please find below a feature proposal for sorting entries of
world-clock, and a first draft of a patch attached to this message.

# Motivation

The `world-clock' displays time zone entries in the order they appear
in `world-clock-list'. Usually this is enough, since orders like time
can often be done statically. However, there are scenarios where you
might want a dynamic ordering. For example:

1. You work with people in multiple time zones, and you want entries
to appear in order of proximity to working hours, e.g. in what time
zones are people likely to be responsive right now?

2. Daylight savings can go into effect at [different times]. For
example, eastern Europe and Egypt both shift from UTC+2 in the winter
to UTC+3 in the summer.

| Zone           | DST start            | DST end                  |
|----------------+----------------------+--------------------------|
| eastern Europe | last Sunday of March | last Sunday of October   |
| Egypt          | last Friday of April | last Thursday of October |

For a period in the spring, local time in Egypt (UTC+2) is earlier
than eastern Europe (UTC+3). Depending on the year, DST in Egypt may
end earlier or later than in eastern Europe.

In years like 2025 & 2026 where the last Thursday of October is after
the last Sunday of October, there’s no static ordering of the zones
that works all year (bracket contents are UTC offsets at a vertical
time slice):

#+begin_src text
time             |< ~April             ~November >|
Egypt  ---(+2)-----(+2)-|-(+3)--------(+3)---(+3)-|-(+2)-----
Europe ---(+2)---|-(+3)---(+3)--------(+3)-|-(+2)---(+2)-----
#+end_src

In other years like 2027, a consistent ordering is possible:

#+begin_src text
time             |< ~April             ~November >|
Egypt  ---(+2)-----(+2)-|-(+3)--------(+3)-|-(+2)---(+2)-----
Europe ---(+2)---|-(+3)---(+3)--------(+3)---(+3)-|-(+2)-----
#+end_src

[different times]
https://en.wikipedia.org/wiki/Daylight_saving_time_by_country

# Possible Implementation

A simple way to do this is with a `world-clock-sort-function' custom
variable that sorts the entries in `world-clock-display'.

#+begin_src emacs-lisp
(setopt world-clock-sort-function 'my/world-clock-sort-function)
#+end_src

You could then accomplish each of the orderings mentioned earlier:

1. Time zones appear in order of how far they are from the middle of
the workday:

#+begin_src emacs-lisp
(defun my/world-clock-sort-function (tzlist)
  "Sort on proximity of local time to work hours."
  (let* ((now (current-time))
         (start 9)
         (end  17)
         (mid (/ (+ start end) 2.0)))
    (sort tzlist
          :key (lambda (entry)
                 (pcase-let* ((zone (car entry))
                              (`(,s ,m ,h _) (decode-time now zone))
                              (hour (+ h (/ m 60.0) (/ s 3600.0)))
                              (diff (min (abs (- hour (- mid 24)))
                                         (abs (- hour mid))
                                         (abs (- hour (+ mid 24))))))
                   diff)))))
#+end_src

2. Sort on an ISO-8601 timestamp so zones are always in ascending
order:

#+begin_src emacs-lisp
(defun my/world-clock-sort-function (tzlist)
  "Sort on local ISO-8601 timestamps."
  (let ((now (current-time)))
    (sort tzlist
          :key (lambda (entry)
                 (let ((zone (car entry)))
                   (format-time-string "%FT%T" now zone))))))
#+end_src

Note that in the current patch there’s a small race condition between
recording the current time in `world-clock-display' and sorting the
list.

Any thoughts on this idea and the interface?

Thanks,

-- 
Jacob S. Gordon
jacob.as.gordon <at> gmail.com

=========================

Please avoid sending me HTML emails and MS Office documents.
https://useplaintext.email/#etiquette
https://www.gnu.org/philosophy/no-word-attachments.html


In GNU Emacs 30.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.49,
cairo version 1.18.4)
System Description: Arch Linux

Configured using:
 'configure --with-pgtk --sysconfdir=/etc --prefix=/usr
 --libexecdir=/usr/lib --localstatedir=/var --disable-build-details
 --with-cairo --with-harfbuzz --with-libsystemd --with-modules
 --with-native-compilation=aot --with-tree-sitter 'CFLAGS=-march=x86-64
 -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=3
 -Wformat -Werror=format-security -fstack-clash-protection
 -fcf-protection -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -g
 -ffile-prefix-map=/build/emacs/src=/usr/src/debug/emacs -flto=auto'
 'LDFLAGS=-Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro
 -Wl,-z,now -Wl,-z,pack-relative-relocs -flto=auto'
 'CXXFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions
 -Wp,-D_FORTIFY_SOURCE=3 -Wformat -Werror=format-security
 -fstack-clash-protection -fcf-protection -fno-omit-frame-pointer
 -mno-omit-leaf-frame-pointer -Wp,-D_GLIBCXX_ASSERTIONS -g
 -ffile-prefix-map=/build/emacs/src=/usr/src/debug/emacs -flto=auto''

[v1-0001-world-clock-sort-entries-based-user-provided-function.patch (text/patch, attachment)]

This bug report was last modified 1 day ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.