gpt4 book ai didi

emacs - 如何向 `format-time-string` 添加格式说明符?

转载 作者:行者123 更新时间:2023-12-02 21:41:50 24 4
gpt4 key购买 nike

我有以下函数定义:

(defun nth (n)
(format
(concat
"%d"
(if (memq n '(11 12 13)) "th"
(let ((last-digit (% n 10)))
(case last-digit
(1 "st")
(2 "nd")
(3 "rd")
(otherwise "th"))))) n))

我希望能够在 format-time-string 中使用它.通常,我会查看函数的源代码,但这个函数是在 C 源代码中定义的。 (我认为这排除了将某些东西卡在上面的可能性,但我有待纠正。)

我如何添加另一个格式说明符(例如,%o)以将 nth 应用于适当的参数?

期望的用法:

(format-time-string "%A, %B %o, %T (%z)" (seconds-to-time 1250553600))

=> "Monday, August 17th, 20:00:00 (-0400)"

最佳答案

这是你想要做的。 Stefan 和 Drew 已经给出了一些重要的评论(不要覆盖 nth 并查看 emacs-lisp/advising 函数的信息文件)。

(defun ordinal (n)
"Special day of month format."
(format
(concat
"%d"
(if (memq n '(11 12 13)) "th"
(let ((last-digit (% n 10)))
(case last-digit
(1 "st")
(2 "nd")
(3 "rd")
(otherwise "th"))))) n))


(defadvice format-time-string (before ordinal activate)
"Add ordinal to %d."
(let ((day (nth 3 (decode-time (or time (current-time))))))
(setq format-string
(replace-regexp-in-string "%o"
(ordinal day)
format-string))))

注意事项:

  1. 我没有处理 UNIVERSAL 参数

  2. 当从 C 调用 format-time-string 时,hack 不起作用(您可以在手册中阅读)。

关于emacs - 如何向 `format-time-string` 添加格式说明符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20316754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com