help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: parsing time stamp


From: Kevin Rodgers
Subject: Re: parsing time stamp
Date: Thu, 22 May 2008 22:47:57 -0600
User-agent: Thunderbird 2.0.0.14 (Macintosh/20080421)

Kevin Rodgers wrote:
Johan Bockgård wrote:
Xah <xahlee@gmail.com> writes:

is there a function that parses a timestamp like “Monday, Nov. 28,
1994” so that i can write a function to turn it into the format yyyy-
mm-dd?

parse-time-string

(parse-time-string "Monday, Nov. 28, 1994")
=> (nil nil nil 28 11 1994 nil nil nil)

Odd that the leading "Monday, " yields nil DOW...  It turns out that is
due to the fact that the parse-time-weekdays variable only has
abbreviations (e.g. "mon"), just like parse-time-months.  Easy to fix:

(let ((parse-time-weekdays (append parse-time-weekdays
                   '(("sunday" . 0)
                     ("monday" . 1)
                     ("tuesday" . 2)
                     ("wednesday" . 3)
                     ("thursday" . 4)
                     ("friday" . 5)
                     ("saturday" . 6)))))
  (parse-time-string "Monday, Nov. 28, 1994"))
=> (nil nil nil 28 11 1994 1 nil nil)

That reminds me of a hack I put together a few years ago, to generate a
list of both day names and abbreviations, which are sensitive to the
locale:

  (let ((year (string-to-number (format-time-string "%Y")))
        (month (string-to-number (format-time-string "%m"))))
    (apply 'nconc
           (mapcar (lambda (time)
                     (list (format-time-string "%a." time)
                           (format-time-string "%A" time)))
                   (sort (mapcar (lambda (day)
                                   (encode-time 0 0 0 day month year 0))
                                 '(1 2 3 4 5 6 7))
                         ;; by day of week:
                         (lambda (time-1 time-2)
                           (< (nth 6 (decode-time time-1))
                              (nth 6 (decode-time time-2))))))))

Perhaps that could be incorporated into the initial value of
parse-time-weekdays to handle localized time strings.  There's also a
corresponding hack for month names and abbreviations (parse-time-months):

  (let ((year (string-to-number (format-time-string "%Y"))))
    (apply 'nconc
           (mapcar (lambda (month)
                     (let ((first (encode-time 0 0 0 1 month year 0)))
                       (list (format-time-string "%b." first t)
                             (format-time-string "%B" first t))))
                   '(1 2 3 4 5 6 7 8 9 10 11 12))))

--
Kevin Rodgers
Denver, Colorado, USA





reply via email to

[Prev in Thread] Current Thread [Next in Thread]