;	動作テスト中のコード。
;	sit-for は popup と重なるとうまく動かない。これは
;	X11 Toolkit の何らかの実装と干渉しているためだと思
;	う。

(defun skk-date (&optional and-time)
  (let* ((str (current-time-string))
	 (y (- (string-to-int (substring str 20 24)) 1988))
	 (year (if skk-date-ad
		   (j-num (substring str 20 24))
		 (if (= y 1) "元" (j-num (int-to-string y)))))
	 (mon (substring str 4 7))
	 (month
	  (j-num
	   (cdr (assoc mon
		       '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3")
			 ("Apr" . "4") ("May" . "5") ("Jun" . "6")
			 ("Jul" . "7") ("Aug" . "8") ("Sep" . "9")
			 ("Oct" . "10") ("Nov" . "11") ("Dec" . "12"))))))
	 (day (j-num (substring str 8 10)))
	 (dw (substring str 0 3))
	 (day-of-week
	  (cdr (assoc dw
		      '(("Sun" . "日") ("Mon" . "月") ("Tue" . "火")
			("Wed" . "水") ("Thu" . "木") ("Fri" . "金")
			("Sat" . "土")))))
	 )
    (concat (if skk-date-ad "" "平成") year "年"
	    month "月" day "日" "\(" day-of-week "\)"
	    (if and-time
		(let ((hour (substring str 11 13))
		      (minute (substring str 14 16))
		      (second (substring str 17 19)))
		  (concat " " hour "時" minute "分" second "秒"))))))

(defun j-clock (&optional kakutei-when-quit time-signal)
  (interactive "*")
  (let ((start (current-time-string))
	(local-map (current-local-map))
	finish mes)
    (unwind-protect
	(progn 
	  (use-local-map nil)
	  (condition-case nil
	      (let (case-fold-search
		    inhibit-quit
		    visible-bell)
		(while (not quit-flag)
		  (setq mes (skk-date t))
		  (message (concat  mes "    Hit any key to quit"))
		  (unless (sit-for 1)
		    (read-event)
		    (signal 'quit nil))))
	    (quit
	     (prog2
		 (setq finish (current-time-string))
		 (skk-date t)
	       ;;(skk-use-local-map skk-map)
	       (if kakutei-when-quit
		   (setq skk-kakutei-flag t) )
	       (message (concat "経過時間 :" (j-time-diff start finish)))))))
      (progn
	(use-local-map local-map)))))

(defun j-time-diff (start finish)
  ;; (current-time-string) の返り値 START と FINISH の時間差を求め、
  ;; "時間:分:秒" の形式で返す。skk-clock のサブルーチン。
  (let ((s-hour (string-to-int (substring start 11 13)))
        (s-minute (string-to-int (substring start 14 16)))
        (s-second (string-to-int (substring start 17 19)))
        (f-hour (string-to-int (substring finish 11 13)))
        (f-minute (string-to-int (substring finish 14 16)))
        (f-second (string-to-int (substring finish 17 19)))
        second-diff minute-diff hour-diff )
    (if (not (string= (substring start 20) (substring finish 20)))
        (error "違う年の時間差は計算できません"))
    (setq second-diff (- f-second s-second))
    (if (> 0 second-diff)
        (setq f-minute (1- f-minute)
              second-diff (- (+ f-second 60) s-second) ))
    (setq minute-diff (- f-minute s-minute))
    (if (> 0 minute-diff)
        (setq f-hour (1- f-hour)
              minute-diff (- (+ f-minute 60) s-minute) ))
    (setq hour-diff (- f-hour s-hour))
    (if (> 0 hour-diff)
        (error "第２引数は第１引数より後の時間でなければなりません"))
    (format "%02d:%02d:%02d" hour-diff minute-diff second-diff) ))

