1 ;;; elmo-date.el -- Date processing module for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
37 (defmacro elmo-match-substring (pos string from)
38 "Substring of POSth matched string of STRING."
39 (` (substring (, string)
40 (+ (match-beginning (, pos)) (, from))
41 (match-end (, pos)))))
43 (defmacro elmo-match-string (pos string)
44 "Substring POSth matched STRING."
45 (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
47 (defmacro elmo-match-buffer (pos)
48 "Substring POSth matched from the current buffer."
49 (` (buffer-substring-no-properties
50 (match-beginning (, pos)) (match-end (, pos)))))
53 (defun elmo-replace-in-string (str regexp newtext &optional literal)
54 "Replace all matches in STR for REGEXP with NEWTEXT string.
55 And returns the new string.
56 Optional LITERAL non-nil means do a literal replacement.
57 Otherwise treat \\ in NEWTEXT string as special:
58 \\& means substitute original matched text,
59 \\N means substitute match for \(...\) number N,
60 \\\\ means insert one \\."
65 (while (setq match (string-match regexp str start))
66 (setq prev-start start
71 (substring str prev-start match)
72 (cond (literal newtext)
79 (cond ((eq c ?\\) "\\")
81 (elmo-match-string 0 str))
82 ((and (>= c ?0) (<= c ?9))
83 (if (> c (+ ?0 (length
86 (error "Invalid match num: %c" c)
88 (elmo-match-string c str)))
89 (t (char-to-string c))))
90 (if (eq c ?\\) (progn (setq special t) nil)
91 (char-to-string c)))))
93 (concat rtn-str (substring str start))))
95 (defvar elmo-date-descriptions
96 '((yesterday . [0 0 1])
99 (lastyear . [1 0 0])))
101 (defun elmo-date-get-description (datevec)
104 (car (rassq (aref datevec 1)
105 timezone-months-assoc))
108 (defun elmo-date-get-datevec (description)
110 ((not elmo-date-match)
111 (error "Date match is not available"))
112 ((string-match "^[ \t]*\\([0-9]+\\)?[ \t]*\\([a-zA-Z]+\\)$" description)
115 (timezone-fix-time (current-time-string) (current-time-zone)
119 (if (match-beginning 1)
120 (elmo-match-string 1 description)
122 (suffix (downcase (elmo-match-string 2 description)))
124 (if (setq pair (assq (intern suffix) elmo-date-descriptions))
125 (elmo-datevec-substitute today (cdr pair))
126 (if (string= "daysago" suffix)
127 (elmo-date-get-offset-datevec today number)
128 (error "%s is not supported yet" suffix)))))
129 ((string-match "[0-9]+-[A-Za-z]+-[0-9]+" description)
131 (concat (elmo-replace-in-string description "-" " ") " 0:00")
134 (defun elmo-datevec-substitute (datevec1 datevec2)
135 (if (/= (aref datevec2 2) 0)
136 (elmo-date-get-offset-datevec datevec1 (aref datevec2 2))
137 (let ((year (- (aref datevec1 0) (aref datevec2 0)))
138 (month (- (aref datevec1 1) (aref datevec2 1)))
139 (timezone (current-time-zone)))
144 (format "%d %s %d 0:00 %s"
146 (car (rassq month timezone-months-assoc))
148 (cadr timezone)) nil nil))))
150 (defun elmo-date-get-week (year month mday)
151 (let ((wday (symbol-value (intern (format
152 "elmo-weekday-name-%s"
156 (setq days (- (+ (* y1 365) (/ y1 400) (/ y1 4)) (/ y1 100)))
159 (setq days (+ days (timezone-last-day-of-month p year)))
162 (setq days (+ days mday))
163 (aref wday (% days 7))))
165 (defun elmo-date-get-offset-datevec (datevec offset &optional time)
166 (let ((year (aref datevec 0))
167 (month (aref datevec 1))
168 (day (aref datevec 2))
169 (hour (aref datevec 3))
170 (minute (aref datevec 4))
171 (second (aref datevec 5))
172 (timezone (aref datevec 6))
176 (setq day-number (- (timezone-day-number month day year)
178 (while (<= day-number 0)
180 day-number (+ (timezone-day-number 12 31 year)
182 (while (> day-number (setq day-of-month
183 (timezone-last-day-of-month p year)))
184 (setq day-number (- day-number day-of-month))
187 (setq day day-number)
189 (format "%d %s %d %s %s"
191 (car (rassq month timezone-months-assoc))
194 (format "%d:%d:%d" hour minute second)
196 (cadr timezone)) nil nil)))
198 (defmacro elmo-date-make-sortable-string (datevec)
199 "Make a sortable string from DATEVEC."
200 (` (timezone-make-sortable-date
204 (aref (, datevec) 3))))
207 (product-provide (provide 'elmo-date) (require 'elmo-version))
209 ;;; elmo-date.el ends here