Function: ls-lisp-format-time

ls-lisp-format-time is a byte-compiled function defined in ls-lisp.el.gz.

Signature

(ls-lisp-format-time FILE-ATTR TIME-INDEX)

Documentation

Format time for file with attributes FILE-ATTR according to TIME-INDEX.

Use the same method as ls to decide whether to show time-of-day or year, depending on distance between file date and the current time. All ls time options, namely c, t and u, are handled.

This function determines as side effect the locale relevant for displaying times, by using system-time-locale if non-nil, and falling back to environment variables LC_ALL, LC_TIME, and LANG.

Source Code

;; Defined in /usr/src/emacs/lisp/ls-lisp.el.gz
(defun ls-lisp-format-time (file-attr time-index)
  "Format time for file with attributes FILE-ATTR according to TIME-INDEX.
Use the same method as ls to decide whether to show time-of-day or year,
depending on distance between file date and the current time.
All ls time options, namely c, t and u, are handled.

This function determines as side effect the locale relevant for
displaying times, by using `system-time-locale' if non-nil, and
falling back to environment variables LC_ALL, LC_TIME, and LANG."
  (let* ((time (nth (or time-index 5) file-attr)) ; default is last modtime
	 (diff (time-subtract time nil))
	 ;; Consider a time to be recent if it is within the past six
	 ;; months.  A Gregorian year has 365.2425 * 24 * 60 * 60 ==
	 ;; 31556952 seconds on the average, and half of that is 15778476.
	 ;; Write the constant explicitly to avoid roundoff error.
	 (past-cutoff -15778476)) ; half a Gregorian year
    (condition-case nil
	;; Use traditional time format in the C or POSIX locale,
	;; ISO-style time format otherwise, so columns line up.
	(let ((locale (or system-time-locale ls-lisp--time-locale)))
	  (if (not locale)
	      (let ((vars '("LC_ALL" "LC_TIME" "LANG")))
		(while (and vars (not (setq locale (getenv (car vars)))))
		  (setq vars (cdr vars)))
                ;; Cache the locale for next calls.
                (setq ls-lisp--time-locale (or locale "C"))))
	  (if (member locale '("C" "POSIX"))
	      (setq locale nil))
	  (format-time-string
	   (if (and (not (time-less-p diff past-cutoff))
		    (not (time-less-p 0 diff)))
	       (if (and locale (not ls-lisp-use-localized-time-format))
		   "%m-%d %H:%M"
		 (nth 0 ls-lisp-format-time-list))
	     (if (and locale (not ls-lisp-use-localized-time-format))
		 "%Y-%m-%d "
	       (nth 1 ls-lisp-format-time-list)))
	   time))
      (error "Unk  0  0000"))))