SimpleDateFormat
我发现在 common lisp中没有像 Java 这样强大的工具。
原理我太难理解了SimpleDateFormat
,只好自己写了。我写了一个函数'date-formatter',但我认为它太丑了并且存在一些错误。
你能解释一下如何SimpleDateFormat
实现或提供强大的功能吗
(defparameter *pattern-char* "yMdhHmsS")
功能DATE-FORMATTER
:
(defun date-formatter (pattern str)
(let ((year nil)
(month nil)
(day nil)
(hour nil)
(minite nil)
(second nil)
(index nil))
(do* ((index 0 (+ 1 index))
(c (subseq *pattern-char* index (+ 1 index))
(subseq *pattern-char* index (1+ index)))
(p (concatenate 'string c "+")
(concatenate 'string c "+"))
(m (cl-ppcre:scan-to-strings p pattern)
(cl-ppcre:scan-to-strings p pattern)))
((= index (1- (length *pattern-char*))))
(if m
(let* ((i (subseq str (search m pattern)
(+ (length m) (search m pattern)))))
(cond ((< 0 (length (cl-ppcre:scan-to-strings "y+" m)))
(setf year (parse-integer i)))
((< 0 (length (cl-ppcre:scan-to-strings "M*" m)))
(setf month (parse-integer i)))
((< 0 (length (cl-ppcre:scan-to-strings "d*" m)))
(setf day (parse-integer i)))
((< 0 (length (cl-ppcre:scan-to-strings "H*" m)))
(setf hour (parse-integer i)))
((< 0 (length (cl-ppcre:scan-to-strings "m*" m)))
(setf minite (parse-integer i)))
((< 0 (length (cl-ppcre:scan-to-strings "s*" m)))
(setf second (parse-integer i)))
(t (print "error!!"))))))
(values year month day hour minite second)))
例子:
(declaim (optimize (speed 0) (safety 3) (debug 3)))
(defun f ()
(multiple-value-bind (year month day hour minute second)
(date-formatter "yyyy-MM-dd HH:mm:ss" "2013-05-12 23:22:11")
(list year month day hour minute second )))