[Mew-dist 14672] [CFR] My little hack to b73 mew-func.el for support time-zone of minute unit

Ninomiya Hideyuki nin at example.com
2000年 10月 21日 (土) 19:12:50 JST


こんばんは	二宮@お四国 です

mew-func.el の タイムゾーン 関連の部分を分単位の タイムゾーン にも
対応できるよう、Emacs での タイムゾーン 内部表現の秒単位を使うようにし、

(defvar mew-time-tmzn-alist
  '(("PDT" -7 0) .. ("GMT" +0 0) .. ("JST" +9 0) ...
    ("タイムゾーン文字列" 符号付-時間-数値 符号無-分-数値)))

としました。

(mew-time-tmzn-str-to-int "タイムゾーン文字列")

は秒単位でタイムゾーンを返します。

(mew-time-rfc-to-list "RFC822 Date:"  &optional "タイムゾーン文字列")

という (年 月 日 時 分 秒) を返す関数が在ります。
オプションの タイムゾーン文字列 を指定するとそのタイムゾーンで返します。
default は GMT を吐きます。

このリストから各項目を取り出す

(new-time-list-year|mon|day|hour|min|sec)

というマクロを用意してます。

(mew-time-list-adjust 年 月 日 時 分 秒)

というマクロが在り、時間の正規化

(2000 13 40 30 50 120) => (2001 2 10 6 52 0)

を行えます。このマクロでは decode|encode-time を使ってるので
扱える年は 1970 〜 2038(??) です。
# ここがちょっと曲者な気がします。夏時間な環境ではまずいようなっ...
# GMT で decode, encode する方法が良く解らなかった....

もし問題無いようなら Merge していただけたらと思います。。。

# mew-time-tmzn-alist にはタイムゾーンの追加はしてません m(__)m
# 他の mew-*.el はチェックしてないですし、他からこの関数等群利用
# 出来るとこ在るかはみてません。一応 b73 にこれ当てて動いてます。

Index: mew-func.el
===================================================================
RCS file: /tmp/am/od/cvs/mew/mew-func.el,v
retrieving revision 0.18
diff -c -r0.18 mew-func.el
*** mew-func.el	2000/10/15 14:20:49	0.18
--- mew-func.el	2000/10/21 06:20:51
***************
*** 936,948 ****
  ;;;
  
  (defvar mew-time-tmzn-alist
!   '(("PST" . -8) ("PDT" . -7) ("MST" . -7) ("MDT" . -6)
!     ("CST" . -6) ("CDT" . -5) ("EST" . -5) ("EDT" . -4)
!     ("AST" . -4) ("NST" . -3) ("UT"  . +0) ("GMT" . +0)
!     ("BST" . +1) ("MET" . +1) ("EET" . +2) ("JST" . +9)))
  
  (defmacro mew-time-tmzn-str-to-int (str)
!   (` (cdr (assoc (upcase (, str)) mew-time-tmzn-alist))))
  
  (defmacro mew-time-tmzn-sym ()
    '(car (cdr (current-time-zone))))
--- 936,952 ----
  ;;;
  
  (defvar mew-time-tmzn-alist
!   '(("PST" -8 0) ("PDT" -7 0) ("MST" -7 0) ("MDT" -6 0)
!     ("CST" -6 0) ("CDT" -5 0) ("EST" -5 0) ("EDT" -4 0)
!     ("AST" -4 0) ("NST" -3 0) ("UT"  +0 0) ("GMT" +0 0)
!     ("BST" +1 0) ("MET" +1 0) ("EET" +2 0) ("JST" +9 0)))
  
  (defmacro mew-time-tmzn-str-to-int (str)
!   (` (let ((l (cdr (assoc (upcase (, str)) mew-time-tmzn-alist))))
!        (if (null l)
! 	   nil
! 	 (if (< (nth 0 l) 0) (setcdr l (cons (- (nth 1 l)) ())))
! 	 (+ (* (nth 0 l) 3600) (* (nth 1 l) 60))))))
  
  (defmacro mew-time-tmzn-sym ()
    '(car (cdr (current-time-zone))))
***************
*** 993,1022 ****
  (defmacro mew-time-rfc-hour ()
    '(string-to-int (substring s (match-beginning 4) (match-end 4))))
  (defmacro mew-time-rfc-min  ()
!   '(substring s (match-beginning 5) (match-end 5)))
  (defmacro mew-time-rfc-sec  ()
!   '(if (match-beginning 7)
!        (substring s (match-beginning 7) (match-end 7))
!      "00"))
  (defmacro mew-time-rfc-tmzn ()
    '(if (match-beginning 9)
         (let ((tmzn (substring s (match-beginning 9) (match-end 9)))
  	     int)
  	 (cond
  	  ((string-match "^[-+][0-9]+$" tmzn)
! 	   (/ (string-to-int tmzn) 100))
  	  ((setq int (mew-time-tmzn-str-to-int tmzn))
  	   int)
  	  (t 0)))
       0))
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;;
  ;;;
  ;;;
  
! ;; "20000726231835"
  (defun mew-time-rfc-to-sortkey (s)
    (if (string-match mew-time-rfc-regex s)
        (let ((year (mew-time-rfc-year))
  	    (mon  (mew-time-mon-str-to-int (mew-time-rfc-mon)))
--- 997,1045 ----
  (defmacro mew-time-rfc-hour ()
    '(string-to-int (substring s (match-beginning 4) (match-end 4))))
  (defmacro mew-time-rfc-min  ()
!   '(string-to-int (substring s (match-beginning 5) (match-end 5))))
  (defmacro mew-time-rfc-sec  ()
!   '(string-to-int (if (match-beginning 7)
! 		      (substring s (match-beginning 7) (match-end 7))
! 		    "0")))
  (defmacro mew-time-rfc-tmzn ()
    '(if (match-beginning 9)
         (let ((tmzn (substring s (match-beginning 9) (match-end 9)))
  	     int)
  	 (cond
  	  ((string-match "^[-+][0-9]+$" tmzn)
! 	   (+ (* (/ (string-to-int tmzn) 100) 3600)
! 	      (* (% (string-to-int tmzn) 100) 60)))
  	  ((setq int (mew-time-tmzn-str-to-int tmzn))
  	   int)
  	  (t 0)))
       0))
  
+ (defmacro mew-time-list-adjust (year mon day hour min sec)
+   (` (let (l)
+        (setq l (decode-time (encode-time (, sec) (, min) (, hour)
+ 					 (, day) (, mon) (, year))))
+        (mapcar (function (lambda (n) (nth n l))) '(5 4 3 2 1 0)))))
+ 
+ (defmacro mew-time-list-year (l) (` (nth 0 (, l))))
+ (defmacro mew-time-list-mon  (l) (` (nth 1 (, l))))
+ (defmacro mew-time-list-day  (l) (` (nth 2 (, l))))
+ (defmacro mew-time-list-hour (l) (` (nth 3 (, l))))
+ (defmacro mew-time-list-min  (l) (` (nth 4 (, l))))
+ (defmacro mew-time-list-sec  (l) (` (nth 5 (, l))))
+ 
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;;
  ;;;
  ;;;
  
! ;; "20000726121835"
  (defun mew-time-rfc-to-sortkey (s)
+   (let ((l (mew-time-rfc-to-list s)))
+     (if l (apply 'format "%4d%02d%02d%02d%02d%02d" l))))
+ 
+ ;; '(2000 7 26 12 18 35)
+ (defun mew-time-rfc-to-list (s &optional tzs)
    (if (string-match mew-time-rfc-regex s)
        (let ((year (mew-time-rfc-year))
  	    (mon  (mew-time-mon-str-to-int (mew-time-rfc-mon)))
***************
*** 1024,1062 ****
  	    (hour (mew-time-rfc-hour))
  	    (min  (mew-time-rfc-min))
  	    (sec  (mew-time-rfc-sec))
! 	    (tmzn (mew-time-rfc-tmzn)))
  	(cond
  	 ((< year 50)
  	  (setq year (+ year 2000)))
  	 ((< year 100)
  	  (setq year (+ year 1900))))
! 	(setq hour (- hour tmzn))
! 	(cond
! 	 ((< hour 0)
! 	  (setq hour (+ hour 24) day (1- day))
! 	  (if (< day 1)
! 	      (progn
! 		(if (< (setq mon (1- mon)) 1)
! 		    (setq year (1- year) mon (+ mon 12)))
! 		(setq day (mew-last-day year mon)))))
! 	 ((> hour 23)
! 	  (setq hour (- hour 24) day (1+ day))
! 	  (if (> day (mew-last-day year mon))
! 		(if (> (setq day 1 mon (1+ mon)) 12)
! 		    (setq year (1+ year) mon (- mon 12))))))
! 	(format "%4d%02d%02d%02d%s%s" year mon day hour min sec))))
! 
! (defvar mew-last-day-list '(31 nil 31 30 31 30 31 31 30 31 30 31))
! 
! (defun mew-last-day (year mon)
!   (let ((day (nth (1- mon) mew-last-day-list)))
!     (cond
!      (day day)
!      ((and (= (% year 4) 0)
! 	   (or (= (% year 400) 0)
! 	       (/= (% year 100) 0)))
!       29)
!      (t 28))))
  
  (defun mew-time-ctz-to-sortkey (time)
    (if (fboundp 'format-time-string)
--- 1047,1063 ----
  	    (hour (mew-time-rfc-hour))
  	    (min  (mew-time-rfc-min))
  	    (sec  (mew-time-rfc-sec))
! 	    (tmzn (mew-time-rfc-tmzn))
! 	    tz)
  	(cond
  	 ((< year 50)
  	  (setq year (+ year 2000)))
  	 ((< year 100)
  	  (setq year (+ year 1900))))
! 	(if tzs (setq tz (mew-time-tmzn-str-to-int tzs)) (setq tz 0))
! 	(setq sec (+ (- sec tmzn ) tz))
! 	(mew-time-list-adjust year mon day hour min sec))
!     nil))
  
  (defun mew-time-ctz-to-sortkey (time)
    (if (fboundp 'format-time-string)

# 拙作のコード捨てて encode|decode-time 任せにしちゃった。。。。
# だって難しいんだもんっ
---
ICQ UIN:57442553                          二宮 秀幸@愛媛 鬼北 広見町
PGP-Fingerprint:  6C59 EC08 5B23 6490 44D0  7CD3 DA40 219F 7114 8553
PGP-Public-Key:    http://user.shikoku.ne.jp/~nin/pgp/public-key.txt
mailto:nin at example.com   http://user.shikoku.ne.jp/~nin/index.html
mailto:nin at example.com http://home.jp.freebsd.org/~nin/index.html



Mew-dist メーリングリストの案内