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