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