annotate lisp/calendar/time-date.el @ 88336:f6a3624b4de6

*** empty log message ***
author Alex Schroeder <alex@gnu.org>
date Tue, 11 Apr 2006 13:29:07 +0000
parents d7ddb3e565de
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
1 ;;; time-date.el --- Date and time handling functions
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
2
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
4 ;; Free Software Foundation, Inc.
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
5
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
7 ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp>
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
8 ;; Keywords: mail news util
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
9
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
11
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
13 ;; it under the terms of the GNU General Public License as published by
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
15 ;; any later version.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
16
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful,
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
20 ;; GNU General Public License for more details.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
21
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
25 ;; Boston, MA 02110-1301, USA.
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
26
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
27 ;;; Commentary:
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
28
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
29 ;; Time values come in three formats. The oldest format is a cons
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
30 ;; cell of the form (HIGH . LOW). This format is obsolete, but still
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
31 ;; supported. The two other formats are the lists (HIGH LOW) and
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
32 ;; (HIGH LOW MICRO). The first two formats specify HIGH * 2^16 + LOW
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
33 ;; seconds; the third format specifies HIGH * 2^16 + LOW + MICRO /
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
34 ;; 1000000 seconds. We should have 0 <= MICRO < 1000000 and 0 <= LOW
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
35 ;; < 2^16. If the time value represents a point in time, then HIGH is
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
36 ;; nonnegative. If the time value is a time difference, then HIGH can
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
37 ;; be negative as well. The macro `with-decoded-time-value' and the
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
38 ;; function `encode-time-value' make it easier to deal with these
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
39 ;; three formats. See `time-subtract' for an example of how to use
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
40 ;; them.
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
41
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
42 ;;; Code:
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
43
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
44 (defmacro with-decoded-time-value (varlist &rest body)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
45 "Decode a time value and bind it according to VARLIST, then eval BODY.
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
46
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
47 The value of the last form in BODY is returned.
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
48
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
49 Each element of the list VARLIST is a list of the form
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
50 \(HIGH-SYMBOL LOW-SYMBOL MICRO-SYMBOL [TYPE-SYMBOL] TIME-VALUE).
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
51 The time value TIME-VALUE is decoded and the result it bound to
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
52 the symbols HIGH-SYMBOL, LOW-SYMBOL and MICRO-SYMBOL.
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
53
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
54 The optional TYPE-SYMBOL is bound to the type of the time value.
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
55 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
56 LOW), and type 3 is the list (HIGH LOW MICRO)."
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
57 (declare (indent 1)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
58 (debug ((&rest (symbolp symbolp symbolp &or [symbolp form] form))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
59 body)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
60 (if varlist
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
61 (let* ((elt (pop varlist))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
62 (high (pop elt))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
63 (low (pop elt))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
64 (micro (pop elt))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
65 (type (unless (eq (length elt) 1)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
66 (pop elt)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
67 (time-value (car elt))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
68 (gensym (make-symbol "time")))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
69 `(let* ,(append `((,gensym ,time-value)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
70 (,high (pop ,gensym))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
71 ,low ,micro)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
72 (when type `(,type)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
73 (if (consp ,gensym)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
74 (progn
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
75 (setq ,low (pop ,gensym))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
76 (if ,gensym
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
77 ,(append `(setq ,micro (car ,gensym))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
78 (when type `(,type 2)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
79 ,(append `(setq ,micro 0)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
80 (when type `(,type 1)))))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
81 ,(append `(setq ,low ,gensym ,micro 0)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
82 (when type `(,type 0))))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
83 (with-decoded-time-value ,varlist ,@body)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
84 `(progn ,@body)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
85
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
86 (defun encode-time-value (high low micro type)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
87 "Encode HIGH, LOW, and MICRO into a time value of type TYPE.
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
88 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH LOW),
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
89 and type 3 is the list (HIGH LOW MICRO)."
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
90 (cond
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
91 ((eq type 0) (cons high low))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
92 ((eq type 1) (list high low))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
93 ((eq type 2) (list high low micro))))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
94
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
95 (autoload 'parse-time-string "parse-time")
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
96 (autoload 'timezone-make-date-arpa-standard "timezone")
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
97
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
98 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
99 (defun date-to-time (date)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
100 "Parse a string that represents a date-time and return a time value."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
101 (condition-case ()
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
102 (apply 'encode-time
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
103 (parse-time-string
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
104 ;; `parse-time-string' isn't sufficiently general or
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
105 ;; robust. It fails to grok some of the formats that
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
106 ;; timezone does (e.g. dodgy post-2000 stuff from some
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
107 ;; Elms) and either fails or returns bogus values. Lars
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
108 ;; reverted this change, but that loses non-trivially
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
109 ;; often for me. -- fx
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
110 (timezone-make-date-arpa-standard date)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
111 (error (error "Invalid date: %s" date))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
112
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
113 ;;;###autoload
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
114 (defun time-to-seconds (time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
115 "Convert time value TIME to a floating point number.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
116 You can use `float-time' instead."
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
117 (with-decoded-time-value ((high low micro time))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
118 (+ (* 1.0 high 65536)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
119 low
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
120 (/ micro 1000000.0))))
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
121
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
122 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
123 (defun seconds-to-time (seconds)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
124 "Convert SECONDS (a floating point number) to a time value."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
125 (list (floor seconds 65536)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
126 (floor (mod seconds 65536))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
127 (floor (* (- seconds (ffloor seconds)) 1000000))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
128
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
129 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
130 (defun time-less-p (t1 t2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
131 "Say whether time value T1 is less than time value T2."
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
132 (with-decoded-time-value ((high1 low1 micro1 t1)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
133 (high2 low2 micro2 t2))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
134 (or (< high1 high2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
135 (and (= high1 high2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
136 (or (< low1 low2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
137 (and (= low1 low2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
138 (< micro1 micro2)))))))
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
139
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
140 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
141 (defun days-to-time (days)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
142 "Convert DAYS into a time value."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
143 (let* ((seconds (* 1.0 days 60 60 24))
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
144 (high (condition-case nil (floor (/ seconds 65536))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
145 (range-error most-positive-fixnum))))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
146 (list high (condition-case nil (floor (- seconds (* 1.0 high 65536)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
147 (range-error 65535)))))
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
148
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
149 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
150 (defun time-since (time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
151 "Return the time elapsed since TIME.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
152 TIME should be either a time value or a date-time string."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
153 (when (stringp time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
154 ;; Convert date strings to internal time.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
155 (setq time (date-to-time time)))
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
156 (time-subtract (current-time) time))
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
157
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
158 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
159 (defalias 'subtract-time 'time-subtract)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
160
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
161 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
162 (defun time-subtract (t1 t2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
163 "Subtract two time values.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
164 Return the difference in the format of a time value."
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
165 (with-decoded-time-value ((high low micro type t1)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
166 (high2 low2 micro2 type2 t2))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
167 (setq high (- high high2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
168 low (- low low2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
169 micro (- micro micro2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
170 type (max type type2))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
171 (when (< micro 0)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
172 (setq low (1- low)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
173 micro (+ micro 1000000)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
174 (when (< low 0)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
175 (setq high (1- high)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
176 low (+ low 65536)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
177 (encode-time-value high low micro type)))
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
178
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
179 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
180 (defun time-add (t1 t2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
181 "Add two time values. One should represent a time difference."
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
182 (with-decoded-time-value ((high low micro type t1)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
183 (high2 low2 micro2 type2 t2))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
184 (setq high (+ high high2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
185 low (+ low low2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
186 micro (+ micro micro2)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
187 type (max type type2))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
188 (when (>= micro 1000000)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
189 (setq low (1+ low)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
190 micro (- micro 1000000)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
191 (when (>= low 65536)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
192 (setq high (1+ high)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
193 low (- low 65536)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
194 (encode-time-value high low micro type)))
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
195
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
196 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
197 (defun date-to-day (date)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
198 "Return the number of days between year 1 and DATE.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
199 DATE should be a date-time string."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
200 (time-to-days (date-to-time date)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
201
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
202 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
203 (defun days-between (date1 date2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
204 "Return the number of days between DATE1 and DATE2.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
205 DATE1 and DATE2 should be date-time strings."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
206 (- (date-to-day date1) (date-to-day date2)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
207
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
208 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
209 (defun date-leap-year-p (year)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
210 "Return t if YEAR is a leap year."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
211 (or (and (zerop (% year 4))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
212 (not (zerop (% year 100))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
213 (zerop (% year 400))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
214
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
215 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
216 (defun time-to-day-in-year (time)
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
217 "Return the day number within the year corresponding to TIME."
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
218 (let* ((tim (decode-time time))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
219 (month (nth 4 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
220 (day (nth 3 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
221 (year (nth 5 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
222 (day-of-year (+ day (* 31 (1- month)))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
223 (when (> month 2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
224 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
225 (when (date-leap-year-p year)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
226 (setq day-of-year (1+ day-of-year))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
227 day-of-year))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
228
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
229 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
230 (defun time-to-days (time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
231 "The number of days between the Gregorian date 0001-12-31bce and TIME.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
232 TIME should be a time value.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
233 The Gregorian date Sunday, December 31, 1bce is imaginary."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
234 (let* ((tim (decode-time time))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
235 (month (nth 4 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
236 (day (nth 3 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
237 (year (nth 5 tim)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
238 (+ (time-to-day-in-year time) ; Days this year
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
239 (* 365 (1- year)) ; + Days in prior years
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
240 (/ (1- year) 4) ; + Julian leap years
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
241 (- (/ (1- year) 100)) ; - century years
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
242 (/ (1- year) 400)))) ; + Gregorian leap years
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
243
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
244 (defun time-to-number-of-days (time)
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
245 "Return the number of days represented by TIME.
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
246 The number of days will be returned as a floating point number."
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
247 (/ (time-to-seconds time) (* 60 60 24)))
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
248
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
249 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
250 (defun safe-date-to-time (date)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
251 "Parse a string that represents a date-time and return a time value.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
252 If DATE is malformed, return a time value of zeros."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
253 (condition-case ()
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
254 (date-to-time date)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
255 (error '(0 0))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
256
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
257 (provide 'time-date)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
258
88155
d7ddb3e565de sync with trunk
Henrik Enberg <henrik.enberg@telia.com>
parents: 43049
diff changeset
259 ;;; arch-tag: addcf07b-b20a-465b-af72-550b8ac5190f
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
260 ;;; time-date.el ends here