home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / clisp-li.000 / clisp-li / clisp-1996-07-22 / src / defs1.lsp < prev    next >
Encoding:
Text File  |  1996-06-05  |  27.0 KB  |  708 lines

  1. ;;;; Einige Definitionen von Standard-Funktionen in LISP
  2. ;;;; 1.8.1989, 2.9.1989, 8.10.1989
  3.  
  4. (in-package "LISP")
  5. (export '(doseq dohash #-UNIX *default-time-zone* default-directory dir))
  6. (in-package "SYSTEM")
  7.  
  8.  
  9. ;;; Funktionen fⁿr Symbole (Kapitel 10)
  10.  
  11. (defun copy-symbol (symbol &optional flag)
  12.                    ;; Common LISP, S. 169
  13.   (let ((sym (make-symbol (symbol-name symbol))))
  14.     (when flag
  15.       (when (boundp symbol) (set sym (%symbol-value symbol)))
  16.       (when (fboundp symbol) (sys::%putd sym (symbol-function symbol)))
  17.       (sys::%putplist sym (copy-list (symbol-plist symbol)))
  18.     )
  19.     sym
  20. ) )
  21.  
  22. (let ((gentemp-count 0))
  23.   (defun gentemp (&optional (prefix "T") (package *package*))
  24.                  ;; Common LISP, S. 170
  25.     (loop
  26.       (setq gentemp-count (1+ gentemp-count))
  27.       (multiple-value-bind (sym flag)
  28.         (intern
  29.           (string-concat prefix
  30.             (write-to-string gentemp-count :base 10 :radix nil :readably nil)
  31.           )
  32.           package
  33.         )
  34.         (unless flag (return sym))
  35. ) ) ) )
  36.  
  37.  
  38. ;;; Macros fⁿr Packages (Kapitel 11), S. 187-188
  39.  
  40. (defmacro do-symbols ((var &optional (packageform '*package*) (resultform nil))
  41.                       &body body &environment env)
  42.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  43.     (let ((packvar (gensym)))
  44.       `(BLOCK NIL
  45.          (LET ((,packvar ,packageform))
  46.            (LET ((,var NIL))
  47.              (DECLARE (IGNORABLE ,var) ,@declarations)
  48.              (SYSTEM::MAP-SYMBOLS
  49.                #'(LAMBDA (,var)
  50.                    ,@(if declarations `((DECLARE ,@declarations)) '())
  51.                    ,@body-rest
  52.                  )
  53.                ,packvar
  54.              )
  55.              ,resultform
  56.        ) ) )
  57. ) ) )
  58.  
  59. (defmacro do-external-symbols ((var &optional (packageform '*package*) (resultform nil))
  60.                                &body body &environment env)
  61.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  62.     (let ((packvar (gensym)))
  63.       `(BLOCK NIL
  64.          (LET ((,packvar ,packageform))
  65.            (LET ((,var NIL))
  66.              (DECLARE (IGNORABLE ,var) ,@declarations)
  67.              (SYSTEM::MAP-EXTERNAL-SYMBOLS
  68.                #'(LAMBDA (,var)
  69.                    ,@(if declarations `((DECLARE ,@declarations)) '())
  70.                    ,@body-rest
  71.                  )
  72.                ,packvar
  73.              )
  74.              ,resultform
  75.        ) ) )
  76. ) ) )
  77.  
  78. (defmacro do-all-symbols ((var &optional (resultform nil))
  79.                           &body body &environment env)
  80.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  81.     `(BLOCK NIL
  82.        (LET ((,var NIL))
  83.          (DECLARE (IGNORABLE ,var) ,@declarations)
  84.          (SYSTEM::MAP-ALL-SYMBOLS
  85.            #'(LAMBDA (,var)
  86.                ,@(if declarations `((DECLARE ,@declarations)) '())
  87.                ,@body-rest
  88.              )
  89.          )
  90.          ,resultform
  91.      ) )
  92. ) )
  93.  
  94. ;;; Modulverwaltung (Kapitel 11.8), CLTL S. 188
  95.  
  96. (defvar *modules* nil)
  97.  
  98. (defun provide (module-name)
  99.   (setq *modules* (adjoin (string module-name) *modules* :test #'string=))
  100. )
  101.  
  102. (defun require (module-name &optional (pathname nil p-given))
  103.   (unless (member (string module-name) *modules* :test #'string-equal)
  104.     (unless p-given (setq pathname (pathname module-name)))
  105.     (let (#-CLISP(*default-pathname-defaults* '#""))
  106.       (if (atom pathname) (load pathname) (mapcar #'load pathname))
  107.     )
  108. ) )
  109.  
  110.  
  111. ;;; Konstanten fⁿr Zahlen (Kapitel 12)
  112.  
  113. ; vgl. File INTLOG.TXT
  114. (defconstant boole-clr 0)
  115. (defconstant boole-set 15)
  116. (defconstant boole-1 10)
  117. (defconstant boole-2 12)
  118. (defconstant boole-c1 5)
  119. (defconstant boole-c2 3)
  120. (defconstant boole-and 8)
  121. (defconstant boole-ior 14)
  122. (defconstant boole-xor 6)
  123. (defconstant boole-eqv 9)
  124. (defconstant boole-nand 7)
  125. (defconstant boole-nor 1)
  126. (defconstant boole-andc1 4)
  127. (defconstant boole-andc2 2)
  128. (defconstant boole-orc1 13)
  129. (defconstant boole-orc2 11)
  130.  
  131. ; Zum Wiedereinlesen von BYTEs:
  132. (defun make-byte (&key size position) (byte size position))
  133.  
  134. ; X3J13 vote <79>
  135. (defconstant least-positive-normalized-short-float least-positive-short-float)
  136. (defconstant least-negative-normalized-short-float least-negative-short-float)
  137. (defconstant least-positive-normalized-single-float least-positive-single-float)
  138. (defconstant least-negative-normalized-single-float least-negative-single-float)
  139. (defconstant least-positive-normalized-double-float least-positive-double-float)
  140. (defconstant least-negative-normalized-double-float least-negative-double-float)
  141. (defconstant least-positive-normalized-long-float least-positive-long-float)
  142. (defconstant least-negative-normalized-long-float least-negative-long-float)
  143.  
  144.  
  145. ;;; Konstanten fⁿr Zeichen (Kapitel 13)
  146.  
  147. (defconstant char-code-limit 256)
  148. (defconstant char-font-limit 16)
  149. (defconstant char-bits-limit 16)
  150.                    ;; Common LISP, S. 233, 234
  151.  
  152. (defconstant char-control-bit 1)
  153. (defconstant char-meta-bit 2)
  154. (defconstant char-super-bit 4)
  155. (defconstant char-hyper-bit 8)
  156.                    ;; Common LISP, S. 243
  157.  
  158.  
  159. ;;; Funktionen fⁿr Sequences (Kapitel 14)
  160.  
  161. (defmacro doseq ((var seqform &optional resultform) &body body &environment env)
  162.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  163.     (let ((seqvar (gensym)))
  164.       `(BLOCK NIL
  165.          (LET ((,seqvar ,seqform))
  166.            (LET ((,var NIL))
  167.              (DECLARE (IGNORABLE ,var) ,@declarations)
  168.              (MAP NIL
  169.                   #'(LAMBDA (,var)
  170.                       ,@(if declarations `((DECLARE ,@declarations)) '())
  171.                       (TAGBODY ,@body-rest)
  172.                     )
  173.                   ,seqvar
  174.              )
  175.              ,resultform
  176.        ) ) )
  177. ) ) )
  178.  
  179.  
  180. ;;; Funktionen fⁿr Listen (Kapitel 15)
  181.  
  182. ; Hilfsversion von MEMBER, die das :KEY-Argument auch auf item anwendet:
  183. (defun sys::member1 (item list &rest rest &key test test-not key)
  184.   (declare (ignore test test-not))
  185.   (unless key (setq key #'identity))
  186.   (apply #'member (funcall key item) list rest)
  187. )
  188.  
  189. (defun union (list1 list2 &rest rest &key test test-not key)
  190.   (declare (ignore test test-not key))
  191.   (cond ((endp list1) list2)
  192.         ((apply #'sys::member1 (car list1) list2 rest)
  193.          (apply #'union (cdr list1) list2 rest))
  194.         (t (cons (car list1) (apply #'union (cdr list1) list2 rest)))
  195. ) )
  196.  
  197. (defun nunion (list1 list2 &rest rest &key test test-not key)
  198.   (declare (ignore test test-not key))
  199.   (cond ((endp list1) list2)
  200.         ((apply #'sys::member1 (car list1) list2 rest)
  201.          (apply #'nunion (cdr list1) list2 rest))
  202.         (t (rplacd list1 (apply #'nunion (cdr list1) list2 rest)))
  203. ) )
  204.  
  205. (defun intersection (list1 list2 &rest rest &key test test-not key)
  206.   (declare (ignore test test-not key))
  207.   (cond ((endp list1) nil)
  208.         ((apply #'sys::member1 (car list1) list2 rest)
  209.          (cons (car list1)
  210.                (apply #'intersection (cdr list1) list2 rest)))
  211.         (t (apply #'intersection (cdr list1) list2 rest))
  212. ) )
  213.  
  214. (defun nintersection (list1 list2 &rest rest &key test test-not key)
  215.   (declare (ignore test test-not key))
  216.   (cond ((endp list1) nil)
  217.         ((apply #'sys::member1 (car list1) list2 rest)
  218.          (rplacd list1 (apply #'nintersection (cdr list1) list2 rest)) )
  219.         (t (apply #'nintersection (cdr list1) list2 rest))
  220. ) )
  221.  
  222. (defun set-difference (list1 list2 &rest rest &key test test-not key)
  223.   (declare (ignore test test-not key))
  224.   (cond ((endp list1) nil)
  225.         ((not (apply #'sys::member1 (car list1) list2 rest))
  226.          (cons (car list1)
  227.                (apply #'set-difference (cdr list1) list2 rest)
  228.         ))
  229.         (t (apply #'set-difference (cdr list1) list2 rest))
  230. ) )
  231.  
  232. (defun nset-difference (list1 list2 &rest rest &key test test-not key)
  233.   (declare (ignore test test-not key))
  234.   (cond ((endp list1) nil)
  235.         ((not (apply #'sys::member1 (car list1) list2 rest))
  236.          (rplacd list1 (apply #'nset-difference (cdr list1) list2 rest)) )
  237.         (t (apply #'nset-difference (cdr list1) list2 rest))
  238. ) )
  239.  
  240. (defun set-exclusive-or (list1 list2 &rest rest &key test test-not key)
  241.   (declare (ignore test test-not key))
  242.   (append (apply #'set-difference list1 list2 rest)
  243.           (apply #'set-difference list2 list1 rest)
  244. ) )
  245.  
  246. (defun nset-exclusive-or (list1 list2 &rest rest &key test test-not key)
  247.   (declare (ignore test test-not key))
  248.   (nconc (apply #'set-difference list1 list2 rest)
  249.          (apply #'nset-difference list2 list1 rest)
  250. ) )
  251.  
  252. (defun subsetp (list1 list2 &rest rest &key test test-not key)
  253.   (declare (ignore test test-not key))
  254.   (do ((l list1 (cdr l)))
  255.       ((endp l) t)
  256.     (if (not (apply #'sys::member1 (car l) list2 rest)) (return nil))
  257. ) )
  258.  
  259. ; Wie SUBST-IF, nur da▀ das Ersatz-Element durch eine Funktion gegeben wird
  260. ; und nicht konstant sein mu▀.
  261. (defun subst-if-then (newfun testfun tree &key (key #'identity))
  262.   (labels ((subst (tree)
  263.              (if (funcall testfun (funcall key tree))
  264.                (funcall newfun tree)
  265.                (if (consp tree)
  266.                  (let* ((car (car tree)) (cdr (cdr tree))
  267.                         (newcar (subst car)) (newcdr (subst cdr)))
  268.                    (if (and (eq car newcar) (eq cdr newcdr))
  269.                      tree
  270.                      (cons newcar newcdr)
  271.                  ) )
  272.                  tree
  273.           )) ) )
  274.     (subst tree)
  275. ) )
  276.  
  277.  
  278. ;;; Funktionen fⁿr Hash-Tabellen (Kapitel 16)
  279.  
  280. (defmacro dohash ((keyvar valuevar HTform &optional resultform) &body body &environment env)
  281.   (multiple-value-bind (body-rest declarations) (system::parse-body body nil env)
  282.     (let ((HTvar (gensym)))
  283.       `(BLOCK NIL
  284.          (LET ((,HTvar ,HTform))
  285.            (LET ((,keyvar NIL) (,valuevar NIL))
  286.              (DECLARE (IGNORABLE ,keyvar ,valuevar) ,@declarations)
  287.              (MAPHASH
  288.                #'(LAMBDA (,keyvar ,valuevar)
  289.                    ,@(if declarations `((DECLARE ,@declarations)) '())
  290.                    (TAGBODY ,@body-rest)
  291.                  )
  292.                ,HTvar
  293.              )
  294.              ,resultform
  295.        ) ) )
  296. ) ) )
  297.  
  298.  
  299. ;;; Funktionen fⁿr Strings (Kapitel 18)
  300.  
  301. (defun string-trim (character-bag string)
  302.   (sys::string-both-trim character-bag character-bag string)
  303. )
  304.  
  305. (defun string-left-trim (character-bag string)
  306.   (sys::string-both-trim character-bag nil string)
  307. )
  308.  
  309. (defun string-right-trim (character-bag string)
  310.   (sys::string-both-trim nil character-bag string)
  311. )
  312.  
  313.  
  314. ;;; Funktionen fⁿr Pathnames (Kapitel 23.1.5)
  315. #+LOGICAL-PATHNAMES
  316. (progn
  317.   (defun logical-pathname-translations (host)
  318.     (setq host (string-upcase host))
  319.     (or (gethash host sys::*logical-pathname-translations*) ; :test #'equal !
  320.         (error (DEUTSCH "~S: ~S benennt keinen Logical Host."
  321.                 ENGLISH "~S: ~S does not name a logical host"
  322.                 FRANCAIS "~S : ~S n'est pas le nom d'un ½host logique╗.")
  323.                'logical-pathname-translations host
  324.   ) )   )
  325.   (defun set-logical-pathname-translations (host translations)
  326.     (setq host (string-upcase host))
  327.     (puthash host sys::*logical-pathname-translations* ; :test #'equal !
  328.              (let ;((host-pathname (logical-pathname (string-concat host ":;"))))
  329.                   ((host-pathname (sys::make-logical-pathname :host host)))
  330.                (mapcar #'(lambda (rule)
  331.                            (cons (merge-pathnames (logical-pathname (first rule))
  332.                                                   host-pathname 'NIL
  333.                                  )
  334.                                  (rest rule)
  335.                          ) )
  336.                        translations
  337.   ) )        ) )
  338.   (defun load-logical-pathname-translations (host)
  339.     (setq host (string-upcase host))
  340.     (unless (gethash host sys::*logical-pathname-translations*) ; :test #'equal !
  341.       (set-logical-pathname-translations host
  342.         ; Woher bekommt man die Liste der Umsetzungen??
  343.         #| ; Marcus versucht es so:
  344.            (read
  345.              (open
  346.                (merge-pathnames
  347.                  (merge-pathnames host
  348.                    (string-concat (sys::getenv "CLISP_LOGICAL_PATHNAME_TRANSLATIONS"))
  349.                  )
  350.                  (make-pathname :type '#".lsp")
  351.            ) ) )
  352.         |#
  353.         (if (and (fboundp 'sys::getenv)
  354.                  (sys::getenv (string-concat "LOGICAL_HOST_" host "_FROM"))
  355.                  (sys::getenv (string-concat "LOGICAL_HOST_" host "_TO"))
  356.             )
  357.           (list (list (sys::getenv (string-concat "LOGICAL_HOST_" host "_FROM"))
  358.                       (sys::getenv (string-concat "LOGICAL_HOST_" host "_TO"))
  359.           )     )
  360.           '()
  361.   ) ) ) )
  362.   (set-logical-pathname-translations "SYS"
  363.     '#+(or DOS)             (("**;*.LISP" "\\**\\*.LSP") (";**;*.LISP" "**\\*.LSP")
  364.                              ("**;*.*" "\\**\\*.*") (";**;*.*" "**\\*.*")
  365.                             )
  366.      #+(or AMIGA UNIX OS/2) (("**;*.LISP" "/**/*.lsp") (";**;*.LISP" "**/*.lsp")
  367.                              ("**;*.*" "/**/*.*") (";**;*.*" "**/*.*")
  368.                             )
  369.      #+ACORN-RISCOS         (("**;*.LISP" "$.**.*.lsp") (";**;*.LISP" "**.*.lsp")
  370.                              ("**;*.*" "$.**.*.*") (";**;*.*" "**.*.*")
  371.                             )
  372.   )
  373. )
  374.  
  375.  
  376. ;;; Funktionen fⁿr Zeit (Kapitel 25.4.1)
  377.  
  378. ; Hilfsfunktion fⁿr Macro TIME
  379. (defun %time (new-real1 new-real2 new-run1 new-run2 new-gc1 new-gc2
  380.               new-space1 new-space2 new-gccount
  381.               old-real1 old-real2 old-run1 old-run2 old-gc1 old-gc2
  382.               old-space1 old-space2 old-gccount)
  383.   (macrolet ((merge-2-values (val1 val2)
  384.                (if (< internal-time-units-per-second 1000000)
  385.                  `(dpb ,val1 (byte 16 16) ,val2) ; TIME_1: AMIGA, DOS, OS/2, UNIX_TIMES
  386.                  `(+ (* ,val1 internal-time-units-per-second) ,val2) ; TIME_2: UNIX sonst
  387.             )) )
  388.     (let ((Real-Time (- (merge-2-values new-real1 new-real2)
  389.                         (merge-2-values old-real1 old-real2)
  390.           )          )
  391.           (Run-Time (- (merge-2-values new-run1 new-run2)
  392.                        (merge-2-values old-run1 old-run2)
  393.           )         )
  394.           (GC-Time (- (merge-2-values new-gc1 new-gc2)
  395.                       (merge-2-values old-gc1 old-gc2)
  396.           )        )
  397.           (Space (- (dpb new-space1 (byte 24 24) new-space2)
  398.                     (dpb old-space1 (byte 24 24) old-space2)
  399.           )      )
  400.           (GC-Count (- new-gccount old-gccount))
  401.          )
  402.       (terpri)
  403.       (write-string "Real time: ")
  404.       (write (float (/ Real-Time internal-time-units-per-second)))
  405.       (write-string " sec.")
  406.       (terpri)
  407.       (write-string "Run time: ")
  408.       (write (float (/ Run-Time internal-time-units-per-second)))
  409.       (write-string " sec.")
  410.       (terpri)
  411.       (write-string "Space: ") (write Space) (write-string " Bytes")
  412.       (when (or (plusp GC-Count) (plusp GC-Time))
  413.         (terpri)
  414.         (write-string "GC: ") (write GC-Count)
  415.         (write-string ", GC time: ")
  416.         (write (float (/ GC-Time internal-time-units-per-second)))
  417.         (write-string " sec.")
  418.       )
  419. ) ) )
  420.  
  421. ; (sleep seconds) macht seconds Sekunden Pause. CLTL S. 447
  422. (defun sleep (time)
  423.   (if (and (realp time) (not (minusp time)))
  424.     (progn
  425.       ; Diese Fallunterscheidung hΣngt von sys::%sleep in time.d ab.
  426.       #+(or AMIGA DOS OS/2 ACORN-RISCOS) ; SLEEP_1
  427.       (if (> time '#,(floor (expt 2 31) internal-time-units-per-second))
  428.         ; Mehr als 248 bzw. 994 bzw. 497 Tage? (Denn sys::%sleep akzeptiert nur
  429.         ; Argumente < 2^32, bei #+(or DOS OS/2) sogar nur Argumente < 2^31.)
  430.         (loop ; ja -> Endlosschleife
  431.           (sys::%sleep '#,(* 86400 internal-time-units-per-second))
  432.         )
  433.         (sys::%sleep (round (* time internal-time-units-per-second)))
  434.       )
  435.       #+UNIX ; SLEEP_2
  436.       (if (> time 16700000) ; mehr als 193 Tage?
  437.         (loop (sys::%sleep 86400 0)) ; ja -> Endlosschleife
  438.         (multiple-value-bind (seconds rest) (floor time)
  439.           (sys::%sleep seconds (round (* rest 1000000)))
  440.       ) )
  441.     )
  442.     (error-of-type 'type-error
  443.       :datum time :expected-type '(REAL 0 *)
  444.       (DEUTSCH "~S: Argument mu▀ eine Zahl >=0 sein, nicht ~S"
  445.        ENGLISH "~S: argument ~S should be a nonnegative number"
  446.        FRANCAIS "~S : L'argument doit Ωtre un nombre positif ou zΘro et non ~S")
  447.       'sleep time
  448. ) ) )
  449.  
  450.  
  451. ;; Funktionen fⁿr Zeit-Umrechnung und Zeitzonen (CLTL Kapitel 25.4.1)
  452. ;; Version 2, beinhaltet mehr Mathematik und basiert auf MΣrz-Jahren
  453.  
  454. ; Ein MΣrz-Jahr sei die Periode vom 1.3. bis 28/29.2.
  455. ; Vorteil: Umrechnung Monat/Tag <--> Jahrtag wird einfacher.
  456. ; Skizze:
  457. ;   1.1.1900            1.1.1901            1.1.1902
  458. ;                                         
  459. ;   |-------------------|-------------------|-------------------|
  460. ;   |     Jahr 1900     |     Jahr 1901     |     Jahr 1902     |
  461. ;   |--|----------------|--|----------------|--|----------------|--|
  462. ;      |  MΣrz-Jahr 1900   |  MΣrz-Jahr 1901   |  MΣrz-Jahr 1902   |
  463. ;      |-------------------|-------------------|-------------------|
  464. ;                                            
  465. ;      1.3.1900            1.3.1901            1.3.1902
  466.  
  467. ; (UTag Jahr) = Nummer des Tages 1.3.Jahr (gegenⁿber 1.1.1900)
  468. ; UTag(J) = 365*J + floor(J/4) - floor(J/100) + floor(J/400) - 693901
  469. ; damit  UTag(J) - UTag(J-1) = 365 + [1 falls J Schaltjahr]
  470. ; und    UTag(1899) = -306
  471. ; gelten.
  472. (defun UTag (Jahr)
  473.   (+ (* 365 Jahr) (floor Jahr 4) (- (floor Jahr 100)) (floor Jahr 400) -693901)
  474. )
  475.  
  476. ; NΣherungwert:
  477. ; 365+1/4-1/100+1/400 = 365.2425 = 146097/400 .
  478. ; Durch Betrachtung einer Wertetabelle der 400-periodischen Funktion
  479. ; (J -> UTag(J)-146097/400*J) sieht man:
  480. ;   146097/400*J - 693902.4775 <= UTag(J) <= 146097/400*J - 693900.28
  481.  
  482. ; Bestimmt zu einem Tag (0 = 1.1.1900) das MΣrz-Jahr und den Tag im MΣrz-Jahr.
  483. ; (Jahr&Tag UTTag) ==> Jahr, Jahrtag
  484. ; mit (= UTTag (+ (UTag Jahr) Jahrtag))
  485. (defun Jahr&Tag (UTTag)
  486.   ; Gesucht ist das gr÷▀te Jahr mit UTag(Jahr) <= UTTag.
  487.   ; Fⁿr dieses Jahr J gilt
  488.   ; 146097/400*J - 693902.4775 <= UTag(J) <= UTTag < UTag(J+1) <= 146097/400*J - 693535.0375,
  489.   ; also 146097*J - 277560991 <= 400*UTTag < 146097*J - 277414015,
  490.   ; also 146097*(J-1900) + 23309 <= 400*UTTag < 146097*(J-1900) + 170285,
  491.   ; also J + 0.159544... <= 1900 + UTTag/(146097/400) < J + 1.165561... .
  492.   (let* ((Jahr (+ 1900 (floor (- UTTag 58) 146097/400)))
  493.          (Jahresanfang (UTag Jahr)))
  494.     ; Wegen 146097*(J-1900) + 109 <= 400*(UTTag-58) < 146097*(J-1900) + 147084,
  495.     ; also J <= 1900 + (UTTag-58)/(146097/400) < J+1.006755...,
  496.     ; ist die SchΣtzung  Jahr := floor(1900 + (UTTag-58)/(146097/400))
  497.     ; meist richtig und jedenfalls nicht zu klein und um h÷chstens 1 zu gro▀.
  498.     (when (< UTTag Jahresanfang) ; zu gro▀?
  499.       (decf Jahr)
  500.       (setq Jahresanfang (UTag Jahr))
  501.     )
  502.     (values Jahr (- UTTag Jahresanfang))
  503. ) )
  504.  
  505. ; Bei vielen Betriebssystemen (nicht bei UNIX) mu▀ die Zeitzone beim
  506. ; Installieren in timezone.lsp eingetragen werden. Hier stehen nur
  507. ; Defaultwerte.
  508.  
  509. #-UNIX
  510. ; lokale Zeitzone
  511. (defvar *default-time-zone* -1) ; Default: 1 h ÷stlich GMT = MEZ
  512. ; NB: Zeitzone mu▀ nicht ganzzahlig sein, sollte aber Vielfaches
  513. ; einer Sekunde sein.
  514.  
  515. #-UNIX
  516. ; Funktion, die feststellt, ob bei gegebenem MΣrz-Jahr und Tag und Stunde
  517. ; Sommerzeit gilt.
  518. (defvar *default-dst-check* ; Default: Sommerzeit nicht explizit bekannt
  519.   #'(lambda (Jahr Jahrtag Stunde) (declare (ignore Jahr Jahrtag Stunde)) nil)
  520. )
  521.  
  522. ; andere Abbildung  Jahrtag -> Monat  fⁿr decode-universal-time:
  523. ; Seien Monat und Jahrtag auf den 1. MΣrz bezogen
  524. ; (d.h. Jahrtag = 0 am 1. MΣrz, = 364 am 28. Februar, usw.,
  525. ;  und MΣrz=0,...,Dezember=9,Januar=10,Februar=11).
  526. ; Dann ist
  527. ;                Monat = floor(a*Jahrtag+b)
  528. ; sofern a und b so gewΣhlt sind, da▀ die Ungleichungen
  529. ;   122*a+b >= 4, 275*a+b >= 9, 30*a+b < 1, 336*a+b < 11
  530. ; gelten. Dies ist ein Viereck im Bereich
  531. ; 0.032653... = 8/245 <= a <= 7/214 = 0.032710...,
  532. ; 0.009345... = 1/107 <= b <= 1/49 = 0.020408...,
  533. ; in dem z.B. der Punkt (a=5/153,b=2/153) liegt:
  534. ;                Monat = floor((5*Jahrtag+2)/153).
  535.  
  536. ; andere Abbildung  Monat -> Jahrtag
  537. ; fⁿr encode-universal-time und decode-universal-time:
  538. ; Seien Monat und Jahrtag auf den 1. MΣrz bezogen
  539. ; (d.h. Jahrtag = 0 am 1. MΣrz, = 364 am 28. Februar, usw.,
  540. ;  und MΣrz=0,...,Dezember=9,Januar=10,Februar=11).
  541. ; Die Abbildung
  542. ;      Monat   0  1  2  3  4   5   6   7   8   9   10  11
  543. ;      Jahrtag 0 31 61 92 122 153 184 214 245 275 306 337
  544. ; kann man schreiben
  545. ;                Jahrtag = floor(a*Monat+b)
  546. ; sofern a und b so gewΣhlt sind, da▀ die Ungleichungen
  547. ;   a+b >= 31, 11*a+b >= 337, 4*a+b < 123, 9*a+b < 276
  548. ; gelten. Dies ist ein Viereck im Bereich
  549. ; 30.5714... = 214/7 <= a <= 245/8 = 30.625,
  550. ; 0.375      = 3/8   <= b <= 5/7   = 0.7142...,
  551. ; in dem z.B. der Punkt (a=153/5,b=2/5) liegt:
  552. ;                Jahrtag = floor((153*Monat+2)/5).
  553. ; Dies ist allerdings langsamer als ein Tabellenzugriff.
  554.  
  555. (macrolet ((Monat->Jahrtag (Monat) ; 0 <= Monat < 12, 0=MΣrz,...,11=Februar
  556.              `(svref '#(0 31 61 92 122 153 184 214 245 275 306 337) ,Monat)
  557.           ))
  558.  
  559. ; (encode-universal-time second minute hour date month year [time-zone]),
  560. ; CLTL S. 446
  561. (defun encode-universal-time
  562.               (Sekunde Minute Stunde Tag Monat Jahr &optional (Zeitzone nil)
  563.                &aux Monat3 Jahr3 Jahrtag UTTag)
  564.   (unless (and (and (integerp Jahr)
  565.                     (progn
  566.                       (when (<= 0 Jahr 99)
  567.                         (multiple-value-bind (i1 i2 i3 i4 i5 Jahrjetzt) (get-decoded-time)
  568.                           (declare (ignore i1 i2 i3 i4 i5))
  569.                           (setq Jahr
  570.                             (+ Jahr (* 100 (ceiling (- Jahrjetzt Jahr 50) 100)))
  571.                       ) ) )
  572.                       (<= 1900 Jahr)
  573.                )    )
  574.                (and (integerp Monat) (<= 1 Monat 12))
  575.                (progn
  576.                  (if (< Monat 3)
  577.                    (setq Jahr3 (1- Jahr)  Monat3 (+ Monat 9)) ; Monat3 10..11
  578.                    (setq Jahr3 Jahr       Monat3 (- Monat 3)) ; Monat3 0..9
  579.                  )
  580.                  (and (and (integerp Tag) (<= 1 Tag))
  581.                       (progn
  582.                         (setq Jahrtag (+ (1- Tag) (Monat->Jahrtag Monat3)))
  583.                         (setq UTTag (+ Jahrtag (UTag Jahr3)))
  584.                         (and (if (not (eql Monat3 11))
  585.                                (< Jahrtag (Monat->Jahrtag (1+ Monat3)))
  586.                                (< UTTag (UTag (1+ Jahr3)))
  587.                              )
  588.                              (and (integerp Stunde) (<= 0 Stunde 23))
  589.                              (and (integerp Minute) (<= 0 Minute 59))
  590.                              (and (integerp Sekunde) (<= 0 Sekunde 59))
  591.                              (and (progn
  592.                                     (unless Zeitzone
  593.                                       (setq Zeitzone
  594.                                         #-UNIX (- *default-time-zone*
  595.                                                   (if (funcall *default-dst-check* Jahr3 Jahrtag Stunde) 1 0)
  596.                                                )
  597.                                         #+UNIX (default-time-zone (+ (* 24 UTTag) Stunde))
  598.                                     ) )
  599.                                     (when (floatp Zeitzone) (setq Zeitzone (rational Zeitzone)))
  600.                                     (or (integerp Zeitzone)
  601.                                         (and (rationalp Zeitzone) (integerp (* 3600 Zeitzone)))
  602.                                   ) )
  603.                                   (<= -13 Zeitzone 12)
  604.           )    ) )    ) )    )
  605.     (error-of-type 'error
  606.       (DEUTSCH "Inkorrektes Datum: ~S.~S.~S, ~Sh~Sm~Ss, Zeitzone ~S"
  607.        ENGLISH "incorrect date: ~S.~S.~S, ~Sh~Sm~Ss, time zone ~S"
  608.        FRANCAIS "Date incorrecte : ~S/~S/~S, ~Sh~Sm~Ss, heure ~S")
  609.       Tag Monat Jahr Stunde Minute Sekunde Zeitzone
  610.   ) )
  611.   (+ Sekunde
  612.      (* 60 (+ Minute
  613.               (* 60 (+ Stunde Zeitzone
  614.                        (* 24 UTTag)
  615.   )  )     )  )     )
  616. )
  617.  
  618. ; (decode-universal-time universal-time [time-zone]), CLTL S. 445
  619. (defun decode-universal-time (UT &optional (time-zone nil)
  620.                               &aux Sommerzeit Zeitzone)
  621.   (if time-zone
  622.     (setq Sommerzeit nil Zeitzone time-zone)
  623.     #-UNIX
  624.     (setq time-zone *default-time-zone*
  625.           Sommerzeit (let ((UT (- UT (round (* 3600 time-zone)))))
  626.                        (multiple-value-bind (UTTag Stunde) (floor (floor UT 3600) 24)
  627.                          (multiple-value-bind (Jahr Jahrtag) (Jahr&Tag UTTag)
  628.                            (funcall *default-dst-check* Jahr Jahrtag Stunde)
  629.                      ) ) )
  630.           Zeitzone (- time-zone (if Sommerzeit 1 0))
  631.     )
  632.     #+UNIX
  633.     (progn
  634.       (multiple-value-setq (Zeitzone Sommerzeit) (default-time-zone (floor UT 3600)))
  635.       (setq time-zone (+ Zeitzone (if Sommerzeit 1 0)))
  636.     )
  637.   )
  638.   ; time-zone = Zeitzone ohne Sommerzeitberⁿcksichtigung,
  639.   ; Zeitzone = Zeitzone mit Sommerzeitberⁿcksichtigung.
  640.   (let ((UTSekunden (- UT (round (* 3600 Zeitzone)))))
  641.     (multiple-value-bind (UTMinuten Sekunde) (floor UTSekunden 60)
  642.       (multiple-value-bind (UTStunden Minute) (floor UTMinuten 60)
  643.         (multiple-value-bind (UTTage Stunde) (floor UTStunden 24)
  644.           (multiple-value-bind (Jahr Jahrtag) (Jahr&Tag UTTage)
  645.             (let* ((Monat (floor (+ (* 5 Jahrtag) 2) 153))
  646.                    (Tag (1+ (- Jahrtag (Monat->Jahrtag Monat)))))
  647.               (if (< Monat 10) ; Monat MΣrz..Dezember?
  648.                 (setq Monat (+ Monat 3)) ; Monat 3..12
  649.                 (setq Monat (- Monat 9) Jahr (+ Jahr 1)) ; Monat 1..2
  650.               )
  651.               (values Sekunde Minute Stunde Tag Monat Jahr (mod UTTage 7)
  652.                       Sommerzeit time-zone
  653. ) ) ) ) ) ) ) )
  654.  
  655. ) ; Ende von macrolet
  656.  
  657. ; (get-decoded-time), CLTL S. 445
  658. (defun get-decoded-time ()
  659.   (decode-universal-time (get-universal-time))
  660. )
  661.  
  662.  
  663. ;;; Verschiedenes
  664.  
  665. ; (concat-pnames obj1 obj2) liefert zu zwei Objekten (Symbolen oder Strings)
  666. ;  ein Symbol, dessen Printname sich aus den beiden Objekten zusammensetzt.
  667. (defun concat-pnames (obj1 obj2)
  668.   (let ((str (string-concat (string obj1) (string obj2))))
  669.     (if (and (plusp (length str)) (eql (char str 0) #\:))
  670.       (intern (subseq str 1) *keyword-package*)
  671.       (intern str)
  672. ) ) )
  673.  
  674. ; (default-directory) ist ein Synonym fⁿr (cd).
  675. (defun default-directory () (cd))
  676.  
  677. ; FORMAT-Control-String zur Datumsausgabe,
  678. ; anwendbar auf eine Liste (sec min hour day month year ...),
  679. ; belegt 17-19 Zeichen
  680. (defun date-format ()
  681.   (DEUTSCH (formatter "~1{~3@*~D.~4@*~D.~5@*~D ~2@*~2,'0D:~1@*~2,'0D:~0@*~2,'0D~:}")
  682.    ENGLISH (formatter "~1{~5@*~D/~4@*~D/~3@*~D ~2@*~2,'0D.~1@*~2,'0D.~0@*~2,'0D~:}")
  683.    FRANCAIS (formatter "~1{~3@*~D/~4@*~D/~5@*~D ~2@*~2,'0D:~1@*~2,'0D:~0@*~2,'0D~:}"))
  684. )
  685.  
  686. ; zeigt ein Directory an.
  687. (defun dir (&optional (pathnames #+(or DOS) '("*.*\\" "*.*")
  688.                                  #+(or AMIGA UNIX OS/2) '("*/" "*")
  689.                                  #+ACORN-RISCOS '("*." "*" "*.*")
  690.            )          )
  691.   (flet ((onedir (pathname)
  692.            (let ((pathname-list (directory pathname :full t :circle t)))
  693.              (if (every #'atom pathname-list)
  694.                (format t "~{~%~A~}"
  695.                  (sort pathname-list #'string< :key #'namestring)
  696.                )
  697.                (let ((date-format (date-format)))
  698.                  (dolist (l (sort pathname-list #'string< :key #'(lambda (l) (namestring (first l)))))
  699.                    (format t "~%~A~40T~7D~52T~21<~@?~>"
  700.                              (first l) (fourth l) date-format (third l)
  701.                ) ) )
  702.         )) ) )
  703.     (if (listp pathnames) (mapc #'onedir pathnames) (onedir pathnames))
  704.   )
  705.   (values)
  706. )
  707.  
  708.