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