1 ;;; 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 (defun parse-time-string (date)
30 "Convert DATE into time."
33 (let* ((d1 (timezone-parse-date date))
34 (t1 (timezone-parse-time (aref d1 3))))
37 (and el (string-to-number el)))
39 (aref t1 2) (aref t1 1) (aref t1 0)
40 (aref d1 2) (aref d1 1) (aref d1 0)
42 (* 60 (timezone-zone-to-minute (aref d1 4))))))))
43 ;; If we get an error, then we just return a 0 time.
46 (defun date-to-time (date)
47 "Convert DATE into time."
48 (apply 'encode-time (parse-time-string date)))
50 (defun time-less-p (t1 t2)
51 "Say whether time T1 is less than time T2."
52 (or (< (car t1) (car t2))
53 (and (= (car t1) (car t2))
54 (< (nth 1 t1) (nth 1 t2)))))
56 (defun days-to-time (days)
57 "Convert DAYS into time."
58 (let* ((seconds (* 1.0 days 60 60 24))
60 (ms (condition-case nil (floor (/ seconds rest))
61 (range-error (expt 2 16)))))
62 (list ms (condition-case nil (round (- seconds (* ms rest)))
63 (range-error (expt 2 16))))))
65 (defun time-since (time)
66 "Return the time since TIME, which is either an internal time or a date."
68 ;; Convert date strings to internal time.
69 (setq time (date-to-time time)))
70 (let* ((current (current-time))
71 (rest (when (< (nth 1 current) (nth 1 time))
73 (list (- (+ (car current) (if rest -1 0)) (car time))
74 (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
76 (defun subtract-time (t1 t2)
77 "Subtract two internal times."
78 (let ((borrow (< (cadr t1) (cadr t2))))
79 (list (- (car t1) (car t2) (if borrow 1 0))
80 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
82 (defun date-to-day (date)
83 "Return the number of days between year 1 and DATE."
84 (time-to-day (date-to-time date)))
86 (defun days-between (date1 date2)
87 "Return the number of days between DATE1 and DATE2."
88 (- (date-to-day date1) (date-to-day date2)))
90 (defun date-leap-year-p (year)
91 "Return t if YEAR is a leap year."
92 (or (and (zerop (% year 4))
93 (not (zerop (% year 100))))
94 (zerop (% year 400))))
96 (defun time-to-day-in-year (time)
97 "Return the day number within the year of the date month/day/year."
98 (let* ((tim (decode-time time))
102 (day-of-year (+ day (* 31 (1- month)))))
104 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
105 (when (date-leap-year-p year)
106 (setq day-of-year (1+ day-of-year))))
109 (defun time-to-day (time)
110 "The number of days between the Gregorian date 0001-12-31bce and TIME.
111 The Gregorian date Sunday, December 31, 1bce is imaginary."
112 (let* ((tim (decode-time time))
116 (+ (time-to-day-in-year time) ; Days this year
117 (* 365 (1- year)) ; + Days in prior years
118 (/ (1- year) 4) ; + Julian leap years
119 (- (/ (1- year) 100)) ; - century years
120 (/ (1- year) 400)))) ; + Gregorian leap years
124 ;;; date.el ends here