| 1 | ;;; -*- Mode: Lisp -*- |
|---|
| 2 | |
|---|
| 3 | ;;; License: MIT license. |
|---|
| 4 | |
|---|
| 5 | ;;; |
|---|
| 6 | ;;; This file was part of xyzzy. |
|---|
| 7 | ;;; |
|---|
| 8 | |
|---|
| 9 | ;;; Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 10 | ;;; a copy of this software and associated documentation files (the |
|---|
| 11 | ;;; "Software"), to deal in the Software without restriction, including |
|---|
| 12 | ;;; without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 13 | ;;; distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 14 | ;;; permit persons to whom the Software is furnished to do so, subject to |
|---|
| 15 | ;;; the following conditions: |
|---|
| 16 | ;;; |
|---|
| 17 | ;;; The above copyright notice and this permission notice shall be |
|---|
| 18 | ;;; included in all copies or substantial portions of the Software. |
|---|
| 19 | ;;; |
|---|
| 20 | ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 21 | ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 22 | ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 23 | ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|---|
| 24 | ;;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|---|
| 25 | ;;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|---|
| 26 | ;;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 27 | |
|---|
| 28 | ;;; |
|---|
| 29 | ;;; format-date |
|---|
| 30 | ;;; |
|---|
| 31 | ;;; a: 短い形式の曜日 |
|---|
| 32 | ;;; A: 長い形式の曜日 |
|---|
| 33 | ;;; b: 短い形式の月 |
|---|
| 34 | ;;; B: 長い形式の月 |
|---|
| 35 | ;;; d: 日(00〜59) # (0〜59) |
|---|
| 36 | ;;; e: 和暦の年(01〜) # (1〜) |
|---|
| 37 | ;;; E: 和暦の年(元, 02〜) # (元, 2〜) |
|---|
| 38 | ;;; g: 元号(明治,大正,昭和,平成) # (明,大,昭,平) |
|---|
| 39 | ;;; G: 元号(M, T, S, H) |
|---|
| 40 | ;;; H: 時(00〜23) # (0〜23) |
|---|
| 41 | ;;; I: 12時間の時(01〜12) # (1〜12) |
|---|
| 42 | ;;; i: Internet Time(000〜999) |
|---|
| 43 | ;;; m: 月(01〜12) # (1〜12) |
|---|
| 44 | ;;; M: 分(00〜59) # (0〜59) |
|---|
| 45 | ;;; p: 午前/午後 |
|---|
| 46 | ;;; P: AM/PM # am/pm |
|---|
| 47 | ;;; S: 秒(00〜59) # (0〜59) |
|---|
| 48 | ;;; v: 曜日(日本語) |
|---|
| 49 | ;;; y: 年(2桁) |
|---|
| 50 | ;;; Y: 年(4桁) |
|---|
| 51 | ;;; z: タイムゾーン名(JST-9) |
|---|
| 52 | ;;; Z: タイムゾーン(+0900) # (+09:00) |
|---|
| 53 | |
|---|
| 54 | ;;; Commentary: |
|---|
| 55 | ;;; |
|---|
| 56 | ;;; * xyzzyのlisp/timestmp.lをCommon Lispに移植したものです。 |
|---|
| 57 | ;;; cl-ppcreに依存しています。 |
|---|
| 58 | |
|---|
| 59 | (defpackage :xyzzy-compat |
|---|
| 60 | (:use :cl) |
|---|
| 61 | (:nicknames :xyzzy) |
|---|
| 62 | (:export :format-date |
|---|
| 63 | :format-date-string |
|---|
| 64 | :*date-formats* |
|---|
| 65 | :add-date-format |
|---|
| 66 | :insert-date-string |
|---|
| 67 | :parse-date-string)) |
|---|
| 68 | |
|---|
| 69 | (in-package :xyzzy) |
|---|
| 70 | |
|---|
| 71 | (defconstant *abbreviated-weekday-names* |
|---|
| 72 | #("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun")) |
|---|
| 73 | |
|---|
| 74 | (defconstant *full-weekday-names* |
|---|
| 75 | #("Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday")) |
|---|
| 76 | |
|---|
| 77 | (defconstant *japanese-weekday-names* "月火水木金土日") |
|---|
| 78 | |
|---|
| 79 | (defconstant *abbreviated-month-names* |
|---|
| 80 | #("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")) |
|---|
| 81 | |
|---|
| 82 | (defconstant *full-month-names* |
|---|
| 83 | #("January" "February" "March" "April" "May" "June" |
|---|
| 84 | "July" "August" "September" "October" "November" "December")) |
|---|
| 85 | |
|---|
| 86 | (defvar *timezone-name* "JST") |
|---|
| 87 | |
|---|
| 88 | ;; 元号と西暦の対応表(たぶん合ってる) |
|---|
| 89 | (defconstant *japanese-era-list* |
|---|
| 90 | '(("平成" "H" 1989 1 8) |
|---|
| 91 | ("昭和" "S" 1926 12 25) |
|---|
| 92 | ("大正" "T" 1912 7 30) |
|---|
| 93 | ;; Common LispではGMT1900年より前は存在しない |
|---|
| 94 | ;; ("明治" "M" 1868 5 9) |
|---|
| 95 | )) |
|---|
| 96 | |
|---|
| 97 | (defconstant *japanese-era* |
|---|
| 98 | (mapcar #'(lambda (x) |
|---|
| 99 | (list (encode-universal-time |
|---|
| 100 | 0 0 0 (fifth x) (fourth x) (third x) -9) |
|---|
| 101 | (third x) (first x) (second x))) |
|---|
| 102 | *japanese-era-list*)) |
|---|
| 103 | |
|---|
| 104 | (defun get-japanese-era (universal-time year) |
|---|
| 105 | (let ((x (find universal-time *japanese-era* :test #'>= :key #'car))) |
|---|
| 106 | (if x |
|---|
| 107 | (cons (+ (- year (cadr x)) 1) (cddr x)) |
|---|
| 108 | (list (- year 1867) "明治" "M") ; いまいち |
|---|
| 109 | ))) |
|---|
| 110 | |
|---|
| 111 | (defun format-date (s fmt &optional (universal-time (get-universal-time))) |
|---|
| 112 | (multiple-value-bind (sec min hour day mon year dow daylight tz) |
|---|
| 113 | (decode-universal-time universal-time) |
|---|
| 114 | (declare (ignore daylight)) |
|---|
| 115 | (do ((i 0 (+ i 1)) |
|---|
| 116 | (l (length fmt)) |
|---|
| 117 | (jp nil)) |
|---|
| 118 | ((= i l)) |
|---|
| 119 | (let ((c (elt fmt i))) |
|---|
| 120 | (cond ((char= c #\%) |
|---|
| 121 | (let ((pound nil)) |
|---|
| 122 | (incf i) |
|---|
| 123 | (when (= i l) |
|---|
| 124 | (return)) |
|---|
| 125 | (setq c (elt fmt i)) |
|---|
| 126 | (when (char= c #\#) |
|---|
| 127 | (setq pound t) |
|---|
| 128 | (incf i) |
|---|
| 129 | (when (= i l) |
|---|
| 130 | (return)) |
|---|
| 131 | (setq c (elt fmt i))) |
|---|
| 132 | (let ((fmtd (if pound "~d" "~2,'0d"))) |
|---|
| 133 | (case c |
|---|
| 134 | (#\a |
|---|
| 135 | (princ (svref *abbreviated-weekday-names* dow) s)) |
|---|
| 136 | (#\A |
|---|
| 137 | (princ (svref *full-weekday-names* dow) s)) |
|---|
| 138 | (#\b |
|---|
| 139 | (princ (svref *abbreviated-month-names* (- mon 1)) s)) |
|---|
| 140 | (#\B |
|---|
| 141 | (princ (svref *full-month-names* (- mon 1)) s)) |
|---|
| 142 | (#\d |
|---|
| 143 | (format s fmtd day)) |
|---|
| 144 | (#\e |
|---|
| 145 | (unless jp |
|---|
| 146 | (setq jp (get-japanese-era universal-time year))) |
|---|
| 147 | (format s fmtd (car jp))) |
|---|
| 148 | (#\E |
|---|
| 149 | (unless jp |
|---|
| 150 | (setq jp (get-japanese-era universal-time year))) |
|---|
| 151 | (if (= (car jp) 1) |
|---|
| 152 | (princ "元" s) |
|---|
| 153 | (format s fmtd (car jp)))) |
|---|
| 154 | (#\g |
|---|
| 155 | (unless jp |
|---|
| 156 | (setq jp (get-japanese-era universal-time year))) |
|---|
| 157 | (princ (if pound (svref (cadr jp) 0) (cadr jp)) s)) |
|---|
| 158 | (#\G |
|---|
| 159 | (unless jp |
|---|
| 160 | (setq jp (get-japanese-era universal-time year))) |
|---|
| 161 | (princ (caddr jp) s)) |
|---|
| 162 | (#\H |
|---|
| 163 | (format s fmtd hour)) |
|---|
| 164 | (#\I |
|---|
| 165 | (let ((h (mod hour 12))) |
|---|
| 166 | (format s fmtd (if (zerop h) 12 h)))) |
|---|
| 167 | (#\i |
|---|
| 168 | (format s "~3,'0d" |
|---|
| 169 | (truncate (rem (+ universal-time 3600) 86400) 86.4))) |
|---|
| 170 | (#\m |
|---|
| 171 | (format s fmtd mon)) |
|---|
| 172 | (#\M |
|---|
| 173 | (format s fmtd min)) |
|---|
| 174 | (#\p |
|---|
| 175 | (princ (if (< hour 12) "午前" "午後") s)) |
|---|
| 176 | (#\P |
|---|
| 177 | (if pound |
|---|
| 178 | (princ (if (< hour 12) "am" "pm") s) |
|---|
| 179 | (princ (if (< hour 12) "AM" "PM") s))) |
|---|
| 180 | (#\S |
|---|
| 181 | (format s fmtd sec)) |
|---|
| 182 | (#\v |
|---|
| 183 | (princ (aref *japanese-weekday-names* dow) s)) |
|---|
| 184 | (#\y |
|---|
| 185 | (format s "~2,'0d" (mod year 100))) |
|---|
| 186 | (#\Y |
|---|
| 187 | (princ year s)) |
|---|
| 188 | (#\z |
|---|
| 189 | (format s "~A~D" *timezone-name* tz)) |
|---|
| 190 | (#\Z |
|---|
| 191 | (let ((x (abs tz))) |
|---|
| 192 | (format s "~:[+~;-~]~2,'0d~:[~;:~]~2,'0d" |
|---|
| 193 | (plusp tz) (truncate x) pound |
|---|
| 194 | (mod (truncate (* x 60)) 60)))) |
|---|
| 195 | (t |
|---|
| 196 | (write-char c s)))))) |
|---|
| 197 | (t |
|---|
| 198 | (write-char c s))))))) |
|---|
| 199 | |
|---|
| 200 | (defun format-date-string (fmt &optional universal-time) |
|---|
| 201 | (with-output-to-string (s) |
|---|
| 202 | (format-date s fmt universal-time))) |
|---|
| 203 | |
|---|
| 204 | (defvar *date-formats* |
|---|
| 205 | '("%a, %d %b %Y %H:%M:%S %Z" |
|---|
| 206 | "%a, %d %b %Y %H:%M:%S %z" |
|---|
| 207 | "%a %b %d %H:%M:%S %Y" |
|---|
| 208 | "%d %b %Y %H:%M:%S %Z" |
|---|
| 209 | "%d %b %Y %H:%M:%S %z" |
|---|
| 210 | "%Y-%m-%dT%H:%M:%S%#Z" |
|---|
| 211 | "%B %d, %Y" |
|---|
| 212 | "%b %d %Y" |
|---|
| 213 | "%Y-%m-%d" |
|---|
| 214 | "%d %b %y" |
|---|
| 215 | "%y/%m/%d" |
|---|
| 216 | "%y-%m-%d" |
|---|
| 217 | "%g%#e年%#m月%#d日 %v曜日" |
|---|
| 218 | "%g%#e年%#m月%#d日" |
|---|
| 219 | "%Y年%#m月%#d日(%v)" |
|---|
| 220 | "%Y年%#m月%#d日" |
|---|
| 221 | "%y年%#m月%#d日(%v)" |
|---|
| 222 | "%y年%#m月%#d日" |
|---|
| 223 | " %H:%M:%S" |
|---|
| 224 | " %#H:%M:%S" |
|---|
| 225 | " %#I:%M:%S %P" |
|---|
| 226 | " %#H時%#M分%#S秒" |
|---|
| 227 | " %p%#I時%#M分%#S秒" |
|---|
| 228 | "@%i" |
|---|
| 229 | )) |
|---|
| 230 | |
|---|
| 231 | (defun add-date-format (fmt) |
|---|
| 232 | (pushnew fmt *date-formats* :test #'string=)) |
|---|
| 233 | |
|---|
| 234 | #|(defun insert-date-string () |
|---|
| 235 | (interactive "*") |
|---|
| 236 | (multiple-value-bind (result data) |
|---|
| 237 | (dialog-box '(dialog 0 0 260 120 |
|---|
| 238 | (:caption "日付と時刻") |
|---|
| 239 | (:font 9 "MS UI Gothic") |
|---|
| 240 | (:control |
|---|
| 241 | (:listbox list nil #x50a10001 4 5 192 114) |
|---|
| 242 | (:button IDOK "OK" #x50030001 205 5 52 14) |
|---|
| 243 | (:button IDCANCEL "キャンセル" #x50030000 205 22 52 14))) |
|---|
| 244 | (list (cons 'list (mapcar #'format-date-string *date-formats*))) |
|---|
| 245 | '((list :must-match t :enable (IDOK)))) |
|---|
| 246 | (when result |
|---|
| 247 | (insert (cdr (assoc 'list data))))))|# |
|---|
| 248 | |
|---|
| 249 | (defvar *date-format-regexp* |
|---|
| 250 | (ppcre:create-scanner "([0-9][0-9]?)/([0-9][0-9]?)/([0-9][0-9]?) +([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)")) |
|---|
| 251 | |
|---|
| 252 | (defun parse-date-string (string) |
|---|
| 253 | (when (and (stringp string) |
|---|
| 254 | (ppcre:scan *date-format-regexp* string)) |
|---|
| 255 | (handler-case |
|---|
| 256 | (apply #'encode-universal-time |
|---|
| 257 | (nreverse |
|---|
| 258 | (map 'list #'parse-integer |
|---|
| 259 | (nth-value 1 (ppcre:scan-to-strings *date-format-regexp* string))))) |
|---|
| 260 | (error () nil)))) |
|---|
| 261 | |
|---|
| 262 | ; usage |
|---|
| 263 | ;(format-date-string (nth 12 *date-formats*) |
|---|
| 264 | ; (get-universal-time)) |
|---|
| 265 | ;"平成20年8月19日 火曜日" |
|---|