From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Lars Ingebrigtsen Newsgroups: gmane.emacs.bugs Subject: bug#32931: time-stamp-format: offer numeric time zones too Date: Thu, 11 Jul 2019 19:01:02 +0200 Message-ID: <871ryw33qp.fsf@mouse.gnus.org> References: <87zhvvwatd.fsf@jidanni.org> <877e8o36id.fsf@mouse.gnus.org> <874l3s367v.fsf@mouse.gnus.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="141284"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: 32931@debbugs.gnu.org To: =?UTF-8?Q?=E7=A9=8D=E4=B8=B9=E5=B0=BC?= Dan Jacobson Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Jul 11 19:02:13 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hlcST-000abC-1O for geb-bug-gnu-emacs@m.gmane.org; Thu, 11 Jul 2019 19:02:13 +0200 Original-Received: from localhost ([::1]:43666 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlcSR-0001BM-VD for geb-bug-gnu-emacs@m.gmane.org; Thu, 11 Jul 2019 13:02:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53807) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hlcSN-0001Ad-2y for bug-gnu-emacs@gnu.org; Thu, 11 Jul 2019 13:02:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hlcSL-0003kD-3j for bug-gnu-emacs@gnu.org; Thu, 11 Jul 2019 13:02:06 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:57952) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hlcSI-0003jK-M9 for bug-gnu-emacs@gnu.org; Thu, 11 Jul 2019 13:02:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hlcSI-0004RS-GH for bug-gnu-emacs@gnu.org; Thu, 11 Jul 2019 13:02:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Lars Ingebrigtsen Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 11 Jul 2019 17:02:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 32931 X-GNU-PR-Package: emacs Original-Received: via spool by 32931-submit@debbugs.gnu.org id=B32931.156286447217002 (code B ref 32931); Thu, 11 Jul 2019 17:02:02 +0000 Original-Received: (at 32931) by debbugs.gnu.org; 11 Jul 2019 17:01:12 +0000 Original-Received: from localhost ([127.0.0.1]:38540 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hlcRT-0004QA-Qt for submit@debbugs.gnu.org; Thu, 11 Jul 2019 13:01:12 -0400 Original-Received: from quimby.gnus.org ([80.91.231.51]:58006) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hlcRQ-0004Pz-Hl for 32931@debbugs.gnu.org; Thu, 11 Jul 2019 13:01:10 -0400 Original-Received: from cm-84.212.202.86.getinternet.no ([84.212.202.86] helo=marnie) by quimby.gnus.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hlcRK-0005c6-Rd; Thu, 11 Jul 2019 19:01:05 +0200 In-Reply-To: <874l3s367v.fsf@mouse.gnus.org> (Lars Ingebrigtsen's message of "Thu, 11 Jul 2019 18:07:32 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:162721 Archived-At: Below is as far as I got in rewriting to use format-spec instead.... but there are many strange things in the time-stamp gallery of format specs: %:a weekday name: `Monday'. %#A gives uppercase: `MONDAY' OK... %3a abbreviated weekday: `Mon'. %3A gives uppercase: `MON' Well, sure. %:b month name: `January'. %#B gives uppercase: `JANUARY' %3b abbreviated month: `Jan'. %3B gives uppercase: `JAN' [...] %#p `am' or `pm'. %P gives uppercase: `AM' or `PM' WTF! Well, all of this can be translated into format-spec + format-time-string things; I'll find the time some day, I guess... diff --git a/lisp/time-stamp.el b/lisp/time-stamp.el index f423683852..8157abbb3c 100644 --- a/lisp/time-stamp.el +++ b/lisp/time-stamp.el @@ -36,6 +36,8 @@ ;;; Code: +(require 'format-spec) + (defgroup time-stamp nil "Maintain last change time stamps in files edited by Emacs." :group 'data @@ -413,192 +415,33 @@ time-stamp-string (defconst time-stamp-no-file "(no file)" "String to use when the buffer is not associated with a file.") -;;; FIXME This comment was written in 1996! -;;; time-stamp is transitioning to using the new, expanded capabilities -;;; of format-time-string. During the process, this function implements -;;; intermediate, compatible formats and complains about old, soon to -;;; be unsupported, formats. This function will get a lot (a LOT) shorter -;;; when the transition is complete and we can just pass most things -;;; straight through to format-time-string. -;;; At all times, all the formats recommended in the doc string -;;; of time-stamp-format will work not only in the current version of -;;; Emacs, but in all versions that have been released within the past -;;; two years. -;;; The : modifier is a temporary conversion feature used to resolve -;;; ambiguous formats--formats that are changing (over time) incompatibly. (defun time-stamp-string-preprocess (format &optional time) "Use a FORMAT to format date, time, file, and user information. Optional second argument TIME is only for testing. Implements non-time extensions to `format-time-string' and all `time-stamp-format' compatibility." - (let ((fmt-len (length format)) - (ind 0) - cur-char - (prev-char nil) - (result "") - field-width - field-result - alt-form change-case - (paren-level 0)) - (while (< ind fmt-len) - (setq cur-char (aref format ind)) - (setq - result - (concat result - (cond - ((eq cur-char ?%) - ;; eat any additional args to allow for future expansion - (setq alt-form nil change-case nil field-width "") - (while (progn - (setq ind (1+ ind)) - (setq cur-char (if (< ind fmt-len) - (aref format ind) - ?\0)) - (or (eq ?. cur-char) - (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char) - (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char) - (eq ?\s cur-char) (eq ?# cur-char) (eq ?^ cur-char) - (and (eq ?\( cur-char) - (not (eq prev-char ?\\)) - (setq paren-level (1+ paren-level))) - (if (and (eq ?\) cur-char) - (not (eq prev-char ?\\)) - (> paren-level 0)) - (setq paren-level (1- paren-level)) - (and (> paren-level 0) - (< ind fmt-len))) - (if (and (<= ?0 cur-char) (>= ?9 cur-char)) - ;; get format width - (let ((field-index ind)) - (while (progn - (setq ind (1+ ind)) - (setq cur-char (if (< ind fmt-len) - (aref format ind) - ?\0)) - (and (<= ?0 cur-char) (>= ?9 cur-char)))) - (setq field-width (substring format field-index ind)) - (setq ind (1- ind)) - t)))) - (setq prev-char cur-char) - ;; some characters we actually use - (cond ((eq cur-char ?:) - (setq alt-form t)) - ((eq cur-char ?#) - (setq change-case t)))) - (setq field-result - (cond - ((eq cur-char ?%) - "%%") - ((eq cur-char ?a) ;day of week - (if change-case - (time-stamp--format "%#a" time) - (or alt-form (not (string-equal field-width "")) - (time-stamp-conv-warn "%a" "%:a")) - (if (and alt-form (not (string-equal field-width ""))) - "" ;discourage "%:3a" - (time-stamp--format "%A" time)))) - ((eq cur-char ?A) - (if alt-form - (time-stamp--format "%A" time) - (or change-case (not (string-equal field-width "")) - (time-stamp-conv-warn "%A" "%#A")) - (time-stamp--format "%#A" time))) - ((eq cur-char ?b) ;month name - (if change-case - (time-stamp--format "%#b" time) - (or alt-form (not (string-equal field-width "")) - (time-stamp-conv-warn "%b" "%:b")) - (if (and alt-form (not (string-equal field-width ""))) - "" ;discourage "%:3b" - (time-stamp--format "%B" time)))) - ((eq cur-char ?B) - (if alt-form - (time-stamp--format "%B" time) - (or change-case (not (string-equal field-width "")) - (time-stamp-conv-warn "%B" "%#B")) - (time-stamp--format "%#B" time))) - ((eq cur-char ?d) ;day of month, 1-31 - (time-stamp-do-number cur-char alt-form field-width time)) - ((eq cur-char ?H) ;hour, 0-23 - (time-stamp-do-number cur-char alt-form field-width time)) - ((eq cur-char ?I) ;hour, 1-12 - (time-stamp-do-number cur-char alt-form field-width time)) - ((eq cur-char ?m) ;month number, 1-12 - (time-stamp-do-number cur-char alt-form field-width time)) - ((eq cur-char ?M) ;minute, 0-59 - (time-stamp-do-number cur-char alt-form field-width time)) - ((eq cur-char ?p) ;am or pm - (or change-case - (time-stamp-conv-warn "%p" "%#p")) - (time-stamp--format "%#p" time)) - ((eq cur-char ?P) ;AM or PM - (time-stamp--format "%p" time)) - ((eq cur-char ?S) ;seconds, 00-60 - (time-stamp-do-number cur-char alt-form field-width time)) - ((eq cur-char ?w) ;weekday number, Sunday is 0 - (time-stamp--format "%w" time)) - ((eq cur-char ?y) ;year - (or alt-form (not (string-equal field-width "")) - (time-stamp-conv-warn "%y" "%:y")) - (string-to-number (time-stamp--format "%Y" time))) - ((eq cur-char ?Y) ;4-digit year, new style - (string-to-number (time-stamp--format "%Y" time))) - ((eq cur-char ?z) ;time zone lower case - (if change-case - "" ;discourage %z variations - (time-stamp--format "%#Z" time))) - ((eq cur-char ?Z) - (if change-case - (time-stamp--format "%#Z" time) - (time-stamp--format "%Z" time))) - ((eq cur-char ?f) ;buffer-file-name, base name only - (if buffer-file-name + (format-spec + format + `((?f . ;buffer-file-name, base name only + ,(if buffer-file-name (file-name-nondirectory buffer-file-name) time-stamp-no-file)) - ((eq cur-char ?F) ;buffer-file-name, full path - (or buffer-file-name + (?F . ;buffer-file-name, full path + ,(or buffer-file-name time-stamp-no-file)) - ((eq cur-char ?s) ;system name - (system-name)) - ((eq cur-char ?u) ;user name - (user-login-name)) - ((eq cur-char ?U) ;user full name - (user-full-name)) - ((eq cur-char ?l) ;logname (undocumented user name alt) - (user-login-name)) - ((eq cur-char ?L) ;(undocumented alt user full name) - (user-full-name)) - ((eq cur-char ?h) ;mail host name - (or mail-host-address (system-name))) - ((eq cur-char ?q) ;(undocumented unqual hostname) - (let ((qualname (system-name))) + (?s . ,(system-name)) ; system name + (?u . ,(user-login-name)) ;user name + (?U . ,(user-full-name)) ;user full name + (?l . ,(user-login-name)) ;logname (undocumented user name alt) + (?L . ,(user-full-name)) ;(undocumented alt user full name) + (?h . ,(or mail-host-address (system-name))) ;mail host name + (?q . ;(undocumented unqual hostname) + ,(let ((qualname (system-name))) (if (string-match "\\." qualname) (substring qualname 0 (match-beginning 0)) qualname))) - ((eq cur-char ?Q) ;(undocumented fully-qualified host) - (system-name)) - )) - (let ((padded-result - (format (format "%%%s%c" - field-width - (if (numberp field-result) ?d ?s)) - (or field-result "")))) - (let* ((initial-length (length padded-result)) - (desired-length (if (string-equal field-width "") - initial-length - (string-to-number field-width)))) - (if (> initial-length desired-length) - ;; truncate strings on right, years on left - (if (stringp field-result) - (substring padded-result 0 desired-length) - (if (eq cur-char ?y) - (substring padded-result (- desired-length)) - padded-result)) ;non-year numbers don't truncate - padded-result)))) - (t - (char-to-string cur-char))))) - (setq ind (1+ ind))) - result)) + (?Q , (system-name))) ;(undocumented fully-qualified host) + t)) (defun time-stamp-do-number (format-char alt-form field-width time) "Handle compatible FORMAT-CHAR where only default width/padding will change. -- (domestic pets only, the antidote for overdose, milk.) bloggy blog: http://lars.ingebrigtsen.no