;;; Code:
(require 'cl-lib)
+(require 'iso8601)
+(eval-when-compile (require 'subr-x))
;; Byte-compiler warnings
(defvar parse-time-elt)
(setf (nth (pop slots) time) new-val))))))))
time))
-(defconst parse-time-iso8601-regexp
- (let* ((dash "-?")
- (colon ":?")
- (4digit "\\([0-9][0-9][0-9][0-9]\\)")
- (2digit "\\([0-9][0-9]\\)")
- (date-fullyear 4digit)
- (date-month 2digit)
- (date-mday 2digit)
- (time-hour 2digit)
- (time-minute 2digit)
- (time-second 2digit)
- (time-secfrac "\\(\\.[0-9]+\\)?")
- (time-numoffset (concat "\\([-+]\\)" time-hour ":?" time-minute "?"))
- (partial-time (concat time-hour colon time-minute colon time-second
- time-secfrac))
- (full-date (concat date-fullyear dash date-month dash date-mday)))
- (list (concat "^" full-date)
- (concat "T" partial-time)
- (concat "\\(Z\\|" time-numoffset "\\)")))
- "List of regular expressions matching ISO 8601 dates.
-1st regular expression matches the date.
-2nd regular expression matches the time.
-3rd regular expression matches the (optional) timezone specification.")
-
(defun parse-iso8601-time-string (date-string)
"Parse an ISO 8601 time string, such as 2016-12-01T23:35:06-05:00.
If DATE-STRING cannot be parsed, it falls back to
`parse-time-string'."
- (let* ((date-re (nth 0 parse-time-iso8601-regexp))
- (time-re (nth 1 parse-time-iso8601-regexp))
- (tz-re (nth 2 parse-time-iso8601-regexp))
- re-start
- time seconds minute hour
- day month year day-of-week (dst -1) tz)
- ;; We need to populate 'time' with
- ;; (SEC MIN HOUR DAY MON YEAR DOW DST TZ)
-
- ;; Nobody else handles iso8601 correctly, let's do it ourselves.
- (when (string-match date-re date-string re-start)
- (setq year (string-to-number (match-string 1 date-string))
- month (string-to-number (match-string 2 date-string))
- day (string-to-number (match-string 3 date-string))
- re-start (match-end 0))
- (when (string-match time-re date-string re-start)
- (setq hour (string-to-number (match-string 1 date-string))
- minute (string-to-number (match-string 2 date-string))
- seconds (string-to-number (match-string 3 date-string))
- re-start (match-end 0))
- (when (string-match tz-re date-string re-start)
- (setq dst nil)
- (setq tz (if (string= "Z" (match-string 1 date-string))
- 0 ;; UTC timezone indicated by Z
- (let ((tz (+
- (* 3600
- (string-to-number
- (match-string 3 date-string)))
- (* 60
- (string-to-number
- (or (match-string 4 date-string) "0"))))))
- (if (string= "-" (match-string 2 date-string))
- (- tz) tz)))))
- (setq time (list seconds minute hour day month year day-of-week dst tz))))
-
- ;; Fall back to having `parse-time-string' do fancy things for us.
- (when (not time)
- (setq time (parse-time-string date-string)))
-
- (and time
- (encode-time time))))
+ (when-let ((time
+ (if (iso8601-valid-p date-string)
+ (decoded-time-set-defaults (iso8601-parse date-string))
+ ;; Fall back to having `parse-time-string' do fancy
+ ;; things for us.
+ (parse-time-string date-string))))
+ (encode-time time)))
(provide 'parse-time)