Function: htz:date-parse

htz:date-parse is a byte-compiled function defined in htz.el.

Signature

(htz:date-parse DATE &optional PARSED-CURRENT-DATE)

Documentation

Parse DATE string and return a vector [year month day time timezone].

If a two-digit year, the first two digits of the current year are prepended. Timezone in DATE is optional, it defaults to the value of htz:local.

Recognizes the following styles:
 (1) "(1 30 1999)" calendar-julian-date requires PARSED-CURRENT-DATE arg
 (2) "14 Apr 89 03:20[:12] [GMT]"
 (3) "Fri, 17 Mar [19]89 4:01[:33] [GMT]"
 (4) "Mon Jan 16 16:12[:37] [GMT] 1989"
 (5) "19911014:07:51:08 or 1991101407:51:08" sortable date
 (6) "Mar 29 14:00" ls -l date requires PARSED-CURRENT-DATE arg
 (7) "Mar 7 1994" ls -l date requires PARSED-CURRENT-DATE arg

Source Code

;; Defined in ~/.emacs.d/elpa/hyperbole-20260414.325/htz.el
(defun htz:date-parse (date &optional parsed-current-date)
  "Parse DATE string and return a vector [year month day time timezone].
If a two-digit year, the first two digits of the current year are
prepended.  Timezone in DATE is optional, it defaults to the
value of `htz:local'.

Recognizes the following styles:
 (1) \"(1 30 1999)\" `calendar-julian-date' requires PARSED-CURRENT-DATE arg
 (2) \"14 Apr 89 03:20[:12] [GMT]\"
 (3) \"Fri, 17 Mar [19]89 4:01[:33] [GMT]\"
 (4) \"Mon Jan 16 16:12[:37] [GMT] 1989\"
 (5) \"19911014:07:51:08 or 1991101407:51:08\"  `sortable date'
 (6) \"Mar 29 14:00\"    `ls -l date'  requires PARSED-CURRENT-DATE arg
 (7) \"Mar  7  1994\"    `ls -l date'  requires PARSED-CURRENT-DATE arg"
  (let ((date (or date ""))
	year month day time
	zone)			; This may be nil.
    (if (listp date)
	(setq month (nth 0 date)
	      day   (nth 1 date)
	      year  (nth 2 date))
      (cond ((string-match
	      "\\`(\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\))\\'" date)
	     ;; Style (1)
	     (setq year 3 month 1 day 2 time nil zone nil))
	    ((string-match
	      ;; Allow for 3 digits in hour to handle prior error in
	      ;; generating hours fixed on 2019-06-10; 3rd digit
	      ;; removed in htz:time-make-string and htz:time-parse.
	      "\\([0-9][0-9][0-9][0-9]\\)\\([0-1][0-9]\\)\\([0-3][0-9]\\):?\\([0-9][0-9][0-9]?:[0-5][0-9:]+\\)[ ]*\\'" date)
	     ;; Style (5)
	     (setq year 1 month 2 day 3 time 4 zone nil))
	    ((string-match
	      "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\'" date)
	     ;; Styles: (2) and (3) without timezone
	     (setq year 3 month 2 day 1 time 4 zone nil))
	    ((string-match
	      "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\([-+a-zA-Z0-9]+\\)" date)
	     ;; Styles: (2) and (3) with timezone and buggy timezone
	     (setq year 3 month 2 day 1 time 4 zone 5))
	    ((string-match
	      "\\([^ ,]+\\) +\\([0-9]+\\) \\([0-9]+:[0-9:]+\\(:[0-9]+\\)?\\) \\([0-9]+\\)" date)
	     ;; Styles: (4) without timezone
	     (setq year 5 month 1 day 2 time 3 zone nil))
	    ((string-match
	      "\\([^ ,]+\\) +\\([0-9]+\\) \\([0-9]+:[0-9:]+\\(:[0-9]+\\)?\\) \\([-+a-zA-Z0-9]+\\) \\([0-9]+\\)" date)
	     ;; Styles: (4) with timezone
	     (setq year 6 month 1 day 2 time 3 zone 5))
	    ((string-match "^\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9]+:[0-9:]+\\)$" date)
	     ;; Style: (6)
	     (setq year nil month 1 day 2 time 3 zone nil))
	    ((string-match
	      "^\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9][0-9][0-9][0-9]\\)$" date)
	     ;; Style: (7)
	     (setq year 3 month 1 day 2 time nil zone nil))
	    (t (error "(htz:date-parse): Invalid date format: `%s'" date)))
      (if year
	  (setq year
		(substring date (match-beginning year) (match-end year))
		year (if (/= (length year) 2) year
		       (let* ((yr (substring (current-time-string) -4))
			      (curr-yr (substring yr 2))
			      (century (substring yr 0 2)))
			 (concat (if (string< curr-yr yr)
				     (format "%02d"
					     (1- (string-to-number century)))
				   century)
				 year))))
	(setq year (if (vectorp parsed-current-date)
		       (aref parsed-current-date 0)
		     "0")))
      (if month
	  (setq month (substring date
				 (match-beginning month) (match-end month))
		month (if (/= (string-to-number month) 0) month
			(int-to-string
			 (cdr (assoc (upcase month) htz:months-assoc)))))
	(setq month (if (vectorp parsed-current-date)
			(aref parsed-current-date 1)
		      "0")))
      (if day
	  (setq day (substring date (match-beginning day) (match-end day)))
	(setq day (if (vectorp parsed-current-date)
		      (aref parsed-current-date 2)
		    "0"))))
    (if time
	(setq time (substring date (match-beginning time) (match-end time)))
      (setq time (if (vectorp parsed-current-date)
		     (aref parsed-current-date 3))))
    (if zone
	(setq zone (substring date (match-beginning zone) (match-end zone)))
      (setq zone (if (vectorp parsed-current-date)
		     (aref parsed-current-date 4)
		   htz:local)))
    ;; Return a vector.
    (vector year month day time zone)))