【问题标题】:How do I get the number of days in the month specified by an elisp time?如何获取 elisp 时间指定的月份中的天数?
【发布时间】:2009-08-17 06:38:39
【问题描述】:

在 elisp 中,我有一个时间(三个整数的形式),我可以使用 decode-time 获得月份。我想得到的是那个月(和年)的天数,使用 elisp 函数(而不是我自己写的)。

即:

(defun days-in-month-at-time(t)
    ; Figure out month and year with decode-time
    ; return number of days in that month   
)

【问题讨论】:

    标签: emacs date elisp


    【解决方案1】:
    (require 'timezone)
    
    (defun days-in-month-at-time (time)
      "Return number of days in month at TIME."
      (let ((datetime (decode-time time)))
        (timezone-last-day-of-month (nth 4 datetime) (nth 5 datetime))))
    

    【讨论】:

    • 另见相关的 SO 问题:stackoverflow.com/questions/753248/…
    • timezone-last-day-of-month 在我的工作机器上不存在 emacs 23.1.1 的 Windows 版本 我想知道它是被删除还是在那之后添加的?
    • 啊,我明白了,您需要加载或需要日历(或时区),因此存在明显差异。
    • @justinhj:你说得对,calendartimezone 默认都没有加载,所以你可能需要先 require 包。
    【解决方案2】:

    看起来这样更好,因为时区似乎不是所有最近的 emacs 版本 编辑:抱歉,您只需要根据您是使用此答案还是其他答案来要求时区或日历。

    (defun days-in-month-at-time (time)
      "Return number of days in month at TIME."
      (let ((datetime (decode-time time)))
        (calendar-last-day-of-month (nth 4 datetime) (nth 5 datetime))))
    

    【讨论】:

    • 这在 Mac 上的 emacs 22.1.1 中对我不起作用:(days-in-month-at-time (current-time))
    • 我认为您首先需要(需要'日历)。
    【解决方案3】:

    我想出了一个不需要timezonecalendar 的解决方案,方法是从该月的最后一天提取日期:

    (defun days-in-month-at-time(tm)
      (let ((d (decoded-time-add (decode-time tm) (make-decoded-time :month 1))))
        (setf (decoded-time-day d) 1)
        (decoded-time-day (decoded-time-add d (make-decoded-time :day -1))))
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 2010-11-14
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      相关资源
      最近更新 更多