cd6f9e9d3abb4937bf4b2181bcb4a99b76d3581d
[elisp/gnus.git-] / lisp / time-date.el
1 ;;; time-date.el --- Date and time handling functions
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      Masanobu Umeda <umerin@mse.kyutech.ac.jp>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-and-compile
28   (eval
29    '(if (not (string-match "XEmacs" emacs-version))
30         (require 'parse-time)
31
32       (require 'timezone)
33       (defun parse-time-string (date)
34         "Convert DATE into time."
35         (decode-time
36          (condition-case ()
37              (let* ((d1 (timezone-parse-date date))
38                     (t1 (timezone-parse-time (aref d1 3))))
39                (apply 'encode-time
40                       (mapcar (lambda (el)
41                                 (and el (string-to-number el)))
42                               (list
43                                (aref t1 2) (aref t1 1) (aref t1 0)
44                                (aref d1 2) (aref d1 1) (aref d1 0)
45                                (number-to-string
46                                 (* 60 (timezone-zone-to-minute (aref d1 4))))))))
47            ;; If we get an error, then we just return a 0 time.
48            (error (list 0 0))))))))
49
50 (defun date-to-time (date)
51   "Convert DATE into time."
52   (apply 'encode-time (parse-time-string date)))
53
54 (defun time-to-float (time)
55   "Convert TIME to a floating point number."
56   (+ (* (car time) 65536.0)
57      (cadr time)))
58
59 (defun float-to-time (float)
60   "Convert FLOAT (a floating point number) to an Emacs time structure."
61   (list (floor float 65536)
62         (floor (mod float 65536))))
63
64 (defun time-less-p (t1 t2)
65   "Say whether time T1 is less than time T2."
66   (or (< (car t1) (car t2))
67       (and (= (car t1) (car t2))
68            (< (nth 1 t1) (nth 1 t2)))))
69
70 (defun days-to-time (days)
71   "Convert DAYS into time."
72   (let* ((seconds (* 1.0 days 60 60 24))
73          (rest (expt 2 16))
74          (ms (condition-case nil (floor (/ seconds rest))
75                (range-error (expt 2 16)))))
76     (list ms (condition-case nil (round (- seconds (* ms rest)))
77                (range-error (expt 2 16))))))
78
79 (defun time-since (time)
80   "Return the time since TIME, which is either an internal time or a date."
81   (when (stringp time)
82     ;; Convert date strings to internal time.
83     (setq time (date-to-time time)))
84   (let* ((current (current-time))
85          (rest (when (< (nth 1 current) (nth 1 time))
86                  (expt 2 16))))
87     (list (- (+ (car current) (if rest -1 0)) (car time))
88           (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
89
90 (defun subtract-time (t1 t2)
91   "Subtract two internal times."
92   (let ((borrow (< (cadr t1) (cadr t2))))
93     (list (- (car t1) (car t2) (if borrow 1 0))
94           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
95
96 (defun date-to-day (date)
97   "Return the number of days between year 1 and DATE."
98   (time-to-day (date-to-time date)))
99   
100 (defun days-between (date1 date2)
101   "Return the number of days between DATE1 and DATE2."
102   (- (date-to-day date1) (date-to-day date2)))
103
104 (defun date-leap-year-p (year)
105   "Return t if YEAR is a leap year."
106   (or (and (zerop (% year 4))
107            (not (zerop (% year 100))))
108       (zerop (% year 400))))
109
110 (defun time-to-day-in-year (time)
111   "Return the day number within the year of the date month/day/year."
112   (let* ((tim (decode-time time))
113          (month (nth 4 tim))
114          (day (nth 3 tim))
115          (year (nth 5 tim))
116          (day-of-year (+ day (* 31 (1- month)))))
117     (when (> month 2)
118       (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
119       (when (date-leap-year-p year)
120         (setq day-of-year (1+ day-of-year))))
121     day-of-year))
122
123 (defun time-to-day (time)
124   "The number of days between the Gregorian date 0001-12-31bce and TIME.
125 The Gregorian date Sunday, December 31, 1bce is imaginary."
126   (let* ((tim (decode-time time))
127          (month (nth 4 tim))
128          (day (nth 3 tim))
129          (year (nth 5 tim)))
130     (+ (time-to-day-in-year time)       ;       Days this year
131        (* 365 (1- year))                ;       + Days in prior years
132        (/ (1- year) 4)                  ;       + Julian leap years
133        (- (/ (1- year) 100))            ;       - century years
134        (/ (1- year) 400))))             ;       + Gregorian leap years
135
136 (provide 'time-date)
137
138 ;;; time-date.el ends here