T-gnus 6.15.18 revision 00.
[elisp/gnus.git-] / lisp / time-date.el
1 ;;; time-date.el --- Date and time handling functions
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      Masanobu Umeda <umerin@mse.kyutech.ac.jp>
6 ;; Keywords: mail news util
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs 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 ;; GNU Emacs 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 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'parse-time)
32
33 (autoload 'timezone-make-date-arpa-standard "timezone")
34
35 ;;;###autoload
36 (defun date-to-time (date)
37   "Parse a string that represents a date-time and return a time value."
38   (condition-case ()
39       (apply 'encode-time
40              (parse-time-string
41               ;; `parse-time-string' isn't sufficiently general or
42               ;; robust.  It fails to grok some of the formats that
43               ;; timezone does (e.g. dodgy post-2000 stuff from some
44               ;; Elms) and either fails or returns bogus values.  Lars
45               ;; reverted this change, but that loses non-trivially
46               ;; often for me.  -- fx
47               (timezone-make-date-arpa-standard date)))
48     (error (error "Invalid date: %s" date))))
49
50 (defun time-to-seconds (time)
51   "Convert time value TIME to a floating point number.
52 You can use `float-time' instead."
53   (+ (* (car time) 65536.0)
54      (cadr time)
55      (/ (or (nth 2 time) 0) 1000000.0)))
56
57 ;;;###autoload
58 (defun seconds-to-time (seconds)
59   "Convert SECONDS (a floating point number) to a time value."
60   (list (floor seconds 65536)
61         (floor (mod seconds 65536))
62         (floor (* (- seconds (ffloor seconds)) 1000000))))
63
64 ;;;###autoload
65 (defun time-less-p (t1 t2)
66   "Say whether time value T1 is less than time value T2."
67   (or (< (car t1) (car t2))
68       (and (= (car t1) (car t2))
69            (< (nth 1 t1) (nth 1 t2)))))
70
71 ;;;###autoload
72 (defun days-to-time (days)
73   "Convert DAYS into a time value."
74   (let* ((seconds (* 1.0 days 60 60 24))
75          (rest (expt 2 16))
76          (ms (condition-case nil (floor (/ seconds rest))
77                (range-error (expt 2 16)))))
78     (list ms (condition-case nil (round (- seconds (* ms rest)))
79                (range-error (expt 2 16))))))
80
81 ;;;###autoload
82 (defun time-since (time)
83   "Return the time elapsed since TIME.
84 TIME should be either a time value or a date-time string."
85   (when (stringp time)
86     ;; Convert date strings to internal time.
87     (setq time (date-to-time time)))
88   (let* ((current (current-time))
89          (rest (when (< (nth 1 current) (nth 1 time))
90                  (expt 2 16))))
91     (list (- (+ (car current) (if rest -1 0)) (car time))
92           (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
93
94 ;;;###autoload
95 (defalias 'subtract-time 'time-subtract)
96
97 ;;;###autoload
98 (defun time-subtract (t1 t2)
99   "Subtract two time values.
100 Return the difference in the format of a time value."
101   (let ((borrow (< (cadr t1) (cadr t2))))
102     (list (- (car t1) (car t2) (if borrow 1 0))
103           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
104
105 ;;;###autoload
106 (defun time-add (t1 t2)
107   "Add two time values.  One should represent a time difference."
108   (let ((high (car t1))
109         (low (if (consp (cdr t1)) (nth 1 t1) (cdr t1)))
110         (micro (if (numberp (car-safe (cdr-safe (cdr t1))))
111                    (nth 2 t1)
112                  0))
113         (high2 (car t2))
114         (low2 (if (consp (cdr t2)) (nth 1 t2) (cdr t2)))
115         (micro2 (if (numberp (car-safe (cdr-safe (cdr t2))))
116                     (nth 2 t2)
117                   0)))
118     ;; Add
119     (setq micro (+ micro micro2))
120     (setq low (+ low low2))
121     (setq high (+ high high2))
122
123     ;; Normalize
124     ;; `/' rounds towards zero while `mod' returns a positive number,
125     ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))).
126     (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0)))
127     (setq micro (mod micro 1000000))
128     (setq high (+ high (/ low 65536) (if (< low 0) -1 0)))
129     (setq low (logand low 65535))
130
131     (list high low micro)))
132
133 ;;;###autoload
134 (defun date-to-day (date)
135   "Return the number of days between year 1 and DATE.
136 DATE should be a date-time string."
137   (time-to-days (date-to-time date)))
138
139 ;;;###autoload
140 (defun days-between (date1 date2)
141   "Return the number of days between DATE1 and DATE2.
142 DATE1 and DATE2 should be date-time strings."
143   (- (date-to-day date1) (date-to-day date2)))
144
145 ;;;###autoload
146 (defun date-leap-year-p (year)
147   "Return t if YEAR is a leap year."
148   (or (and (zerop (% year 4))
149            (not (zerop (% year 100))))
150       (zerop (% year 400))))
151
152 ;;;###autoload
153 (defun time-to-day-in-year (time)
154   "Return the day number within the year of the date month/day/year."
155   (let* ((tim (decode-time time))
156          (month (nth 4 tim))
157          (day (nth 3 tim))
158          (year (nth 5 tim))
159          (day-of-year (+ day (* 31 (1- month)))))
160     (when (> month 2)
161       (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
162       (when (date-leap-year-p year)
163         (setq day-of-year (1+ day-of-year))))
164     day-of-year))
165
166 ;;;###autoload
167 (defun time-to-days (time)
168   "The number of days between the Gregorian date 0001-12-31bce and TIME.
169 TIME should be a time value.
170 The Gregorian date Sunday, December 31, 1bce is imaginary."
171   (let* ((tim (decode-time time))
172          (month (nth 4 tim))
173          (day (nth 3 tim))
174          (year (nth 5 tim)))
175     (+ (time-to-day-in-year time)       ;       Days this year
176        (* 365 (1- year))                ;       + Days in prior years
177        (/ (1- year) 4)                  ;       + Julian leap years
178        (- (/ (1- year) 100))            ;       - century years
179        (/ (1- year) 400))))             ;       + Gregorian leap years
180
181 (defun time-to-number-of-days (time)
182   "Return the number of days represented by TIME.
183 The number of days will be returned as a floating point number."
184   (/ (+ (* 1.0 65536 (car time)) (cadr time)) (* 60 60 24)))
185
186 ;;;###autoload
187 (defun safe-date-to-time (date)
188   "Parse a string that represents a date-time and return a time value.
189 If DATE is malformed, return a time value of zeros."
190   (condition-case ()
191       (date-to-time date)
192     (error '(0 0))))
193
194 (provide 'time-date)
195
196 ;;; time-date.el ends here