2010-12-06

W3C-DTF文字列をSRFI 19の日付オブジェクトに変換する関数

WiLiKiのrssmix.scmを参考に、W3C-DTFの文字列をSRFI 19の日付オブジェクトに変換する関数を書いた。

(use srfi-1)
(use srfi-13)
(use srfi-19)

(define (w3cdtf->date str)
  (define (df->nano df)
    (string->number (string-pad-right (number->string df) 9 #\0)))
  (and-let*
      ((match (#/^(\d\d\d\d)(?:-(\d\d)(?:-(\d\d)(?:T(\d\d):(\d\d)(?::(\d\d)(?:\.(\d+))?)?(?:Z|([+-]\d\d):(\d\d)))?)?)?$/ str)))
    (receive (year month day hour minute second df zh zm)
        (apply values (map (lambda (i) (x->integer (match i))) (iota 9 1)))
      (make-date (df->nano df)
                 second minute hour day month year
                 (* (if (negative? zh) -1 1)
                    (+ (* (abs zh) 3600) (* zm 60)))))))

0 件のコメント: