Path: utzoo!attcan!uunet!husc6!mailrus!uwmcsd1!ig!agate!garnet!weemba From: weemba@garnet.berkeley.edu (Matthew P Wiener) Newsgroups: comp.emacs Subject: Re: insert time/date and signature Message-ID: <14107@agate.BERKELEY.EDU> Date: 10 Sep 88 12:25:21 GMT References: <29477@bbn.COM> <7243@haddock.ima.isc.com> Sender: usenet@agate.BERKELEY.EDU Reply-To: weemba@garnet.berkeley.edu (Matthew P Wiener) Organization: Brahms Gang Posting Central Lines: 81 In-reply-to: karl@haddock.ima.isc.com (Karl Heuer) In article <7243@haddock.ima.isc.com>, karl@haddock (Karl Heuer) writes: >It would be better yet if, instead of having to reverse-engineer the string >format, we could just invoke an elisp equivalent to localtime(). You're right. Failing that, I have the following functions, currently unused, from Gnews: gnews-time Return current time in ((YY MM DD).(HH MM SS)) format. gnews-time-form Convert a list L in the form (YY MM DD) into "YYMMDD". gnews-date-back From a date L in (YY MM DD) format, return the list for N days earlier. gnews-date-forward From a date L in (YY MM DD) format, return the list for N days later. ---------------------------------------------------------------------- ;;; Copyright (C) 1987 by Matthew P Wiener; all rights reserved. ;;; These functions are distributed as described in the GNU Emacs ;;; General Public license. (defun gnews-time () "Return current time in ((YY MM DD).(HH MM SS)) format." ;; This assumes (current-time-string) returns ;; something like: "Sun Jan 10 00:00:00 1988" (let* ((months '(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)) (time (current-time-string)) (mm (read-from-string time 4)) (dd (read-from-string time (cdr mm))) (d (car dd)) (di (cdr dd)) (h (read (substring time (+ 0 di) (+ 2 di)))) (m (read (substring time (+ 3 di) (+ 5 di)))) (s (read (substring time (+ 6 di) (+ 8 di)))) (y (read (substring time (+ 11 di) (+ 13 di)))) (mo (- 13 (length (memq (car mm) months))))) (cons (list y mo d) (list h m s)))) (defun gnews-time-form (l) ; RLS "Convert a list L in the form (YY MM DD) into \"YYMMDD\"." (apply 'format "%02d%02d%02d" l)) (defun gnews-date-back (l n) "From a date L in (YY MM DD) format, return the list for N days earlier. As in NNTP, we assume the 2-digit year YY is centered on 2000." (let* ((y (car l)) (m (gnadr l)) (p (1- m)) (d (gnaddr l)) (z '(31 28 31 30 31 30 31 31 30 31 30 31))) (while (<= d n) (setq n (- n d) m (if (< 0 p) p (setq y (1- y)) 12) p (1- m) d (nth p z)) (if (= y -1) (setq y 99)) (if (and (= m 2) (zerop (% y 4)) (not (zerop y))) (setq d (1+ d)))) (list y m (- d n)))) (defun gnews-date-forward (l n) "From a date L in (YY MM DD) format, return the list for N days later. As in NNTP, we assume the 2-digit year YY is centered on 2000." (let* ((y (car l)) (m (gnadr l)) (p (1+ m)) (z '(31 28 31 30 31 30 31 31 30 31 30 31)) (d (gnaddr l)) (c (nth (1- m) z)) (e (+ n d))) (if (and (= m 2) (zerop (% y 4)) (not (zerop y))) (setq c (1+ c))) (while (< c e) (setq e (- e c) m (if (< p 13) p (setq y (1+ y)) 1) p (1+ m) c (nth (1- m) z)) (if (= y 100) (setq y 0)) (if (and (= m 2) (zerop (% y 4)) (not (zerop y))) (setq c (1+ c)))) (list y m e))) ucbvax!garnet!weemba Matthew P Wiener/Brahms Gang/Berkeley CA 94720 "Nil sounds like a lot of kopins! I never got paid nil before!" --Groo