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