(lw_separator_p): Init separator_p (bug with "-- some text").
[emacs.git] / lisp / gnus / time-date.el
blobba7f81a1ed1d01b37edb55639264f80992b3384a
1 ;;; time-date.el --- Date and time handling functions
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp>
6 ;; Keywords: mail news util
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
27 ;;; Code:
29 (require 'parse-time)
31 ;;;###autoload
32 (defun date-to-time (date)
33 "Convert DATE into time."
34 (condition-case ()
35 (apply 'encode-time (parse-time-string date))
36 (error (error "Invalid date: %s" date))))
38 (defun time-to-seconds (time)
39 "Convert TIME to a floating point number."
40 (+ (* (car time) 65536.0)
41 (cadr time)
42 (/ (or (nth 2 time) 0) 1000000.0)))
44 (defun seconds-to-time (seconds)
45 "Convert SECONDS (a floating point number) to an Emacs time structure."
46 (list (floor seconds 65536)
47 (floor (mod seconds 65536))
48 (floor (* (- seconds (ffloor seconds)) 1000000))))
50 (defun time-less-p (t1 t2)
51 "Say whether time T1 is less than time T2."
52 (or (< (car t1) (car t2))
53 (and (= (car t1) (car t2))
54 (< (nth 1 t1) (nth 1 t2)))))
56 (defun days-to-time (days)
57 "Convert DAYS into time."
58 (let* ((seconds (* 1.0 days 60 60 24))
59 (rest (expt 2 16))
60 (ms (condition-case nil (floor (/ seconds rest))
61 (range-error (expt 2 16)))))
62 (list ms (condition-case nil (round (- seconds (* ms rest)))
63 (range-error (expt 2 16))))))
65 (defun time-since (time)
66 "Return the time since TIME, which is either an internal time or a date."
67 (when (stringp time)
68 ;; Convert date strings to internal time.
69 (setq time (date-to-time time)))
70 (let* ((current (current-time))
71 (rest (when (< (nth 1 current) (nth 1 time))
72 (expt 2 16))))
73 (list (- (+ (car current) (if rest -1 0)) (car time))
74 (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
76 (defun subtract-time (t1 t2)
77 "Subtract two internal times."
78 (let ((borrow (< (cadr t1) (cadr t2))))
79 (list (- (car t1) (car t2) (if borrow 1 0))
80 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
82 (defun date-to-day (date)
83 "Return the number of days between year 1 and DATE."
84 (time-to-days (date-to-time date)))
86 (defun days-between (date1 date2)
87 "Return the number of days between DATE1 and DATE2."
88 (- (date-to-day date1) (date-to-day date2)))
90 (defun date-leap-year-p (year)
91 "Return t if YEAR is a leap year."
92 (or (and (zerop (% year 4))
93 (not (zerop (% year 100))))
94 (zerop (% year 400))))
96 (defun time-to-day-in-year (time)
97 "Return the day number within the year of the date month/day/year."
98 (let* ((tim (decode-time time))
99 (month (nth 4 tim))
100 (day (nth 3 tim))
101 (year (nth 5 tim))
102 (day-of-year (+ day (* 31 (1- month)))))
103 (when (> month 2)
104 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
105 (when (date-leap-year-p year)
106 (setq day-of-year (1+ day-of-year))))
107 day-of-year))
109 (defun time-to-days (time)
110 "The number of days between the Gregorian date 0001-12-31bce and TIME.
111 The Gregorian date Sunday, December 31, 1bce is imaginary."
112 (let* ((tim (decode-time time))
113 (month (nth 4 tim))
114 (day (nth 3 tim))
115 (year (nth 5 tim)))
116 (+ (time-to-day-in-year time) ; Days this year
117 (* 365 (1- year)) ; + Days in prior years
118 (/ (1- year) 4) ; + Julian leap years
119 (- (/ (1- year) 100)) ; - century years
120 (/ (1- year) 400)))) ; + Gregorian leap years
122 ;;;###autoload
123 (defun safe-date-to-time (date)
124 "Parse DATE and return a time structure.
125 If DATE is malformed, a zero time will be returned."
126 (condition-case ()
127 (date-to-time date)
128 (error '(0 0))))
130 (provide 'time-date)
132 ;;; time-date.el ends here