1 ;;; time-date.el --- Date and time handling functions
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp>
6 ;; This file is part of GNU Emacs.
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)
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.
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.
29 '(if (not (string-match "XEmacs" emacs-version))
33 (defun parse-time-string (date)
34 "Convert DATE into time."
37 (let* ((d1 (timezone-parse-date date))
38 (t1 (timezone-parse-time (aref d1 3))))
41 (and el (string-to-number el)))
43 (aref t1 2) (aref t1 1) (aref t1 0)
44 (aref d1 2) (aref d1 1) (aref d1 0)
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))))))))
50 (defun date-to-time (date)
51 "Convert DATE into time."
52 (apply 'encode-time (parse-time-string date)))
54 (defun time-to-float (time)
55 "Convert TIME to a floating point number."
56 (+ (* (car time) 65536.0)
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))))
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)))))
70 (defun days-to-time (days)
71 "Convert DAYS into time."
72 (let* ((seconds (* 1.0 days 60 60 24))
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))))))
79 (defun time-since (time)
80 "Return the time since TIME, which is either an internal time or a date."
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))
87 (list (- (+ (car current) (if rest -1 0)) (car time))
88 (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
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)))))
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)))
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)))
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))))
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))
116 (day-of-year (+ day (* 31 (1- month)))))
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))))
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))
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
138 ;;; time-date.el ends here