ptexinfmt.el; Fix last change
[elisp/wanderlust.git] / elmo / elmo-date.el
1 ;;; elmo-date.el --- Date processing module for ELMO.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
9
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)
13 ;; any later version.
14 ;;
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.
19 ;;
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.
24 ;;
25
26 ;;; Commentary:
27 ;;
28
29 ;;; Code:
30 ;;
31
32
33 (require 'path-util)
34 (require 'timezone)
35 (require 'elmo-vars)
36 (eval-when-compile (require 'cl))
37
38 (defmacro elmo-match-substring (pos string from)
39   "Substring of POSth matched string of STRING."
40   `(substring ,string
41               (+ (match-beginning ,pos) ,from)
42               (match-end ,pos)))
43
44 (defmacro elmo-match-string (pos string)
45   "Substring POSth matched STRING."
46   `(substring ,string (match-beginning ,pos) (match-end ,pos)))
47
48 (defmacro elmo-match-buffer (pos)
49   "Substring POSth matched from the current buffer."
50   `(buffer-substring-no-properties
51     (match-beginning ,pos) (match-end ,pos)))
52
53 ;; from subr.el
54 (defun elmo-replace-in-string (str regexp newtext &optional literal)
55   "Replace all matches in STR for REGEXP with NEWTEXT string.
56 And returns the new string.
57 Optional LITERAL non-nil means do a literal replacement.
58 Otherwise treat \\ in NEWTEXT string as special:
59   \\& means substitute original matched text,
60   \\N means substitute match for \(...\) number N,
61   \\\\ means insert one \\."
62   (let ((rtn-str "")
63         (start 0)
64         (special)
65         match prev-start)
66     (while (setq match (string-match regexp str start))
67       (setq prev-start start
68             start (match-end 0)
69             rtn-str
70             (concat
71              rtn-str
72              (substring str prev-start match)
73              (cond (literal newtext)
74                    (t (mapconcat
75                        (lambda (c)
76                          (if special
77                              (progn
78                                (setq special nil)
79                                (cond ((eq c ?\\) "\\")
80                                      ((eq c ?&)
81                                       (elmo-match-string 0 str))
82                                      ((and (>= c ?0) (<= c ?9))
83                                       (if (> c (+ ?0 (length
84                                                       (match-data))))
85                                           ;; Invalid match num
86                                           (error "Invalid match num: %c" c)
87                                         (setq c (- c ?0))
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))))
92                        newtext ""))))))
93     (concat rtn-str (substring str start))))
94
95 (defvar elmo-date-descriptions
96   '((yesterday . [0 0 1])
97     (lastweek  . [0 0 7])
98     (lastmonth . [0 1 0])
99     (lastyear  . [1 0 0])))
100
101 (defun elmo-date-get-description (datevec)
102   (format "%d-%s-%d"
103           (aref datevec 2)
104           (car (rassq (aref datevec 1)
105                       timezone-months-assoc))
106           (aref datevec 0)))
107
108 (defun elmo-date-get-datevec (description)
109   (cond
110    ((not elmo-date-match)
111     (error "Date match is not available"))
112    ((string-match "^[ \t]*\\([0-9]+\\)?[ \t]*\\([a-zA-Z]+\\)$" description)
113     (let ((today
114            (save-match-data
115              (timezone-fix-time (current-time-string) (current-time-zone)
116                                 nil)))
117           (number
118            (string-to-number
119             (if (match-beginning 1)
120                 (elmo-match-string 1 description)
121               "0")))
122           (suffix (downcase (elmo-match-string 2 description)))
123           pair)
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)
130     (timezone-fix-time
131      (concat (elmo-replace-in-string description "-" " ") " 0:0")
132      (current-time-zone) nil))
133    ((string-match "\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)" description)
134     (vector (string-to-number (match-string 1 description))
135             (string-to-number (match-string 2 description))
136             (string-to-number (match-string 3 description))
137             0 0 0
138             (current-time-zone)))))
139
140 (defun elmo-datevec-substitute (datevec1 datevec2)
141   (if (/= (aref datevec2 2) 0)
142       (elmo-date-get-offset-datevec datevec1 (aref datevec2 2))
143     (let ((year (- (aref datevec1 0) (aref datevec2 0)))
144           (month (- (aref datevec1 1) (aref datevec2 1)))
145           (timezone (current-time-zone)))
146       (while (<= month 0)
147         (setq year (1- year)
148               month (+ 12 month)))
149       (timezone-fix-time
150        (format "%d %s %d 0:00 %s"
151                (aref datevec1 2)
152                (car (rassq month timezone-months-assoc))
153                year
154                (cadr timezone)) nil nil))))
155
156 (defun elmo-date-get-week (year month mday)
157   (let ((wday (symbol-value (intern (format
158                                      "elmo-weekday-name-%s"
159                                      elmo-lang))))
160         y1 days p)
161     (setq y1 (- year 1))
162     (setq days (- (+ (* y1 365) (/ y1 400) (/ y1 4)) (/ y1 100)))
163     (setq p 1)
164     (while (< p month)
165       (setq days (+ days (timezone-last-day-of-month p year)))
166       (setq p (+ p 1)))
167     (setq days (+ days mday))
168     (aref wday (% days 7))))
169
170 (defun elmo-date-get-offset-datevec (datevec offset &optional time)
171   (let ((year  (aref datevec 0))
172         (month (aref datevec 1))
173         (day   (aref datevec 2))
174         (hour     (aref datevec 3))
175         (minute   (aref datevec 4))
176         (second   (aref datevec 5))
177         (timezone (aref datevec 6))
178         day-number p
179         day-of-month)
180     (setq p 1)
181     (setq day-number (- (timezone-day-number month day year)
182                         offset))
183     (while (<= day-number 0)
184       (setq year (1- year)
185             day-number (+ (timezone-day-number 12 31 year)
186                           day-number)))
187     (while (> day-number (setq day-of-month
188                                (timezone-last-day-of-month p year)))
189       (setq day-number (- day-number day-of-month))
190       (setq p (1+ p)))
191     (setq month p)
192     (setq day day-number)
193     (timezone-fix-time
194      (format "%d %s %d %s %s"
195              day
196              (car (rassq month timezone-months-assoc))
197              year
198              (if time
199                  (format "%d:%d:%d" hour minute second)
200                "0:00")
201              (cadr timezone)) nil nil)))
202
203 (defmacro elmo-date-make-sortable-string (datevec)
204   "Make a sortable string from DATEVEC."
205   `(timezone-make-sortable-date
206     (aref ,datevec 0)
207     (aref ,datevec 1)
208     (aref ,datevec 2)
209     (timezone-make-time-string
210      (aref ,datevec 3)
211      (aref ,datevec 4)
212      (aref ,datevec 5))))
213
214 (defsubst elmo-datevec-to-time (datevec)
215   (encode-time (aref datevec 5) (aref datevec 4) (aref datevec 3)
216                (aref datevec 2) (aref datevec 1) (aref datevec 0)
217                (aref datevec 6)))
218
219 (defun elmo-time-parse-date-string (date)
220   (ignore-errors
221    (elmo-datevec-to-time (timezone-fix-time date nil nil))))
222
223 (defun elmo-time-make-date-string (time)
224   (let ((system-time-locale "C"))
225     (format-time-string "%a, %d %b %Y %T %z" time)))
226
227 (defun elmo-time-less-p (lhs rhs)
228   (while (and (car lhs) (car rhs))
229     (cond ((< (car lhs) (car rhs))
230            (setq lhs nil))
231           ((= (car lhs) (car rhs))
232            (setq lhs (cdr lhs)
233                  rhs (cdr rhs)))
234           (t
235            (setq rhs nil))))
236   (not (null rhs)))
237
238 (defalias 'elmo-time< 'elmo-time-less-p)
239
240 (defun elmo-time-to-days (time)
241   (let ((date (decode-time time)))
242     (timezone-absolute-from-gregorian
243      (nth 4 date) (nth 3 date) (nth 5 date))))
244
245 ;; from timezone-fix-time in `timezone.el'
246 (defun elmo-time-to-datevec (time &optional timezone)
247   (when time
248     (let* ((date   (decode-time time))
249            (year   (nth 5 date))
250            (month  (nth 4 date))
251            (day    (nth 3 date))
252            (hour   (nth 2 date))
253            (minute (nth 1 date))
254            (second (nth 0 date))
255            (local  (nth 8 date))
256            (timezone
257             (or timezone
258                 (timezone-time-zone-from-absolute
259                  (timezone-absolute-from-gregorian month day year)
260                  (+ second (* 60 (+ minute (* 60 hour)))))))
261            (diff   (- (timezone-zone-to-minute timezone) (/ local 60)))
262            (minute (+ minute diff))
263            (hour-fix (floor minute 60)))
264       (setq hour (+ hour hour-fix))
265       (setq minute (- minute (* 60 hour-fix)))
266       ;; HOUR may be larger than 24 or smaller than 0.
267       (cond ((<= 24 hour)                       ;24 -> 00
268              (setq hour (- hour 24))
269              (setq day  (1+ day))
270              (when (< (timezone-last-day-of-month month year) day)
271                (setq month (1+ month))
272                (setq day 1)
273                (when (< 12 month)
274                  (setq month 1)
275                  (setq year (1+ year)))))
276             ((> 0 hour)
277              (setq hour (+ hour 24))
278              (setq day  (1- day))
279              (when (> 1 day)
280                (setq month (1- month))
281                (when (> 1 month)
282                  (setq month 12)
283                  (setq year (1- year)))
284                (setq day (timezone-last-day-of-month month year)))))
285       (vector year month day hour minute second timezone))))
286
287 (require 'product)
288 (product-provide (provide 'elmo-date) (require 'elmo-version))
289
290 ;;; elmo-date.el ends here