在Emacs Lisp中插入整个月份的日期 [英] Insert whole month of dates in Emacs Lisp

查看:104
本文介绍了在Emacs Lisp中插入整个月份的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Emacs Lisp中进行一些编程,但是我并不完全确定如何处理某些事情。



我是尝试插入一整个月的日期,每一个日期都在新的行上,如下面的列表:



January



01/01/09星期一:



02/01/09星期二:



03/01/09 Wed :





我该怎么做?我已经找到如何格式化日期,但是我找不到如何循环在一定范围的日期(在这种情况下循环一整个月,并打印一个月的每一天的日期)。

有没有人得到一些指针,他们可以给我介绍如何开始?

解决方案

你想要的功能是'encode-time 'format-time-string '解码时间
对于正确的文档, Ch f function-name 或者将为您提供该函数的文档,或者可以在这里找到一般的elisp信息页面: Ch im elisp RET时间转换RET



这是代码片段:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ 3)
(第1天)
(时间(编码时间1 1 0日月2009)))
(while(=(nth 4(decode-time time))month)
(insert(format-time-string%D%a:\\\
time))
(setq day(1+ day))
(setq time(encode-time 1 1 0 day month 2009)))))

我找不到如何确定天数在一个给定的月份(当然,你可以硬编码,但是你必须处理闰年)。幸运的是,'编码时间为你添加所有的添加,所以如果你通过相当于2月31日,它将返回3月3日(假设28天)。


I'm doing a bit of programming here and there in Emacs Lisp, but I'm not entirely sure how to go about certain things.

I'm trying to insert a whole month of dates, each on a new line like the list below:

January

01/01/09 Mon:

02/01/09 Tue:

03/01/09 Wed:

etc

How would I go about doing that? I've found how to format dates, but I can't find how to loop over a certain range of dates (in this instance to loop round a whole month and print a date for each day in the month).

Has anyone got some pointers they could give me on how to get started?

解决方案

The functions you want are 'encode-time, 'format-time-string, and 'decode-time. For the proper documentation, either C-h f function-name or will give you the documentation for the function, or the general elisp info pages can be found here: C-h i m elisp RET m time conversion RET

Here's that snippet:

(defun my-insert-dates ()
  "insert a bunch of dates"
  (interactive)
  (let* ((month 3)
         (day 1)
         (time (encode-time 1 1 0 day month 2009)))
    (while (= (nth 4 (decode-time time)) month)
      (insert (format-time-string "%D %a:\n" time))
      (setq day (1+ day))
      (setq time (encode-time 1 1 0 day month 2009)))))

I couldn't find how to determine the number of days in a given month (sure, you could hard-code it, but then you've got to deal with leap years). Luckily, 'encode-time does all the addition for you, so if you pass it the equivalent of "February 31", it'll return "March 3" (assuming 28 days).

这篇关于在Emacs Lisp中插入整个月份的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆