annotate lisp/calendar/time-date.el @ 59646:763e003c8226

*** empty log message ***
author Kim F. Storm <storm@cua.dk>
date Wed, 19 Jan 2005 23:16:39 +0000
parents 55fd4f77387a
children 608b5e008a8b cce1c0ee76ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
1 ;;; time-date.el --- date and time handling functions
56927
55fd4f77387a Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents: 56797
diff changeset
2 ;; Copyright (C) 1998, 1999, 2000, 2004 Free Software Foundation, Inc.
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
3
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
4 ;; 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
5 ;; 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
6 ;; Keywords: mail news util
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
7
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
8 ;; 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
9
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
10 ;; 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
11 ;; 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
12 ;; 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
13 ;; any later version.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
14
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
15 ;; 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
16 ;; 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
17 ;; 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
18 ;; 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
19
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
20 ;; 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
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
23 ;; Boston, MA 02111-1307, USA.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
24
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
25 ;;; Commentary:
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 ;;; Code:
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
28
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
29 (require 'parse-time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
30
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
31 (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
32
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
33 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
34 (defun date-to-time (date)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
35 "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
36 (condition-case ()
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
37 (apply 'encode-time
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
38 (parse-time-string
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
39 ;; `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
40 ;; robust. It fails to grok some of the formats that
56927
55fd4f77387a Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents: 56797
diff changeset
41 ;; 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
42 ;; 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
43 ;; 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
44 ;; often for me. -- fx
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
45 (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
46 (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
47
56797
2383026ff0d9 (time-to-seconds): Add autoload cookies.
Masatake YAMATO <jet@gyve.org>
parents: 55002
diff changeset
48 ;;;###autoload
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
49 (defun time-to-seconds (time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
50 "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
51 You can use `float-time' instead."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
52 (+ (* (car time) 65536.0)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
53 (cadr time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
54 (/ (or (nth 2 time) 0) 1000000.0)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
55
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
56 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
57 (defun seconds-to-time (seconds)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
58 "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
59 (list (floor seconds 65536)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
60 (floor (mod seconds 65536))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
61 (floor (* (- seconds (ffloor seconds)) 1000000))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
62
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
63 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
64 (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
65 "Say whether time value T1 is less than time value T2."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
66 (or (< (car t1) (car t2))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
67 (and (= (car t1) (car t2))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
68 (< (nth 1 t1) (nth 1 t2)))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
69
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
70 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
71 (defun days-to-time (days)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
72 "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
73 (let* ((seconds (* 1.0 days 60 60 24))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
74 (rest (expt 2 16))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
75 (ms (condition-case nil (floor (/ seconds rest))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
76 (range-error (expt 2 16)))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
77 (list ms (condition-case nil (round (- seconds (* ms rest)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
78 (range-error (expt 2 16))))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
79
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
80 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
81 (defun time-since (time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
82 "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
83 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
84 (when (stringp time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
85 ;; 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
86 (setq time (date-to-time time)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
87 (let* ((current (current-time))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
88 (rest (when (< (nth 1 current) (nth 1 time))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
89 (expt 2 16))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
90 (list (- (+ (car current) (if rest -1 0)) (car time))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
91 (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
92
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
93 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
94 (defalias 'subtract-time 'time-subtract)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
95
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
96 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
97 (defun time-subtract (t1 t2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
98 "Subtract two time values.
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
99 Return the difference in the format of a time value."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
100 (let ((borrow (< (cadr t1) (cadr t2))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
101 (list (- (car t1) (car t2) (if borrow 1 0))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
102 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
103
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
104 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
105 (defun time-add (t1 t2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
106 "Add two time values. One should represent a time difference."
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
107 (let ((high (car t1))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
108 (low (if (consp (cdr t1)) (nth 1 t1) (cdr t1)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
109 (micro (if (numberp (car-safe (cdr-safe (cdr t1))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
110 (nth 2 t1)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
111 0))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
112 (high2 (car t2))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
113 (low2 (if (consp (cdr t2)) (nth 1 t2) (cdr t2)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
114 (micro2 (if (numberp (car-safe (cdr-safe (cdr t2))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
115 (nth 2 t2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
116 0)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
117 ;; Add
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
118 (setq micro (+ micro micro2))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
119 (setq low (+ low low2))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
120 (setq high (+ high high2))
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 ;; Normalize
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
123 ;; `/' rounds towards zero while `mod' returns a positive number,
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
124 ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))).
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
125 (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
126 (setq micro (mod micro 1000000))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
127 (setq high (+ high (/ low 65536) (if (< low 0) -1 0)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
128 (setq low (logand low 65535))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
129
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
130 (list high low micro)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
131
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
132 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
133 (defun date-to-day (date)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
134 "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
135 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
136 (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
137
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
138 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
139 (defun days-between (date1 date2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
140 "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
141 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
142 (- (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
143
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
144 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
145 (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
146 "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
147 (or (and (zerop (% year 4))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
148 (not (zerop (% year 100))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
149 (zerop (% year 400))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
150
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
151 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
152 (defun time-to-day-in-year (time)
55002
8e406d7e53be (time-to-day-in-year): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 52401
diff changeset
153 "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
154 (let* ((tim (decode-time time))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
155 (month (nth 4 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
156 (day (nth 3 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
157 (year (nth 5 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
158 (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
159 (when (> month 2)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
160 (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
161 (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
162 (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
163 day-of-year))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
164
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
165 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
166 (defun time-to-days (time)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
167 "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
168 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
169 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
170 (let* ((tim (decode-time time))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
171 (month (nth 4 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
172 (day (nth 3 tim))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
173 (year (nth 5 tim)))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
174 (+ (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
175 (* 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
176 (/ (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
177 (- (/ (1- year) 100)) ; - century years
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
178 (/ (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
179
56927
55fd4f77387a Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents: 56797
diff changeset
180 (defun time-to-number-of-days (time)
55fd4f77387a Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents: 56797
diff changeset
181 "Return the number of days represented by TIME.
55fd4f77387a Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents: 56797
diff changeset
182 The number of days will be returned as a floating point number."
55fd4f77387a Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents: 56797
diff changeset
183 (/ (+ (* 1.0 65536 (car time)) (cadr time)) (* 60 60 24)))
55fd4f77387a Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents: 56797
diff changeset
184
43049
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
185 ;;;###autoload
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
186 (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
187 "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
188 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
189 (condition-case ()
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
190 (date-to-time date)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
191 (error '(0 0))))
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
192
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
193 (provide 'time-date)
d94124a8eddf Moved time-date.el from lisp/gnus to lisp/calendar.
Eli Zaretskii <eliz@gnu.org>
parents:
diff changeset
194
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 43049
diff changeset
195 ;;; 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
196 ;;; time-date.el ends here