lawlist
lawlist

Reputation: 13457

How to convert (day month year) to ( month day year)

My e-mails in Wanderlust have a header that looks like this:

Date:  Wed, 23 Oct 2013 12:18:15 -0700

I would like to modify the beginning of my print-to-pdf function so that it searches the current buffer for the first date it finds (usually the first line of the buffer) and converts it into a proposed pdf-file-name that looks like this:

10_23_2013.pdf

The beginning of my print-to-pdf function looks like this:

(defun print-to-pdf (pdf-file-name)
  "Print the current buffer to the given file."
  (interactive (list
    (ns-read-file-name "Write PDF file: " "/Users/HOME/.0.data/" nil ".pdf")))
  (cond (
    (not (equal pdf-file-name nil))

  ***

Can anyone think of a way to search for the date and turn it into a proposed pdf-file-name?


EDIT:  Here are some of the date string functions I found by grepping the Wanderlust code:

(defun wl-make-date-string ()
  (let ((system-time-locale "C"))
    (format-time-string "%a, %d %b %Y %T %z")))

(defsubst wl-get-date-iso8601 (date)
  (or (get-text-property 0 'wl-date date)
      (let* ((d1 (timezone-fix-time date nil nil))
         (time (format "%04d%02d%02dT%02d%02d%02d"
               (aref d1 0) (aref d1 1) (aref d1 2)
               (aref d1 3) (aref d1 4) (aref d1 5))))
    (put-text-property 0 1 'wl-date time date)
    time)))

(defun wl-make-date-string ()
  (let ((s (current-time-string)))
    (string-match "\\`\\([A-Z][a-z][a-z]\\) +[A-Z][a-z][a-z] +[0-9][0-9]? *[0-9][0-9]?:[0-9][0-9]:[0-9][0-9] *[0-9]?[0-9]?[0-9][0-9]"
          s)
    (concat (wl-match-string 1 s) ", "
        (timezone-make-date-arpa-standard s (current-time-zone)))))

(defun wl-date-iso8601 (date)
  "Convert the DATE to YYMMDDTHHMMSS."
  (condition-case ()
      (wl-get-date-iso8601 date)
    (error "")))

Upvotes: 1

Views: 241

Answers (1)

abo-abo
abo-abo

Reputation: 20352

Here's the function. If you can find a way to extract Wed, 23 Oct 2013 12:18:15 -0700, it will produce 10_23_2013.pdf.

(defun transform-date (s &optional shift)
  (let ((time (apply 'encode-time
                     (org-read-date-analyze
                      s nil
                      (decode-time (current-time))))))
    (when shift
      (setq time (time-add time (days-to-time shift))))
    (format-time-string "%m_%d_%Y.pdf" time)))

Here's a simple finder for the date:

(defun find-and-transform ()
  (goto-char (point-min))
  (when (re-search-forward "Date: \\([^:]*?\\)[0-9]+:")
    (transform-date
     (match-string-no-properties 1)))) 

Upvotes: 2

Related Questions