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

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

Date hacking


From: Michael J. Barillier
Subject: Date hacking
Date: Fri, 02 Aug 2002 12:58:28 -0600
User-agent: Gnus/5.090007 (Oort Gnus v0.07) Emacs/21.1 (i586-pc-linux-gnu)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Since I'm being so generous (*cough* *cough*), here's another hack I
just used to fix up a file full of dates.  Basically, I wanted to add
the day name to the start of all dates, e.g. change

  2002 August 02

to:

  Friday 2002 August 02

calendar.el didn't have a suitable set of functions for working with
dates in the above format, so I hacked up:

(require 'calendar)

(defun monthname-to-index (monthname)
  "Converts a month name to its numeric (1-based) index."
  (cdr (assoc monthname
              (calendar-make-alist calendar-month-name-array))))

(defconst *bw-preferred-date-fmt-regexp*
  "\\(\\(19\\|20\\)[0-9][0-9]\\) \\([^ ]+\\) \\([0-9]+\\)"
  "My preferred date format, e.g. 2002 August 02.")

(defun ymd->date (ymd)
  "Converts a date formatted as:
  \"YYYY Month DD\"
to a date, as used in most of the `calendar.el' functions, i.e. a
list `(m d y)'."
  (save-match-data
    (when (string-match *bw-preferred-date-fmt-regexp* ymd)
      (list (monthname-to-index (match-string 3 ymd))
            (string-to-number (match-string 4 ymd))
            (string-to-number (match-string 1 ymd))))))

(defun fix-dates-in-region (start end)
  "Prefixes dates in the region with the day name.  Should have a
closure parameter or use a closure var or something to allow other
fix-ups ... but this is a hack."
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region start end)
      (goto-char (point-min))
      (condition-case nil
          (while t
            (re-search-forward *bw-preferred-date-fmt-regexp*)
            (let ((date (ymd->date (match-string 0))))
              (replace-match (concat
                              (elt calendar-day-name-array
                                   (calendar-day-of-week date))
                              " "
                              (match-string 0))))
            (goto-char (match-end 0)))
        (error (message "That's all, folks."))))))

- -- 
Michael J. Barillier
(let ((n "blackwolf") (h "pcisys.net")) (concatenate 'string n "@" h))
GnuPG public key ID: 0x35E54973
Fingerprint: EDB9 4FBC 4D0B 070C C0E3 2EE4 D822 78CE 35E5 4973
Knowledge shared is power lost. -- Aleister Crowley
Got a job lead?  Email me--will code for food.  Resume at:
  <http://www.pcisys.net/~blackwolf/resume/>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9StY72CJ4zjXlSXMRAhdXAJ4jApliIvx6SSZudGGDZiLaiFHxJwCgkLDQ
RXs+LkTYtVyHAkBGYh383AY=
=5i+Z
-----END PGP SIGNATURE-----



reply via email to

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