home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / gnu / emacs / gnus / 1513 < prev    next >
Encoding:
Text File  |  1993-01-26  |  11.0 KB  |  317 lines

  1. Xref: sparky gnu.emacs.gnus:1513 gnu.emacs.sources:967
  2. Newsgroups: gnu.emacs.gnus,gnu.emacs.sources
  3. Path: sparky!uunet!cs.utexas.edu!sun-barr!sh.wide!wnoc-kyo!kyu-cs!wnoc-fukuoka-news!shiwasu!shiwasu!umerin
  4. From: umerin@mse.kyutech.ac.jp (Masanobu UMEDA)
  5. Subject: timezone.el (part of gnus-3.14.3)
  6. Sender: news@shiwasu.isci.kyutech.ac.jp
  7. Organization: Department of Mechanical System Engineering, Kyutech, Japan
  8. Date: Tue, 26 Jan 1993 03:09:25 GMT
  9. Message-ID: <UMERIN.93Jan26120925@orchid.mse.kyutech.ac.jp>
  10. Reply-To: umerin@mse.kyutech.ac.jp
  11. Distribution: gnu
  12. Lines: 303
  13.  
  14. Here is a new version of timezone.el that is a part of gnus-3.14.3.
  15.  
  16. ----------------------------------------------------------------------
  17. ;;; Timezone package for GNU Emacs
  18. ;; Copyright(C) 1990, 1991, 1992 Masanobu UMEDA (umerin@mse.kyutech.ac.jp)
  19. ;; $Header: timezone.el,v 1.4 93/01/26 12:05:40 umerin Locked $
  20.  
  21. ;; This file is part of GNU Emacs.
  22.  
  23. ;; GNU Emacs is distributed in the hope that it will be useful,
  24. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  25. ;; accepts responsibility to anyone for the consequences of using it
  26. ;; or for whether it serves any particular purpose or works at all,
  27. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  28. ;; License for full details.
  29.  
  30. ;; Everyone is granted permission to copy, modify and redistribute
  31. ;; GNU Emacs, but only under the conditions described in the
  32. ;; GNU Emacs General Public License.   A copy of this license is
  33. ;; supposed to have been given to you along with GNU Emacs so you
  34. ;; can know your rights and responsibilities.  It should be in a
  35. ;; file named COPYING.  Among other things, the copyright notice
  36. ;; and this notice must be preserved on all copies.
  37.  
  38. (provide 'timezone)
  39.  
  40. (defvar timezone-world-timezones
  41.   '(("PST" .  -800)
  42.     ("PDT" .  -700)
  43.     ("MST" .  -700)
  44.     ("MDT" .  -600)
  45.     ("CST" .  -600)
  46.     ("CDT" .  -500)
  47.     ("EST" .  -500)
  48.     ("EDT" .  -400)
  49.     ("GMT" .  +000)
  50.     ("BST" .  +100)
  51.     ("MET" .  +100)
  52.     ("EET" .  +200)
  53.     ("JST" .  +900)
  54.     ("GMT+1"  .  +100) ("GMT+2"  .  +200) ("GMT+3"  .  +300)
  55.     ("GMT+4"  .  +400) ("GMT+5"  .  +500) ("GMT+6"  .  +600)
  56.     ("GMT+7"  .  +700) ("GMT+8"  .  +800) ("GMT+9"  .  +900)
  57.     ("GMT+10" . +1000) ("GMT+11" . +1100) ("GMT+12" . +1200) ("GMT+13" . +1300)
  58.     ("GMT-1"  .  -100) ("GMT-2"  .  -200) ("GMT-3"  .  -300)
  59.     ("GMT-4"  .  -400) ("GMT-5"  .  -500) ("GMT-6"  .  -600)
  60.     ("GMT-7"  .  -700) ("GMT-8"  .  -800) ("GMT-9"  .  -900)
  61.     ("GMT-10" . -1000) ("GMT-11" . -1100) ("GMT-12" . -1200))
  62.   "*Time differentials of timezone from GMT in hour.")
  63.  
  64. (defvar timezone-months-assoc
  65.   '(("JAN" .  1)("FEB" .  2)("MAR" .  3)
  66.     ("APR" .  4)("MAY" .  5)("JUN" .  6)
  67.     ("JUL" .  7)("AUG" .  8)("SEP" .  9)
  68.     ("OCT" . 10)("NOV" . 11)("DEC" . 12))
  69.   "Alist of first three letters of a month and its numerical representation.")
  70.  
  71. (defun timezone-make-date-arpa-standard (date &optional local timezone)
  72.   "Convert DATE to an arpanet standard date.
  73. Optional 1st argumetn LOCAL specifies the default local timezone of the DATE.
  74. Optional 2nd argument TIMEZONE specifies a timezone to be represented in."
  75.   (let* ((date   (timezone-parse-date date))
  76.      (year   (string-to-int (aref date 0)))
  77.      (month  (string-to-int (aref date 1)))
  78.      (day    (string-to-int (aref date 2)))
  79.      (time   (timezone-parse-time (aref date 3)))
  80.      (hour   (string-to-int (aref time 0)))
  81.      (minute (string-to-int (aref time 1)))
  82.      (second (string-to-int (aref time 2)))
  83.      (local  (or (aref date 4) local)) ;Use original if defined
  84.      (timezone (or timezone local))
  85.      (diff   (- (timezone-zone-to-minute timezone)
  86.             (timezone-zone-to-minute local)))
  87.      (new    (timezone-fix-time year month day
  88.                     hour (+ minute diff) second)))
  89.     (timezone-make-arpa-date (aref new 0) (aref new 1) (aref new 2)
  90.                  (timezone-make-time-string
  91.                   (aref new 3) (aref new 4) (aref new 5))
  92.                  timezone)
  93.     ))
  94.  
  95. (defun timezone-make-date-sortable (date &optional local timezone)
  96.   "Convert DATE to a sortable date string.
  97. Optional 1st argumetn LOCAL specifies the default local timezone of the DATE.
  98. Optional 2nd argument TIMEZONE specifies a timezone to be represented in."
  99.   (let* ((date   (timezone-parse-date date))
  100.      (year   (string-to-int (aref date 0)))
  101.      (month  (string-to-int (aref date 1)))
  102.      (day    (string-to-int (aref date 2)))
  103.      (time   (timezone-parse-time (aref date 3)))
  104.      (hour   (string-to-int (aref time 0)))
  105.      (minute (string-to-int (aref time 1)))
  106.      (second (string-to-int (aref time 2)))
  107.      (local  (or (aref date 4) local)) ;Use original if defined
  108.      (timezone (or timezone local))
  109.      (diff   (- (timezone-zone-to-minute timezone)
  110.             (timezone-zone-to-minute local)))
  111.      (new    (timezone-fix-time year month day
  112.                     hour (+ minute diff) second)))
  113.     (timezone-make-sortable-date (aref new 0) (aref new 1) (aref new 2)
  114.                  (timezone-make-time-string
  115.                   (aref new 3) (aref new 4) (aref new 5)))
  116.     ))
  117.  
  118.  
  119. ;;
  120. ;; Parsers and Constructors of Date and Time
  121. ;;
  122.  
  123. (defun timezone-make-arpa-date (year month day time &optional timezone)
  124.   "Make arpanet standard date string from YEAR, MONTH, DAY, and TIME.
  125. Optional argument TIMEZONE specifies a time zone."
  126.   (format "%02d %s %4d %s%s"
  127.       day
  128.       (capitalize (car (rassq month timezone-months-assoc)))
  129.       ;;(- year (* (/ year 100) 100))    ;1990 -> 90
  130.       (if (< year 100) (+ year 1900) year) ;90->1990
  131.       time
  132.       (if timezone (concat " " timezone) "")
  133.       ))
  134.  
  135. (defun timezone-make-sortable-date (year month day time)
  136.   "Make sortable date string from YEAR, MONTH, DAY, and TIME."
  137.   (format "%4d%02d%02d%s"
  138.       ;;(- year (* (/ year 100) 100))    ;1990 -> 90
  139.       (if (< year 100) (+ year 1900) year) ;90->1990
  140.       month day time))
  141.  
  142. (defun timezone-make-time-string (hour minute second)
  143.   "Make time string from HOUR, MINUTE, and SECOND."
  144.   (format "%02d:%02d:%02d" hour minute second))
  145.  
  146. (defun timezone-parse-date (date)
  147.   "Parse DATE and return a vector [year month day time timezone].
  148. 19 is prepended to year if necessary. Timezone may be NIL if nothing.
  149. Understand the following styles:
  150.  (1) 14 Apr 89 03:20[:12] [GMT]
  151.  (2) Fri, 17 Mar 89 4:01[:33] [GMT]
  152.  (3) Mon Jan 16 16:12[:37] [GMT] 1989
  153.  (4) 6 May 1992 1641-JST (Wednesday)"
  154.   (let ((date (or date ""))
  155.     (year nil)
  156.     (month nil)
  157.     (day nil)
  158.     (time nil)
  159.     (zone nil))            ;This may be nil.
  160.     (cond ((string-match
  161. "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\'" date)
  162.        ;; Styles: (1) and (2) without timezone
  163.        (setq year 3 month 2 day 1 time 4 zone nil))
  164.       ((string-match
  165. "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\([-+a-zA-Z0-9]+\\)" date)
  166.        ;; Styles: (1) and (2) with timezone and buggy timezone
  167.        (setq year 3 month 2 day 1 time 4 zone 5))
  168.       ((string-match
  169. "\\([^ ,]+\\) +\\([0-9]+\\) \\([0-9]+:[0-9:]+\\) \\([0-9]+\\)" date)
  170.        ;; Styles: (3) without timezone
  171.        (setq year 4 month 1 day 2 time 3 zone nil))
  172.       ((string-match
  173. "\\([^ ,]+\\) +\\([0-9]+\\) \\([0-9]+:[0-9:]+\\) \\([-+a-zA-Z0-9]+\\) \\([0-9]+\\)" date)
  174.        ;; Styles: (3) with timezoen
  175.        (setq year 5 month 1 day 2 time 3 zone 4))
  176.       ((string-match
  177. "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+\\)[ ]*\\([-+a-zA-Z0-9]+\\)" date)
  178.        ;; Styles: (4) with timezone
  179.        (setq year 3 month 2 day 1 time 4 zone 5))
  180.       )
  181.     (if year
  182.     (progn
  183.       (setq year
  184.         (substring date (match-beginning year) (match-end year)))
  185.       ;; It is now Dec 1992.  8 years before the end of the World.
  186.       (if (< (length year) 4)
  187.           (setq year (concat "19" (substring year -2 nil))))
  188.       (setq month
  189.         (int-to-string
  190.          (cdr
  191.           (assoc
  192.            (upcase
  193.             ;; Don't use `match-end' in order to take 3
  194.             ;; letters from the beginning.
  195.             (substring date
  196.                    (match-beginning month)
  197.                    (+ (match-beginning month) 3)))
  198.            timezone-months-assoc))))
  199.       (setq day
  200.         (substring date (match-beginning day) (match-end day)))
  201.       (setq time
  202.         (substring date (match-beginning time) (match-end time)))))
  203.     (if zone
  204.     (setq zone
  205.           (substring date (match-beginning zone) (match-end zone))))
  206.     ;; Return a vector.
  207.     (if year
  208.     (vector year month day time zone)
  209.       (vector "0" "0" "0" "0" nil))
  210.     ))
  211.  
  212. (defun timezone-parse-time (time)
  213.   "Parse TIME (HH:MM:SS) and return a vector [hour minute second].
  214. Recognize HH:MM:SS, HH:MM, HHMMSS, HHMM."
  215.   (let ((time (or time ""))
  216.     (hour nil)
  217.     (minute nil)
  218.     (second nil))
  219.     (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time)
  220.        ;; HH:MM:SS
  221.        (setq hour 1 minute 2 second 3))
  222.       ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time)
  223.        ;; HH:MM
  224.        (setq hour 1 minute 2 second nil))
  225.       ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
  226.        ;; HHMMSS
  227.        (setq hour 1 minute 2 second 3))
  228.       ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
  229.        ;; HHMM
  230.        (setq hour 1 minute 2 second nil))
  231.       )
  232.     ;; Return [hour minute second]
  233.     (vector
  234.      (if hour
  235.      (substring time (match-beginning hour) (match-end hour)) "0")
  236.      (if minute
  237.      (substring time (match-beginning minute) (match-end minute)) "0")
  238.      (if second
  239.      (substring time (match-beginning second) (match-end second)) "0"))
  240.     ))
  241.  
  242.  
  243. ;; Miscellaneous
  244.  
  245. (defun timezone-zone-to-minute (timezone)
  246.   "Translate TIMEZONE (in zone name or integer) to integer minute."
  247.   (if timezone
  248.       (progn
  249.     (setq timezone
  250.           (or (cdr (assoc (upcase timezone) timezone-world-timezones))
  251.           ;; +900
  252.           timezone))
  253.     (if (stringp timezone)
  254.         (setq timezone (string-to-int timezone)))
  255.     ;; Taking account of minute in timezone.
  256.     ;; HHMM -> MM
  257.     (+ (* 60 (/ timezone 100)) (% timezone 100)))
  258.     0))
  259.  
  260. (defun timezone-fix-time (year month day hour minute second)
  261.   "Fix date and time."
  262.   ;; MINUTE may be larger than 60 or smaller than -60.
  263.   (let ((hour-fix
  264.      (if (< minute 0)
  265.          (/ (- minute 59) 60) (/ minute 60))))
  266.     (setq hour (+ hour hour-fix))
  267.     (setq minute (- minute (* 60 hour-fix))))
  268.   ;; HOUR may be larger than 24 or smaller than 0.
  269.   (cond ((<= 24 hour)            ;24 -> 00
  270.      (setq hour (- hour 24))
  271.      (setq day  (1+ day))
  272.      (if (< (timezone-last-day-of-month month year) day)
  273.          (progn
  274.            (setq month (1+ month))
  275.            (setq day 1)
  276.            (if (< 12 month)
  277.            (progn
  278.              (setq month 1)
  279.              (setq year (1+ year))
  280.              ))
  281.            )))
  282.     ((> 0 hour)
  283.      (setq hour (+ hour 24))
  284.      (setq day  (1- day))
  285.      (if (> 1 day)
  286.          (progn
  287.            (setq month (1- month))
  288.            (if (> 1 month)
  289.            (progn
  290.              (setq month 12)
  291.              (setq year (1- year))
  292.              ))
  293.            (setq day (timezone-last-day-of-month month year))
  294.            )))
  295.     )
  296.   (vector year month day hour minute second))
  297.  
  298. ;; Partly copied from Calendar program by Edward M. Reingold.
  299. ;; Thanks a lot.
  300.  
  301. (defun timezone-last-day-of-month (month year)
  302.   "The last day in MONTH during YEAR."
  303.   (if (and (= month 2) (timezone-leap-year-p year))
  304.       29
  305.     (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
  306.  
  307. (defun timezone-leap-year-p (year)
  308.   "Returns t if YEAR is a Gregorian leap year."
  309.   (or (and (zerop  (mod year 4))
  310.        (not (zerop (mod year 100))))
  311.       (zerop (mod year 400))))
  312. --
  313. Masanobu UMEDA
  314. umerin@mse.kyutech.ac.jp
  315. Faculty of Computer Science and System Engineering
  316. Kyushu Institute of Technology
  317.