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