From: Lady Date: Tue, 21 May 2024 06:06:07 +0000 (-0400) Subject: Use diff to get better dates X-Git-Tag: 0.9.1~3 X-Git-Url: https://git.ladys.computer/Shushe/commitdiff_plain/8ec9a45141899d218b31c488fdbc8f820800ee69?hp=61599492d0451c48f963a13a78a11a59415eb6f0 Use diff to get better dates See the comments for more information, including Macintosh quirks. This is a great deal better than using `ls` and allows for the dropping of the latter as a dependency. --- diff --git a/GNUmakefile b/GNUmakefile index 2c77723..6779aa7 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -22,12 +22,12 @@ override define makefileinfo ║│ • cksum │║ ║│ • cp │║ ║│ • date │║ +║│ • diff │║ ║│ • file │║ ║│ • find │║ ║│ • grep │║ ║│ • git (optional) │║ ║│ • ln │║ -║│ • ls │║ ║│ • mkdir │║ ║│ • mv │║ ║│ • od │║ @@ -102,12 +102,12 @@ CAT := cat CKSUM := cksum CP := cp DATE := date +DIFF := diff FILE := file FIND := find GIT := git GREP := grep LN := ln -LS := ls MKDIR := mkdir MV := mv OD := od @@ -255,17 +255,13 @@ override quote = '$(subst ','"'"',$1)' # (callable) Get the modified time of the provided file. # -# Only the date·time information available via `ls -l´ is supported; that is, only minute precision for files newer than six months, and only day precision for files which are older. -# This is, unfortunately, the best that can be done in pure Posix as of now. +# This touches a file containing only a newline and then diffs it with `/dev/null´ and extracts the timestamp from the diff. +# Interestingly, on Macintosh, the format of `diff -u´ is only Posixy (including a fractional component and timezone) when `COMMAND_MODE=legacy´; however, the timezone will always be zero and the fractional component is ignorable, so it’s not necessary to worry about that here. # -# There is an easier implementation which provides the full date·time by archiving an empty file and then reading it back out of the archive :— -# -# $(shell LC_TIME=POSIX TZ=UTC0 $(TOUCH) -m -r $(callquote,$1) $(BUILDDIR)/.mtime && $(PAX) -w -x ustar $(BUILDDIR)/.mtime | $(PAX) -v -o 'listopt=%(mtime=%Y-%m-%dT%H:%M:%SZ)T') -# -# However, it depends on the `listopt´ option in `pax´, which (despite being specified in Posix) no implementations actually support. +# The diff will always have an exit status of 1, but this is ignored by piping into Sed. # # ☡ This variable creates a subshell every time it is computed. -override modtime = $(shell { $(DATE) -u '+%Y-%m'; LC_TIME=POSIX TZ=UTC0 ls -q -A -l $(call quote,$1) | $(SED) 's/\([^ ]* \{1,\}\)\{5\}\(\([^ ]* \{1,\}\)\{3\}\).*/\2/;s/ \([0123456789]\) / 0\1 /;s/Jan/01/;s/Feb/02/;s/Mar/03/;s/Apr/04/;s/May/05/;s/Jun/06/;s/Jul/07/;s/Aug/08/;s/Sep/09/;s/Oct/10/;s/Nov/11/;s/Dec/12/;s/\([0123456789]\{2\}\) *\([0123456789]\{2\}\) *\([0123456789]\{4\}\)/\3-\1-\2T00:00:00Z/;s/\([0123456789]\{2\}\) *\([0123456789]\{2\}\) *\([0123456789:]\{5\}\)/XXXX-\1-\2T\3:00Z/'; } | $(TR) -d ' ' | $(AWK) -F '-' 'NR==1{year=$$1;month=$$2}$$1=="XXXX"&&$$2<=month{print year"-"$$2"-"$$3}$$1=="XXXX"&&$$2>month{print year-1"-"$$2"-"$$3}NR>1&&$$1!="XXXX"') +override modtime = $(shell if $(TEST) ! -f $(call quote,$(BUILDDIR)/.mtime); then $(PRINTF) '%b' '\n' > $(call quote,$(BUILDDIR)/.mtime); fi; $(TOUCH) -r $(call quote,$1) $(call quote,$(BUILDDIR)/.mtime); TZ=UTC0 $(DIFF) -u $(call quote,$(BUILDDIR)/.mtime) /dev/null | $(SED) '1!d;s/.* \([^ ]*\) \([^ ]*\).*$$/\1T\2Z/') # ─ ¶ Recipe Variable Definitions ───────────────────────────────────── diff --git a/README.markdown b/README.markdown index 3375b8a..8a6efb8 100644 --- a/README.markdown +++ b/README.markdown @@ -184,12 +184,12 @@ In every case, you may supply your own implementation by overriding the - `cksum` - `cp` - `date` +- `diff` - `file` - `find` - `git` (optional; set `GIT=` to disable) - `grep` - `ln` -- `ls` - `mkdir` - `mv` - `od` @@ -571,10 +571,6 @@ The following params are made available globally in parsers and - **`SRCTIME`:** The time at which the source file was last modified. - Due to limitations in Posix, this time will only have minute - precision if the file was modified in the last six months, and will - only have day precision if the file is older. - Users should not expect this value to be particularly stable. - **`THISREV`:** The value of the `THISREV` variable (if present).