7dd66bbf65cd3ffe7c093612b8a251188ddb7597
[elisp/wanderlust.git] / elmo / elmo-date.el
1 ;;; elmo-date.el -- Date processing module for ELMO.
2
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7 ;; Time-stamp: <00/03/14 19:38:44 teranisi>
8
9 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15 ;;
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 ;;
26
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31 ;; 
32
33
34 (require 'path-util)
35 (if (module-installed-p 'timezone)
36     (require 'timezone))
37 (require 'elmo-vars)
38
39 (defvar elmo-date-descriptions
40   '((yesterday . [0 0 1])
41     (lastweek  . [0 0 7])
42     (lastmonth . [0 1 0])
43     (lastyear  . [1 0 0])))
44
45 (defun elmo-date-get-description (datevec)
46   (format "%d-%s-%d"
47           (aref datevec 2)
48           (car (rassq (aref datevec 1)
49                       timezone-months-assoc))
50           (aref datevec 0)))
51
52 (defun elmo-date-get-datevec (description)
53   (cond 
54    ((not elmo-date-match)
55     (error "date match is not available"))
56    ((string-match "^[ \t]*\\([0-9]+\\)?[ \t]*\\([a-zA-Z]+\\)$" description)
57     (let ((today 
58            (save-match-data
59              (timezone-fix-time (current-time-string) (current-time-zone)
60                                 nil)))
61           (number 
62            (string-to-int
63             (if (match-beginning 1)
64                 (elmo-match-string 1 description)
65               "0")))
66           (suffix (downcase (elmo-match-string 2 description)))
67           pair)
68       (if (setq pair (assq (intern suffix) elmo-date-descriptions))
69           (elmo-datevec-substitute today (cdr pair))
70         (if (string= "daysago" suffix)
71             (elmo-date-get-offset-datevec today number)
72           (error "%s is not supported yet" suffix)))))
73    ((string-match "[0-9]+-[A-Za-z]+-[0-9]+" description)
74     (timezone-fix-time 
75      (concat (elmo-replace-in-string description "-" " ") " 0:00")
76      nil nil))))
77
78 (defun elmo-datevec-substitute (datevec1 datevec2)
79   (if (/= (aref datevec2 2) 0)
80       (elmo-date-get-offset-datevec datevec1 (aref datevec2 2))
81     (let ((year (- (aref datevec1 0) (aref datevec2 0)))
82           (month (- (aref datevec1 1) (aref datevec2 1)))
83           (timezone (current-time-zone)))
84       (while (<= month 0)
85         (setq year (1- year)
86               month (+ 12 month)))
87       (timezone-fix-time
88        (format "%d %s %d 0:00 %s"
89                (aref datevec1 2)
90                (car (rassq month timezone-months-assoc))
91                year
92                (cadr timezone)) nil nil))))
93
94 (defun elmo-date-get-week (year month mday)
95   (let ((wday (symbol-value (intern (format
96                                      "elmo-weekday-name-%s"
97                                      elmo-lang))))
98         y1 days p)
99     (setq y1 (- year 1))
100     (setq days (- (+ (* y1 365) (/ y1 400) (/ y1 4)) (/ y1 100)))
101     (setq p 1)
102     (while (< p month)
103       (setq days (+ days (timezone-last-day-of-month p year)))
104       (setq p (+ p 1))
105       )
106     (setq days (+ days mday))
107     (aref wday (% days 7))))
108
109 (defun elmo-date-get-offset-datevec (datevec offset &optional time)
110   (let ((year  (aref datevec 0))
111         (month (aref datevec 1))
112         (day   (aref datevec 2))
113         (hour     (aref datevec 3))
114         (minute   (aref datevec 4))
115         (second   (aref datevec 5))
116         (timezone (aref datevec 6))
117         day-number p 
118         day-of-month)
119     (setq p 1)
120     (setq day-number (- (timezone-day-number month day year)
121                         offset))
122     (while (<= day-number 0)
123       (setq year (1- year)
124             day-number (+ (timezone-day-number 12 31 year)
125                           day-number)))
126     (while (> day-number (setq day-of-month
127                                (timezone-last-day-of-month p year)))
128       (setq day-number (- day-number day-of-month))
129       (setq p (1+ p)))
130     (setq month p)
131     (setq day day-number)
132     (timezone-fix-time
133      (format "%d %s %d %s %s"
134              day
135              (car (rassq month timezone-months-assoc))
136              year
137              (if time
138                  (format "%d:%d:%d" hour minute second)
139                "0:00")
140              (cadr timezone)) nil nil)))
141
142 (provide 'elmo-date)
143
144 ;;; elmo-date.el ends here