如何将月份的名称解码为整数 [英] How to decode name of month into an integer

查看:125
本文介绍了如何将月份的名称解码为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,让我可以选择(交互式地)选择 org-agenda-month-view 的月份和年份 - 它使用格式2013年12月的 201312 双引号)。我想使用这个月的任何一个词( 12月)或数字( 12 ),但需要一些帮助请将该月份的名称解码为一个整数,例如(if(not(integer-p month))(setq month(decode-name-of-month month))) code>。



另外,关于如何整合我的两个读取字符串的任何指针 let 绑定到一个动作将不胜感激 - 即能够手动输入 2014年1月 01 2014 ;而不是首先输入 January ,然后在两个单独的操作中输入 2014

 (let *(
(year(read-stringInsert the year:))
(month(read-string :)))
(org-Agenda nil1)
(org-agenda-month-view(read(format%s%02syear month)))






编辑(2013年12月11日)基于@Drew和@Sean Perry的有用答案的第一个工作草案。添加变量 org-agenda-start-day ,允许(org-Agenda nil1)显示[一开始]基于用户输入的正确月份 - 不再需要功能 org-agenda-month-view 。更新变量 lawlist-parse-time-months 来修正错字,并包含其他可能性 - 例如01,02等。




(01。01)
(01,01)

(jan。01)
(january。01)
(2,02)
(02 02)
(feb。02)
(february02)
(3,03)
(03 $ 03 $$$$$$$$$$$$$ 04)
(apr。04)
(april。04)
(5,05)
(05。05)
(may。05)
(6。06)
(06。
(j06)
(7。07)
(07。07)
(jul。07)
(july。07)
(808)
(08
(aug。08)
(august08)
(9,09)
(09 09)
(sep。09)
(9月09)
(1010)
1 0)
(十月。 10)
(11。11)
(nov。11)
(november11)
12)
(dec。12)
(十二月12)))

(defun encode-name- month(month_name)
(cdr(assoc(downcase month_name)lawlist-parse-time-months)))

(defun decode-name-of-month(month)
(car(rassoc month lawlist-parse-time-months)))

(defun foo(month year)
(interactive
(let *((mon + yr -string
(读字符串月和年:nil nil
(格式时间字符串%B%Y(当前时间)))
nil'TRIM))
(mon(car mon + yr))
(yr(cadr mon + yr))
(m2(condition-case nil(格式%dmon)(error nil)) )
(list(or m2 mon)yr)))
(setq org-agenda-start-day(concat year - (encode-name-of-month month) - 01))
(org-Agenda nil1))


解决方案

 (require'parse-time);;在标准lib 
(defun encode-name-of-month(month_name)
(cdr(assoc(downcase month_name)parse-time-months))

(defun decode -month-month(month)
(car(rassoc month parse-time-months))

这应该让你开始。


I'm trying to write a function that will let me choose (interactively) the month and year for org-agenda-month-view -- which uses a format of 201312 (without double-quotes) for December 2013 . I would like to use either words (December) or numbers (12) for the month, but need some assistance (please) to decode the name of the month into an integer -- something like (if (not (integer-p month)) (setq month (decode-name-of-month month))).

Also, any pointers on how to consolidate my two read-string  let bindings into just one action would be greatly appreciated -- i.e., be able to manually input January 2014 or 01 2014; instead of first inputting January and then inputting 2014 in two separate actions.

(let* (
    (year (read-string "Insert the year: "))
    (month (read-string "Insert the month: ")))
  (org-agenda nil "1")
  (org-agenda-month-view (read (format "%s%02s" year month))))


EDIT (December 11, 2013):  First working draft based upon the helpful answers by @Drew and @Sean Perry. Added variable org-agenda-start-day, which permits (org-agenda nil "1") to display [at the outset] the correct month based upon the user input -- the function org-agenda-month-view is no longer needed. Updated variable lawlist-parse-time-months to fix a typo and include additional possibilities -- e.g., 01, 02, etc.

(defvar lawlist-parse-time-months '(
  ("1" . "01")
  ("01" . "01")
  ("jan" . "01")
  ("january" . "01")
  ("2" . "02")
  ("02" . "02")
  ("feb" . "02")
  ("february". "02")
  ("3" . "03")
  ("03" . "03")
  ("mar" . "03")
  ("march" . "03")
  ("4" . "04")
  ("04" . "04")
  ("apr" . "04")
  ("april" . "04")
  ("5" . "05")
  ("05" . "05")
  ("may" . "05")
  ("6" . "06")
  ("06" . "06")
  ("jun" . "06")
  ("june" . "06")
  ("7" . "07")
  ("07" . "07")
  ("jul" . "07")
  ("july" . "07")
  ("8" . "08")
  ("08" . "08")
  ("aug" . "08")
  ("august" . "08")
  ("9" . "09")
  ("09" . "09")
  ("sep" . "09")
  ("september" . "09")
  ("10" . "10")
  ("oct" . "10")
  ("october" . "10")
  ("11" . "11")
  ("nov" . "11")
  ("november" . "11")
  ("12" . "12")
  ("dec" . "12")
  ("december" . "12")))

(defun encode-name-of-month (month_name)
  (cdr (assoc (downcase month_name) lawlist-parse-time-months)) )

(defun decode-name-of-month (month)
  (car (rassoc month lawlist-parse-time-months)))

(defun foo (month year)
  (interactive
   (let* ((mon+yr  (split-string
                    (read-string "Month and year: " nil nil
                                 (format-time-string "%B %Y"(current-time)))
                    nil 'TRIM))
          (mon     (car mon+yr))
          (yr      (cadr mon+yr))
          (m2      (condition-case nil (format "%d" mon) (error nil))))
     (list (or m2 mon)  yr)))
  (setq org-agenda-start-day (concat year "-" (encode-name-of-month month) "-" "01"))
  (org-agenda nil "1") )

解决方案

(require 'parse-time) ;; in standard lib
(defun encode-name-of-month (month_name)
  (cdr (assoc (downcase month_name) parse-time-months))
 )
(defun decode-name-of-month (month)
  (car (rassoc month parse-time-months))
 )

That should get you started.

这篇关于如何将月份的名称解码为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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