Mercurial > emacs
annotate lisp/gnus/time-date.el @ 43006:e8ac4a1176a4
Add autoload cookies. Many doc fixes.
(time-add): New function.
(time-subtract): Renamed from subtract-time.
(subtract-time): New alias for time-subtract.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 27 Jan 2002 23:30:10 +0000 |
parents | a26d9b55abb6 |
children |
rev | line source |
---|---|
38413
a26d9b55abb6
Some fixes to follow coding conventions in files from Gnus.
Pavel Janík <Pavel@Janik.cz>
parents:
33376
diff
changeset
|
1 ;;; time-date.el --- date and time handling functions |
31717 | 2 ;; Copyright (C) 1998, 1999, 2000 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 | |
33376
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
31 (autoload 'timezone-make-date-arpa-standard "timezone") |
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
32 |
31717 | 33 ;;;###autoload |
34 (defun date-to-time (date) | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
35 "Parse a string that represents a date-time and return a time value." |
31717 | 36 (condition-case () |
33376
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
37 (apply 'encode-time |
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
38 (parse-time-string |
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
39 ;; `parse-time-string' isn't sufficiently general or |
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
40 ;; robust. It fails to grok some of the formats that |
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
41 ;; timzeone does (e.g. dodgy post-2000 stuff from some |
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
42 ;; Elms) and either fails or returns bogus values. Lars |
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
43 ;; reverted this change, but that loses non-trivially |
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
44 ;; often for me. -- fx |
940adf5fe1c0
(timezone-make-date-arpa-standard): Autoload.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
45 (timezone-make-date-arpa-standard date))) |
31717 | 46 (error (error "Invalid date: %s" date)))) |
47 | |
48 (defun time-to-seconds (time) | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
49 "Convert time value TIME to a floating point number. |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
50 You can use `float-time' instead." |
31717 | 51 (+ (* (car time) 65536.0) |
52 (cadr time) | |
53 (/ (or (nth 2 time) 0) 1000000.0))) | |
54 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
55 ;;;###autoload |
31717 | 56 (defun seconds-to-time (seconds) |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
57 "Convert SECONDS (a floating point number) to a time value." |
31717 | 58 (list (floor seconds 65536) |
59 (floor (mod seconds 65536)) | |
60 (floor (* (- seconds (ffloor seconds)) 1000000)))) | |
61 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
62 ;;;###autoload |
31717 | 63 (defun time-less-p (t1 t2) |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
64 "Say whether time value T1 is less than time value T2." |
31717 | 65 (or (< (car t1) (car t2)) |
66 (and (= (car t1) (car t2)) | |
67 (< (nth 1 t1) (nth 1 t2))))) | |
68 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
69 ;;;###autoload |
31717 | 70 (defun days-to-time (days) |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
71 "Convert DAYS into a time value." |
31717 | 72 (let* ((seconds (* 1.0 days 60 60 24)) |
73 (rest (expt 2 16)) | |
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)))))) | |
78 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
79 ;;;###autoload |
31717 | 80 (defun time-since (time) |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
81 "Return the time elapsed since TIME. |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
82 TIME should be either a time value or a date-time string." |
31717 | 83 (when (stringp time) |
84 ;; Convert date strings to internal time. | |
85 (setq time (date-to-time time))) | |
86 (let* ((current (current-time)) | |
87 (rest (when (< (nth 1 current) (nth 1 time)) | |
88 (expt 2 16)))) | |
89 (list (- (+ (car current) (if rest -1 0)) (car time)) | |
90 (- (+ (or rest 0) (nth 1 current)) (nth 1 time))))) | |
91 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
92 ;;;###autoload |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
93 (defalias 'subtract-time 'time-subtract) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
94 |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
95 ;;;###autoload |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
96 (defun time-subtract (t1 t2) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
97 "Subtract two time values. |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
98 Return the difference in the format of a time value." |
31717 | 99 (let ((borrow (< (cadr t1) (cadr t2)))) |
100 (list (- (car t1) (car t2) (if borrow 1 0)) | |
101 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))) | |
102 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
103 ;;;###autoload |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
104 (defun time-add (t1 t2) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
105 "Add two time values. One should represent a time difference." |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
106 (let ((high (car t1)) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
107 (low (if (consp (cdr t1)) (nth 1 t1) (cdr t1))) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
108 (micro (if (numberp (car-safe (cdr-safe (cdr t1)))) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
109 (nth 2 t1) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
110 0)) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
111 (high2 (car t2)) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
112 (low2 (if (consp (cdr t2)) (nth 1 t2) (cdr t2))) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
113 (micro2 (if (numberp (car-safe (cdr-safe (cdr t2)))) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
114 (nth 2 t2) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
115 0))) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
116 ;; Add |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
117 (setq micro (+ micro micro2)) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
118 (setq low (+ low low2)) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
119 (setq high (+ high high2)) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
120 |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
121 ;; Normalize |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
122 ;; `/' rounds towards zero while `mod' returns a positive number, |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
123 ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))). |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
124 (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0))) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
125 (setq micro (mod micro 1000000)) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
126 (setq high (+ high (/ low 65536) (if (< low 0) -1 0))) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
127 (setq low (logand low 65535)) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
128 |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
129 (list high low micro))) |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
130 |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
131 ;;;###autoload |
31717 | 132 (defun date-to-day (date) |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
133 "Return the number of days between year 1 and DATE. |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
134 DATE should be a date-time string." |
31717 | 135 (time-to-days (date-to-time date))) |
136 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
137 ;;;###autoload |
31717 | 138 (defun days-between (date1 date2) |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
139 "Return the number of days between DATE1 and DATE2. |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
140 DATE1 and DATE2 should be date-time strings." |
31717 | 141 (- (date-to-day date1) (date-to-day date2))) |
142 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
143 ;;;###autoload |
31717 | 144 (defun date-leap-year-p (year) |
145 "Return t if YEAR is a leap year." | |
146 (or (and (zerop (% year 4)) | |
147 (not (zerop (% year 100)))) | |
148 (zerop (% year 400)))) | |
149 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
150 ;;;###autoload |
31717 | 151 (defun time-to-day-in-year (time) |
152 "Return the day number within the year of the date month/day/year." | |
153 (let* ((tim (decode-time time)) | |
154 (month (nth 4 tim)) | |
155 (day (nth 3 tim)) | |
156 (year (nth 5 tim)) | |
157 (day-of-year (+ day (* 31 (1- month))))) | |
158 (when (> month 2) | |
159 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10))) | |
160 (when (date-leap-year-p year) | |
161 (setq day-of-year (1+ day-of-year)))) | |
162 day-of-year)) | |
163 | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
164 ;;;###autoload |
31717 | 165 (defun time-to-days (time) |
166 "The number of days between the Gregorian date 0001-12-31bce and TIME. | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
167 TIME should be a time value. |
31717 | 168 The Gregorian date Sunday, December 31, 1bce is imaginary." |
169 (let* ((tim (decode-time time)) | |
170 (month (nth 4 tim)) | |
171 (day (nth 3 tim)) | |
172 (year (nth 5 tim))) | |
173 (+ (time-to-day-in-year time) ; Days this year | |
174 (* 365 (1- year)) ; + Days in prior years | |
175 (/ (1- year) 4) ; + Julian leap years | |
176 (- (/ (1- year) 100)) ; - century years | |
177 (/ (1- year) 400)))) ; + Gregorian leap years | |
178 | |
179 ;;;###autoload | |
180 (defun safe-date-to-time (date) | |
43006
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
181 "Parse a string that represents a date-time and return a time value. |
e8ac4a1176a4
Add autoload cookies. Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
38413
diff
changeset
|
182 If DATE is malformed, return a time value of zeros." |
31717 | 183 (condition-case () |
184 (date-to-time date) | |
185 (error '(0 0)))) | |
186 | |
187 (provide 'time-date) | |
188 | |
189 ;;; time-date.el ends here |