Fix typo.
[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 (if (module-installed-p 'timezone)
35     (require 'timezone))
36 (require 'elmo-vars)
37
38 (defvar elmo-date-descriptions
39   '((yesterday . [0 0 1])
40     (lastweek  . [0 0 7])
41     (lastmonth . [0 1 0])
42     (lastyear  . [1 0 0])))
43
44 (defun elmo-date-get-description (datevec)
45   (format "%d-%s-%d"
46           (aref datevec 2)
47           (car (rassq (aref datevec 1)
48                       timezone-months-assoc))
49           (aref datevec 0)))
50
51 (defun elmo-date-get-datevec (description)
52   (cond
53    ((not elmo-date-match)
54     (error "Date match is not available"))
55    ((string-match "^[ \t]*\\([0-9]+\\)?[ \t]*\\([a-zA-Z]+\\)$" description)
56     (let ((today
57            (save-match-data
58              (timezone-fix-time (current-time-string) (current-time-zone)
59                                 nil)))
60           (number
61            (string-to-int
62             (if (match-beginning 1)
63                 (elmo-match-string 1 description)
64               "0")))
65           (suffix (downcase (elmo-match-string 2 description)))
66           pair)
67       (if (setq pair (assq (intern suffix) elmo-date-descriptions))
68           (elmo-datevec-substitute today (cdr pair))
69         (if (string= "daysago" suffix)
70             (elmo-date-get-offset-datevec today number)
71           (error "%s is not supported yet" suffix)))))
72    ((string-match "[0-9]+-[A-Za-z]+-[0-9]+" description)
73     (timezone-fix-time
74      (concat (elmo-replace-in-string description "-" " ") " 0:00")
75      nil nil))))
76
77 (defun elmo-datevec-substitute (datevec1 datevec2)
78   (if (/= (aref datevec2 2) 0)
79       (elmo-date-get-offset-datevec datevec1 (aref datevec2 2))
80     (let ((year (- (aref datevec1 0) (aref datevec2 0)))
81           (month (- (aref datevec1 1) (aref datevec2 1)))
82           (timezone (current-time-zone)))
83       (while (<= month 0)
84         (setq year (1- year)
85               month (+ 12 month)))
86       (timezone-fix-time
87        (format "%d %s %d 0:00 %s"
88                (aref datevec1 2)
89                (car (rassq month timezone-months-assoc))
90                year
91                (cadr timezone)) nil nil))))
92
93 (defun elmo-date-get-week (year month mday)
94   (let ((wday (symbol-value (intern (format
95                                      "elmo-weekday-name-%s"
96                                      elmo-lang))))
97         y1 days p)
98     (setq y1 (- year 1))
99     (setq days (- (+ (* y1 365) (/ y1 400) (/ y1 4)) (/ y1 100)))
100     (setq p 1)
101     (while (< p month)
102       (setq days (+ days (timezone-last-day-of-month p year)))
103       (setq p (+ p 1))
104       )
105     (setq days (+ days mday))
106     (aref wday (% days 7))))
107
108 (defun elmo-date-get-offset-datevec (datevec offset &optional time)
109   (let ((year  (aref datevec 0))
110         (month (aref datevec 1))
111         (day   (aref datevec 2))
112         (hour     (aref datevec 3))
113         (minute   (aref datevec 4))
114         (second   (aref datevec 5))
115         (timezone (aref datevec 6))
116         day-number p
117         day-of-month)
118     (setq p 1)
119     (setq day-number (- (timezone-day-number month day year)
120                         offset))
121     (while (<= day-number 0)
122       (setq year (1- year)
123             day-number (+ (timezone-day-number 12 31 year)
124                           day-number)))
125     (while (> day-number (setq day-of-month
126                                (timezone-last-day-of-month p year)))
127       (setq day-number (- day-number day-of-month))
128       (setq p (1+ p)))
129     (setq month p)
130     (setq day day-number)
131     (timezone-fix-time
132      (format "%d %s %d %s %s"
133              day
134              (car (rassq month timezone-months-assoc))
135              year
136              (if time
137                  (format "%d:%d:%d" hour minute second)
138                "0:00")
139              (cadr timezone)) nil nil)))
140
141 (defmacro elmo-date-make-sortable-string (datevec)
142   "Make a sortable string from DATEVEC."
143   (` (timezone-make-sortable-date
144       (aref (, datevec) 0)
145       (aref (, datevec) 1)
146       (aref (, datevec) 2)
147       (aref (, datevec) 3))))
148
149 (require 'product)
150 (product-provide (provide 'elmo-date) (require 'elmo-version))
151
152 ;;; elmo-date.el ends here