1

My system's time zone is set to the local time zone, but I want the time zone for Emacs to be set to UTC.

I've tried using various different functions, like current-time-zone, current-time-string, current-time, format-time-string, etc, and I've tried setting various variables like display-time-format, but no matter what I do, the time will only display for my local timezone. ChatGPT suggested that I try:

(setq display-time-format "%a, %d %b %Y %H:%M:%S %Z")
(setq display-time-world-list '(("UTC" "0.0.0.0")))
(display-time-mode 1)

That didn't work, presumably because it only changes the time for the buffer resulting when world-clock is called. I did see time displayed for UTC when calling world-clock, but this didn't cause Emacs to display UTC time in the modeline. I asked ChatGPT again, and it suggested:

(format-time-string "%Y-%m-%d %H:%M:%S" (current-time-string 'universal))

But this didn't work either, since the minibuffer displayed "Invalid time specification".

Any suggestions? I can't think of anything to try, and I don't know how to use any time offsets to adjust the display time.

2
  • This question is more about the OS than about Emacs. As the answer points out, in Unix-like OSes (Linux and MacOS probably), setting the TZ environment variable for the program is enough. I don't know if that works on Windows. On Unix-like systems, you can set the timezone for any program by invoking the program like this at the shell prompt: TZ=UTC progname. Try e.g. TZ=UTC date and of course TZ=UTC emacs.
    – NickD
    Apr 9, 2023 at 3:14
  • BTW, this is a general mechanism: any env variable can be set that way for the invoked program. E.g. a common debugging technique is to have an env variable control verbose logging of the internals of an application, so that if you start it with DEBUG_VERBOSE=1 myapp you get all that output.
    – NickD
    Apr 9, 2023 at 3:29

1 Answer 1

1

Here's how to change the timezone:

(setenv "TZ" "UTC0")

See Time Zone Rules in the Emacs Lisp Reference manual.

5
  • 2
    Does this work on Windows?
    – NickD
    Apr 9, 2023 at 3:15
  • 1
    exactly as you said, the reference says "If TZ is not in the environment, Emacs uses system wall clock time, which is a platform-dependent default time zone."
    – roomworoof
    Apr 9, 2023 at 3:21
  • 2
    OK, thanks - it also says further down that All systems support the string ‘"UTC0"’ meaning Universal Time so that is definitive.
    – NickD
    Apr 9, 2023 at 3:34
  • While this does answer my question, that Elisp statement changes the displayed date and time for all my files in dired as well. As a result, two different dates are displayed for the same file in Emacs versus my GUI file manager, although the underlying information about when the files were created or modified is presumably kept consistent. Is there a way to only change the displayed time for the modeline to UTC, and nothing else? If not, maybe I'll get used to the changes displayed everywhere else.
    – Blender
    Apr 9, 2023 at 11:22
  • So it seems that you want to display a kind of world-clock in modeline, but I couldn't find a package that looks like it. Since the time.el that defines display-time uses Emacs-wide common date functions, I think it would require quite a bit of surgery to override display-time itself.
    – roomworoof
    Apr 9, 2023 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.