Mercurial > emacs
annotate lisp/gnus/time-date.el @ 35688:8b295a5d2192
(imenu-syntax-alist): Add autoload cookie for
the `make-variable-buffer-local'.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Mon, 29 Jan 2001 15:50:20 +0000 |
parents | 940adf5fe1c0 |
children | a26d9b55abb6 |
rev | line source |
---|---|
31717 | 1 ;;; time-date.el --- Date and time handling functions |
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) | |
35 "Convert DATE into time." | |
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) | |
49 "Convert TIME to a floating point number." | |
50 (+ (* (car time) 65536.0) | |
51 (cadr time) | |
52 (/ (or (nth 2 time) 0) 1000000.0))) | |
53 | |
54 (defun seconds-to-time (seconds) | |
55 "Convert SECONDS (a floating point number) to an Emacs time structure." | |
56 (list (floor seconds 65536) | |
57 (floor (mod seconds 65536)) | |
58 (floor (* (- seconds (ffloor seconds)) 1000000)))) | |
59 | |
60 (defun time-less-p (t1 t2) | |
61 "Say whether time T1 is less than time T2." | |
62 (or (< (car t1) (car t2)) | |
63 (and (= (car t1) (car t2)) | |
64 (< (nth 1 t1) (nth 1 t2))))) | |
65 | |
66 (defun days-to-time (days) | |
67 "Convert DAYS into time." | |
68 (let* ((seconds (* 1.0 days 60 60 24)) | |
69 (rest (expt 2 16)) | |
70 (ms (condition-case nil (floor (/ seconds rest)) | |
71 (range-error (expt 2 16))))) | |
72 (list ms (condition-case nil (round (- seconds (* ms rest))) | |
73 (range-error (expt 2 16)))))) | |
74 | |
75 (defun time-since (time) | |
76 "Return the time since TIME, which is either an internal time or a date." | |
77 (when (stringp time) | |
78 ;; Convert date strings to internal time. | |
79 (setq time (date-to-time time))) | |
80 (let* ((current (current-time)) | |
81 (rest (when (< (nth 1 current) (nth 1 time)) | |
82 (expt 2 16)))) | |
83 (list (- (+ (car current) (if rest -1 0)) (car time)) | |
84 (- (+ (or rest 0) (nth 1 current)) (nth 1 time))))) | |
85 | |
86 (defun subtract-time (t1 t2) | |
87 "Subtract two internal times." | |
88 (let ((borrow (< (cadr t1) (cadr t2)))) | |
89 (list (- (car t1) (car t2) (if borrow 1 0)) | |
90 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))) | |
91 | |
92 (defun date-to-day (date) | |
93 "Return the number of days between year 1 and DATE." | |
94 (time-to-days (date-to-time date))) | |
95 | |
96 (defun days-between (date1 date2) | |
97 "Return the number of days between DATE1 and DATE2." | |
98 (- (date-to-day date1) (date-to-day date2))) | |
99 | |
100 (defun date-leap-year-p (year) | |
101 "Return t if YEAR is a leap year." | |
102 (or (and (zerop (% year 4)) | |
103 (not (zerop (% year 100)))) | |
104 (zerop (% year 400)))) | |
105 | |
106 (defun time-to-day-in-year (time) | |
107 "Return the day number within the year of the date month/day/year." | |
108 (let* ((tim (decode-time time)) | |
109 (month (nth 4 tim)) | |
110 (day (nth 3 tim)) | |
111 (year (nth 5 tim)) | |
112 (day-of-year (+ day (* 31 (1- month))))) | |
113 (when (> month 2) | |
114 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10))) | |
115 (when (date-leap-year-p year) | |
116 (setq day-of-year (1+ day-of-year)))) | |
117 day-of-year)) | |
118 | |
119 (defun time-to-days (time) | |
120 "The number of days between the Gregorian date 0001-12-31bce and TIME. | |
121 The Gregorian date Sunday, December 31, 1bce is imaginary." | |
122 (let* ((tim (decode-time time)) | |
123 (month (nth 4 tim)) | |
124 (day (nth 3 tim)) | |
125 (year (nth 5 tim))) | |
126 (+ (time-to-day-in-year time) ; Days this year | |
127 (* 365 (1- year)) ; + Days in prior years | |
128 (/ (1- year) 4) ; + Julian leap years | |
129 (- (/ (1- year) 100)) ; - century years | |
130 (/ (1- year) 400)))) ; + Gregorian leap years | |
131 | |
132 ;;;###autoload | |
133 (defun safe-date-to-time (date) | |
134 "Parse DATE and return a time structure. | |
135 If DATE is malformed, a zero time will be returned." | |
136 (condition-case () | |
137 (date-to-time date) | |
138 (error '(0 0)))) | |
139 | |
140 (provide 'time-date) | |
141 | |
142 ;;; time-date.el ends here |