*** empty log message ***
[gnus] / lisp / time-date.el
1 ;;; time-date.el --- Date and time handling functions
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      Masanobu Umeda <umerin@mse.kyutech.ac.jp>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'parse-time)
28
29 (defun date-to-time (date)
30   "Convert DATE into time."
31   (condition-case ()
32       (apply 'encode-time (parse-time-string date))
33     (error (error "Invalid date: %s" date))))
34
35 (defun time-to-seconds (time)
36   "Convert TIME to a floating point number."
37   (+ (* (car time) 65536.0)
38      (cadr time)
39      (/ (or (caddr time) 0) 1000000.0)))
40
41 (defun seconds-to-time (seconds)
42   "Convert SECONDS (a floating point number) to an Emacs time structure."
43   (list (floor seconds 65536)
44         (floor (mod seconds 65536))
45         (floor (* (- seconds (ffloor seconds)) 1000000))))
46
47 (defun time-less-p (t1 t2)
48   "Say whether time T1 is less than time T2."
49   (or (< (car t1) (car t2))
50       (and (= (car t1) (car t2))
51            (< (nth 1 t1) (nth 1 t2)))))
52
53 (defun days-to-time (days)
54   "Convert DAYS into time."
55   (let* ((seconds (* 1.0 days 60 60 24))
56          (rest (expt 2 16))
57          (ms (condition-case nil (floor (/ seconds rest))
58                (range-error (expt 2 16)))))
59     (list ms (condition-case nil (round (- seconds (* ms rest)))
60                (range-error (expt 2 16))))))
61
62 (defun time-since (time)
63   "Return the time since TIME, which is either an internal time or a date."
64   (when (stringp time)
65     ;; Convert date strings to internal time.
66     (setq time (date-to-time time)))
67   (let* ((current (current-time))
68          (rest (when (< (nth 1 current) (nth 1 time))
69                  (expt 2 16))))
70     (list (- (+ (car current) (if rest -1 0)) (car time))
71           (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
72
73 (defun subtract-time (t1 t2)
74   "Subtract two internal times."
75   (let ((borrow (< (cadr t1) (cadr t2))))
76     (list (- (car t1) (car t2) (if borrow 1 0))
77           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
78
79 (defun date-to-day (date)
80   "Return the number of days between year 1 and DATE."
81   (time-to-day (date-to-time date)))
82   
83 (defun days-between (date1 date2)
84   "Return the number of days between DATE1 and DATE2."
85   (- (date-to-day date1) (date-to-day date2)))
86
87 (defun date-leap-year-p (year)
88   "Return t if YEAR is a leap year."
89   (or (and (zerop (% year 4))
90            (not (zerop (% year 100))))
91       (zerop (% year 400))))
92
93 (defun time-to-day-in-year (time)
94   "Return the day number within the year of the date month/day/year."
95   (let* ((tim (decode-time time))
96          (month (nth 4 tim))
97          (day (nth 3 tim))
98          (year (nth 5 tim))
99          (day-of-year (+ day (* 31 (1- month)))))
100     (when (> month 2)
101       (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
102       (when (date-leap-year-p year)
103         (setq day-of-year (1+ day-of-year))))
104     day-of-year))
105
106 (defun time-to-day (time)
107   "The number of days between the Gregorian date 0001-12-31bce and TIME.
108 The Gregorian date Sunday, December 31, 1bce is imaginary."
109   (let* ((tim (decode-time time))
110          (month (nth 4 tim))
111          (day (nth 3 tim))
112          (year (nth 5 tim)))
113     (+ (time-to-day-in-year time)       ;       Days this year
114        (* 365 (1- year))                ;       + Days in prior years
115        (/ (1- year) 4)                  ;       + Julian leap years
116        (- (/ (1- year) 100))            ;       - century years
117        (/ (1- year) 400))))             ;       + Gregorian leap years
118
119 (defun safe-date-to-time (date)
120   "Parse DATE and return a time structure.
121 If DATE is malformed, a zero time will be returned."
122   (condition-case ()
123       (date-to-time date)
124     (error '(0 0))))
125
126 (provide 'time-date)
127
128 ;;; time-date.el ends here