Add nnir-1.68.
[elisp/gnus.git-] / lisp / time-date.el
1 ;;; time-date.el --- Date and time handling functions
2 ;; Copyright (C) 1998, 1999, 2000, 2001 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   "Convert DATE into time."
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               ;; timzeone 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 to a floating point number."
52   (+ (* (car time) 65536.0)
53      (cadr time)
54      (/ (or (nth 2 time) 0) 1000000.0)))
55
56 (defun seconds-to-time (seconds)
57   "Convert SECONDS (a floating point number) to an Emacs time structure."
58   (list (floor seconds 65536)
59         (floor (mod seconds 65536))
60         (floor (* (- seconds (ffloor seconds)) 1000000))))
61
62 (defun time-less-p (t1 t2)
63   "Say whether time T1 is less than time T2."
64   (or (< (car t1) (car t2))
65       (and (= (car t1) (car t2))
66            (< (nth 1 t1) (nth 1 t2)))))
67
68 (defun days-to-time (days)
69   "Convert DAYS into time."
70   (let* ((seconds (* 1.0 days 60 60 24))
71          (rest (expt 2 16))
72          (ms (condition-case nil (floor (/ seconds rest))
73                (range-error (expt 2 16)))))
74     (list ms (condition-case nil (round (- seconds (* ms rest)))
75                (range-error (expt 2 16))))))
76
77 (defun time-since (time)
78   "Return the time since TIME, which is either an internal time or a date."
79   (when (stringp time)
80     ;; Convert date strings to internal time.
81     (setq time (date-to-time time)))
82   (let* ((current (current-time))
83          (rest (when (< (nth 1 current) (nth 1 time))
84                  (expt 2 16))))
85     (list (- (+ (car current) (if rest -1 0)) (car time))
86           (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
87
88 (defun subtract-time (t1 t2)
89   "Subtract two internal times."
90   (let ((borrow (< (cadr t1) (cadr t2))))
91     (list (- (car t1) (car t2) (if borrow 1 0))
92           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
93
94 (defun date-to-day (date)
95   "Return the number of days between year 1 and DATE."
96   (time-to-days (date-to-time date)))
97
98 (defun days-between (date1 date2)
99   "Return the number of days between DATE1 and DATE2."
100   (- (date-to-day date1) (date-to-day date2)))
101
102 (defun date-leap-year-p (year)
103   "Return t if YEAR is a leap year."
104   (or (and (zerop (% year 4))
105            (not (zerop (% year 100))))
106       (zerop (% year 400))))
107
108 (defun time-to-day-in-year (time)
109   "Return the day number within the year of the date month/day/year."
110   (let* ((tim (decode-time time))
111          (month (nth 4 tim))
112          (day (nth 3 tim))
113          (year (nth 5 tim))
114          (day-of-year (+ day (* 31 (1- month)))))
115     (when (> month 2)
116       (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
117       (when (date-leap-year-p year)
118         (setq day-of-year (1+ day-of-year))))
119     day-of-year))
120
121 (defun time-to-days (time)
122   "The number of days between the Gregorian date 0001-12-31bce and TIME.
123 The Gregorian date Sunday, December 31, 1bce is imaginary."
124   (let* ((tim (decode-time time))
125          (month (nth 4 tim))
126          (day (nth 3 tim))
127          (year (nth 5 tim)))
128     (+ (time-to-day-in-year time)       ;       Days this year
129        (* 365 (1- year))                ;       + Days in prior years
130        (/ (1- year) 4)                  ;       + Julian leap years
131        (- (/ (1- year) 100))            ;       - century years
132        (/ (1- year) 400))))             ;       + Gregorian leap years
133
134 (defun time-to-number-of-days (time)
135   "Return the number of days represented by TIME.
136 The number of days will be returned as a floating point number."
137   (/ (+ (* 1.0 65536 (car time)) (cadr time)) (* 60 60 24)))
138
139 ;;;###autoload
140 (defun safe-date-to-time (date)
141   "Parse DATE and return a time structure.
142 If DATE is malformed, a zero time will be returned."
143   (condition-case ()
144       (date-to-time date)
145     (error '(0 0))))
146
147 (provide 'time-date)
148
149 ;;; time-date.el ends here