]> code.delx.au - gnu-emacs/blob - lisp/calendar/time-date.el
From Ulf Jasper <ulf.jasper@web.de>:
[gnu-emacs] / lisp / calendar / time-date.el
1 ;;; time-date.el --- date and time handling functions
2 ;; Copyright (C) 1998, 1999, 2000, 2004 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp>
6 ;; Keywords: mail news util
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'parse-time)
30
31 (autoload 'timezone-make-date-arpa-standard "timezone")
32
33 ;;;###autoload
34 (defun date-to-time (date)
35 "Parse a string that represents a date-time and return a time value."
36 (condition-case ()
37 (apply 'encode-time
38 (parse-time-string
39 ;; `parse-time-string' isn't sufficiently general or
40 ;; robust. It fails to grok some of the formats that
41 ;; timezone does (e.g. dodgy post-2000 stuff from some
42 ;; Elms) and either fails or returns bogus values. Lars
43 ;; reverted this change, but that loses non-trivially
44 ;; often for me. -- fx
45 (timezone-make-date-arpa-standard date)))
46 (error (error "Invalid date: %s" date))))
47
48 ;;;###autoload
49 (defun time-to-seconds (time)
50 "Convert time value TIME to a floating point number.
51 You can use `float-time' instead."
52 (+ (* (car time) 65536.0)
53 (cadr time)
54 (/ (or (nth 2 time) 0) 1000000.0)))
55
56 ;;;###autoload
57 (defun seconds-to-time (seconds)
58 "Convert SECONDS (a floating point number) to a time value."
59 (list (floor seconds 65536)
60 (floor (mod seconds 65536))
61 (floor (* (- seconds (ffloor seconds)) 1000000))))
62
63 ;;;###autoload
64 (defun time-less-p (t1 t2)
65 "Say whether time value T1 is less than time value T2."
66 (or (< (car t1) (car t2))
67 (and (= (car t1) (car t2))
68 (< (nth 1 t1) (nth 1 t2)))))
69
70 ;;;###autoload
71 (defun days-to-time (days)
72 "Convert DAYS into a time value."
73 (let* ((seconds (* 1.0 days 60 60 24))
74 (rest (expt 2 16))
75 (ms (condition-case nil (floor (/ seconds rest))
76 (range-error (expt 2 16)))))
77 (list ms (condition-case nil (round (- seconds (* ms rest)))
78 (range-error (expt 2 16))))))
79
80 ;;;###autoload
81 (defun time-since (time)
82 "Return the time elapsed since TIME.
83 TIME should be either a time value or a date-time string."
84 (when (stringp time)
85 ;; Convert date strings to internal time.
86 (setq time (date-to-time time)))
87 (let* ((current (current-time))
88 (rest (when (< (nth 1 current) (nth 1 time))
89 (expt 2 16))))
90 (list (- (+ (car current) (if rest -1 0)) (car time))
91 (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
92
93 ;;;###autoload
94 (defalias 'subtract-time 'time-subtract)
95
96 ;;;###autoload
97 (defun time-subtract (t1 t2)
98 "Subtract two time values.
99 Return the difference in the format of a time value."
100 (let ((borrow (< (cadr t1) (cadr t2))))
101 (list (- (car t1) (car t2) (if borrow 1 0))
102 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
103
104 ;;;###autoload
105 (defun time-add (t1 t2)
106 "Add two time values. One should represent a time difference."
107 (let ((high (car t1))
108 (low (if (consp (cdr t1)) (nth 1 t1) (cdr t1)))
109 (micro (if (numberp (car-safe (cdr-safe (cdr t1))))
110 (nth 2 t1)
111 0))
112 (high2 (car t2))
113 (low2 (if (consp (cdr t2)) (nth 1 t2) (cdr t2)))
114 (micro2 (if (numberp (car-safe (cdr-safe (cdr t2))))
115 (nth 2 t2)
116 0)))
117 ;; Add
118 (setq micro (+ micro micro2))
119 (setq low (+ low low2))
120 (setq high (+ high high2))
121
122 ;; Normalize
123 ;; `/' rounds towards zero while `mod' returns a positive number,
124 ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))).
125 (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0)))
126 (setq micro (mod micro 1000000))
127 (setq high (+ high (/ low 65536) (if (< low 0) -1 0)))
128 (setq low (logand low 65535))
129
130 (list high low micro)))
131
132 ;;;###autoload
133 (defun date-to-day (date)
134 "Return the number of days between year 1 and DATE.
135 DATE should be a date-time string."
136 (time-to-days (date-to-time date)))
137
138 ;;;###autoload
139 (defun days-between (date1 date2)
140 "Return the number of days between DATE1 and DATE2.
141 DATE1 and DATE2 should be date-time strings."
142 (- (date-to-day date1) (date-to-day date2)))
143
144 ;;;###autoload
145 (defun date-leap-year-p (year)
146 "Return t if YEAR is a leap year."
147 (or (and (zerop (% year 4))
148 (not (zerop (% year 100))))
149 (zerop (% year 400))))
150
151 ;;;###autoload
152 (defun time-to-day-in-year (time)
153 "Return the day number within the year corresponding to TIME."
154 (let* ((tim (decode-time time))
155 (month (nth 4 tim))
156 (day (nth 3 tim))
157 (year (nth 5 tim))
158 (day-of-year (+ day (* 31 (1- month)))))
159 (when (> month 2)
160 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
161 (when (date-leap-year-p year)
162 (setq day-of-year (1+ day-of-year))))
163 day-of-year))
164
165 ;;;###autoload
166 (defun time-to-days (time)
167 "The number of days between the Gregorian date 0001-12-31bce and TIME.
168 TIME should be a time value.
169 The Gregorian date Sunday, December 31, 1bce is imaginary."
170 (let* ((tim (decode-time time))
171 (month (nth 4 tim))
172 (day (nth 3 tim))
173 (year (nth 5 tim)))
174 (+ (time-to-day-in-year time) ; Days this year
175 (* 365 (1- year)) ; + Days in prior years
176 (/ (1- year) 4) ; + Julian leap years
177 (- (/ (1- year) 100)) ; - century years
178 (/ (1- year) 400)))) ; + Gregorian leap years
179
180 (defun time-to-number-of-days (time)
181 "Return the number of days represented by TIME.
182 The number of days will be returned as a floating point number."
183 (/ (+ (* 1.0 65536 (car time)) (cadr time)) (* 60 60 24)))
184
185 ;;;###autoload
186 (defun safe-date-to-time (date)
187 "Parse a string that represents a date-time and return a time value.
188 If DATE is malformed, return a time value of zeros."
189 (condition-case ()
190 (date-to-time date)
191 (error '(0 0))))
192
193 (provide 'time-date)
194
195 ;;; arch-tag: addcf07b-b20a-465b-af72-550b8ac5190f
196 ;;; time-date.el ends here