1 ;;; time-date.el --- Date and time handling functions
2 ;; Copyright (C) 1998,99 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.
27 (eval-when-compile (require 'cl))
30 (defun date-to-time (date)
31 "Convert DATE into time."
33 (apply 'encode-time (parse-time-string date))
34 (error (error "Invalid date: %s" date))))
36 (defun time-to-seconds (time)
37 "Convert TIME to a floating point number."
38 (+ (* (car time) 65536.0)
40 (/ (or (caddr time) 0) 1000000.0)))
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))))
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)))))
54 (defun days-to-time (days)
55 "Convert DAYS into time."
56 (let* ((seconds (* 1.0 days 60 60 24))
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))))))
63 (defun time-since (time)
64 "Return the time since TIME, which is either an internal time or a date."
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))
71 (list (- (+ (car current) (if rest -1 0)) (car time))
72 (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
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)))))
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)))
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)))
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))))
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))
100 (day-of-year (+ day (* 31 (1- month)))))
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))))
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))
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
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."
129 ;;; time-date.el ends here