Synch to No Gnus 200602081205.
[elisp/gnus.git-] / lisp / time-date.el
1 ;;; time-date.el --- Date and time handling functions
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      Masanobu Umeda <umerin@mse.kyutech.ac.jp>
8 ;; Keywords: mail news util
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; Time values come in three formats.  The oldest format is a cons
30 ;; cell of the form (HIGH . LOW).  This format is obsolete, but still
31 ;; supported.  The two other formats are the lists (HIGH LOW) and
32 ;; (HIGH LOW MICRO).  The first two formats specify HIGH * 2^16 + LOW
33 ;; seconds; the third format specifies HIGH * 2^16 + LOW + MICRO /
34 ;; 1000000 seconds.  We should have 0 <= MICRO < 1000000 and 0 <= LOW
35 ;; < 2^16.  If the time value represents a point in time, then HIGH is
36 ;; nonnegative.  If the time value is a time difference, then HIGH can
37 ;; be negative as well.  The macro `with-decoded-time-value' and the
38 ;; function `encode-time-value' make it easier to deal with these
39 ;; three formats.  See `time-subtract' for an example of how to use
40 ;; them.
41
42 ;;; Code:
43
44 (eval-when-compile (require 'cl))
45
46 (defmacro with-decoded-time-value (varlist &rest body)
47   "Decode a time value and bind it according to VARLIST, then eval BODY.
48
49 The value of the last form in BODY is returned.
50
51 Each element of the list VARLIST is a list of the form
52 \(HIGH-SYMBOL LOW-SYMBOL MICRO-SYMBOL [TYPE-SYMBOL] TIME-VALUE).
53 The time value TIME-VALUE is decoded and the result it bound to
54 the symbols HIGH-SYMBOL, LOW-SYMBOL and MICRO-SYMBOL.
55
56 The optional TYPE-SYMBOL is bound to the type of the time value.
57 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH
58 LOW), and type 3 is the list (HIGH LOW MICRO)."
59   (declare (indent 1)
60            (debug ((&rest (symbolp symbolp symbolp &or [symbolp form] form))
61                    body)))
62   (if varlist
63       (let* ((elt (pop varlist))
64              (high (pop elt))
65              (low (pop elt))
66              (micro (pop elt))
67              (type (unless (eq (length elt) 1)
68                      (pop elt)))
69              (time-value (car elt))
70              (gensym (make-symbol "time")))
71         `(let* ,(append `((,gensym ,time-value)
72                           (,high (pop ,gensym))
73                           ,low ,micro)
74                         (when type `(,type)))
75            (if (consp ,gensym)
76                (progn
77                  (setq ,low (pop ,gensym))
78                  (if ,gensym
79                      ,(append `(setq ,micro (car ,gensym))
80                               (when type `(,type 2)))
81                    ,(append `(setq ,micro 0)
82                             (when type `(,type 1)))))
83              ,(append `(setq ,low ,gensym ,micro 0)
84                       (when type `(,type 0))))
85            (with-decoded-time-value ,varlist ,@body)))
86     `(progn ,@body)))
87
88 (defun encode-time-value (high low micro type)
89   "Encode HIGH, LOW, and MICRO into a time value of type TYPE.
90 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH LOW),
91 and type 3 is the list (HIGH LOW MICRO)."
92   (cond
93    ((eq type 0) (cons high low))
94    ((eq type 1) (list high low))
95    ((eq type 2) (list high low micro))))
96
97 (autoload 'parse-time-string "parse-time")
98 (autoload 'timezone-make-date-arpa-standard "timezone")
99
100 ;;;###autoload
101 (defun date-to-time (date)
102   "Parse a string that represents a date-time and return a time value."
103   (condition-case ()
104       (apply 'encode-time
105              (parse-time-string
106               ;; `parse-time-string' isn't sufficiently general or
107               ;; robust.  It fails to grok some of the formats that
108               ;; timezone does (e.g. dodgy post-2000 stuff from some
109               ;; Elms) and either fails or returns bogus values.  Lars
110               ;; reverted this change, but that loses non-trivially
111               ;; often for me.  -- fx
112               (timezone-make-date-arpa-standard date)))
113     (error (error "Invalid date: %s" date))))
114
115 (defun time-to-seconds (time)
116   "Convert time value TIME to a floating point number.
117 You can use `float-time' instead."
118   (with-decoded-time-value ((high low micro time))
119     (+ (* 1.0 high 65536)
120        low
121        (/ micro 1000000.0))))
122
123 ;;;###autoload
124 (defun seconds-to-time (seconds)
125   "Convert SECONDS (a floating point number) to a time value."
126   (list (floor seconds 65536)
127         (floor (mod seconds 65536))
128         (floor (* (- seconds (ffloor seconds)) 1000000))))
129
130 ;;;###autoload
131 (defun time-less-p (t1 t2)
132   "Say whether time value T1 is less than time value T2."
133   (with-decoded-time-value ((high1 low1 micro1 t1)
134                             (high2 low2 micro2 t2))
135     (or (< high1 high2)
136         (and (= high1 high2)
137              (or (< low1 low2)
138                  (and (= low1 low2)
139                       (< micro1 micro2)))))))
140
141 ;;;###autoload
142 (defun days-to-time (days)
143   "Convert DAYS into a time value."
144   (let* ((seconds (* 1.0 days 60 60 24))
145          (high (condition-case nil (floor (/ seconds 65536))
146                  (range-error most-positive-fixnum))))
147     (list high (condition-case nil (floor (- seconds (* 1.0 high 65536)))
148                  (range-error 65535)))))
149
150 ;;;###autoload
151 (defun time-since (time)
152   "Return the time elapsed since TIME.
153 TIME should be either a time value or a date-time string."
154   (when (stringp time)
155     ;; Convert date strings to internal time.
156     (setq time (date-to-time time)))
157   (time-subtract (current-time) time))
158
159 ;;;###autoload
160 (defalias 'subtract-time 'time-subtract)
161
162 ;;;###autoload
163 (defun time-subtract (t1 t2)
164   "Subtract two time values.
165 Return the difference in the format of a time value."
166   (with-decoded-time-value ((high low micro type t1)
167                             (high2 low2 micro2 type2 t2))
168     (setq high (- high high2)
169           low (- low low2)
170           micro (- micro micro2)
171           type (max type type2))
172     (when (< micro 0)
173       (setq low (1- low)
174             micro (+ micro 1000000)))
175     (when (< low 0)
176       (setq high (1- high)
177             low (+ low 65536)))
178     (encode-time-value high low micro type)))
179
180 ;;;###autoload
181 (defun time-add (t1 t2)
182   "Add two time values.  One should represent a time difference."
183   (with-decoded-time-value ((high low micro type t1)
184                             (high2 low2 micro2 type2 t2))
185     (setq high (+ high high2)
186           low (+ low low2)
187           micro (+ micro micro2)
188           type (max type type2))
189     (when (>= micro 1000000)
190       (setq low (1+ low)
191             micro (- micro 1000000)))
192     (when (>= low 65536)
193       (setq high (1+ high)
194             low (- low 65536)))
195     (encode-time-value high low micro type)))
196
197 ;;;###autoload
198 (defun date-to-day (date)
199   "Return the number of days between year 1 and DATE.
200 DATE should be a date-time string."
201   (time-to-days (date-to-time date)))
202
203 ;;;###autoload
204 (defun days-between (date1 date2)
205   "Return the number of days between DATE1 and DATE2.
206 DATE1 and DATE2 should be date-time strings."
207   (- (date-to-day date1) (date-to-day date2)))
208
209 ;;;###autoload
210 (defun date-leap-year-p (year)
211   "Return t if YEAR is a leap year."
212   (or (and (zerop (% year 4))
213            (not (zerop (% year 100))))
214       (zerop (% year 400))))
215
216 ;;;###autoload
217 (defun time-to-day-in-year (time)
218   "Return the day number within the year of the date month/day/year."
219   (let* ((tim (decode-time time))
220          (month (nth 4 tim))
221          (day (nth 3 tim))
222          (year (nth 5 tim))
223          (day-of-year (+ day (* 31 (1- month)))))
224     (when (> month 2)
225       (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
226       (when (date-leap-year-p year)
227         (setq day-of-year (1+ day-of-year))))
228     day-of-year))
229
230 ;;;###autoload
231 (defun time-to-days (time)
232   "The number of days between the Gregorian date 0001-12-31bce and TIME.
233 TIME should be a time value.
234 The Gregorian date Sunday, December 31, 1bce is imaginary."
235   (let* ((tim (decode-time time))
236          (month (nth 4 tim))
237          (day (nth 3 tim))
238          (year (nth 5 tim)))
239     (+ (time-to-day-in-year time)       ;       Days this year
240        (* 365 (1- year))                ;       + Days in prior years
241        (/ (1- year) 4)                  ;       + Julian leap years
242        (- (/ (1- year) 100))            ;       - century years
243        (/ (1- year) 400))))             ;       + Gregorian leap years
244
245 (defun time-to-number-of-days (time)
246   "Return the number of days represented by TIME.
247 The number of days will be returned as a floating point number."
248   (/ (time-to-seconds time) (* 60 60 24)))
249
250 ;;;###autoload
251 (defun safe-date-to-time (date)
252   "Parse a string that represents a date-time and return a time value.
253 If DATE is malformed, return a time value of zeros."
254   (condition-case ()
255       (date-to-time date)
256     (error '(0 0))))
257
258 (provide 'time-date)
259
260 ;;; time-date.el ends here