【问题标题】:strtotime for Emacs LispEmacs Lisp 的 strtotime
【发布时间】:2009-09-22 11:07:20
【问题描述】:

Emacs Lisp 中是否有与PHP's strtotime function 类似的功能? (实际上 AFAIK 它实现了relative items of the GNU date input formats。)

我可以用 PHP 编写

echo strtotime("+2 months"); //1258891352
echo strtotime("-3 months +2 weeks"); //1246952239

返回相应的 UNIX 时间戳。

【问题讨论】:

    标签: emacs lisp elisp strtotime


    【解决方案1】:

    虽然不完全符合您的要求,但这可能有用。包org-mode 中有一个命令'org-schedule,它有一个非常好的界面来选择日期。执行此操作的具体命令是'org-read-date,它了解表示日期的多种方式,包括:

     +0            --> today
     .             --> today
     +4d           --> four days from today
     +4            --> same as above
     +2w           --> two weeks from today
     ++5           --> five days from default date
     +2tue         --> second Tuesday from now.
    

    如果您正在寻找一种交互式方式来使用方便的语法糖指定日期,那么该例程应该非常适合。如果您正在寻找程序化解决方案,看起来好像上面的命令调用'org-read-date-analyze 来完成腿部工作。注意:它的用法有点晦涩(其中两个参数从未定义...),因此如果您无法直接弄清楚,可能值得将邮件发送到org mailing list

    【讨论】:

    • 检查了 org-mode 的日期功能,但没有找到此功能。谢谢。
    • 函数 `org-read-date-analyze 不是命令,而是在 org.el 的第 13155 行附近定义(最新的 org.el:repo.or.cz/w/org-mode.git/blob/HEAD:/lisp/org.el
    • 我接受这个答案。它不涵盖与 strtotime 提供的完全相同的功能,但其目标相似并且是原生解决方案。
    【解决方案2】:
    (defun string-to-time (date)
      (shell-command-to-string (format "date --date='%s' +%%s" date)))
    

    【讨论】:

      【解决方案3】:

      我所知道的最接近的内置函数是编码时间。如果你想使用自然语言,你最终可能不得不写这样的东西:http://github.com/chaitanyagupta/chronicity

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-06
        • 2012-01-26
        • 1970-01-01
        相关资源
        最近更新 更多