groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Groff] [PATCH] mdate.sh: rewrite in Perl


From: Colin Watson
Subject: [Groff] [PATCH] mdate.sh: rewrite in Perl
Date: Sat, 18 Feb 2017 03:28:03 +0000
User-agent: Mutt/1.5.23 (2014-03-12)

groff already requires perl to build.  This version is much shorter
and easier to understand than the shell/awk version: we don't have
to worry about convincing ls to produce output that we can parse,
and we don't have to play games with the way that the same field may
contain either the year or the time depending on how old the file
is.

While I'm at it, this version also adds `SOURCE_DATE_EPOCH' support
for reproducible builds: when `SOURCE_DATE_EPOCH' is set, files are
considered to have been last modified at that time.

* mdate.sh: Rewrite in Perl, moving to ...
* mdate.pl: ... this new file.
* Makefile.am (EXTRA_DIST, .man): Update references.
---
 ChangeLog   | 19 +++++++++++++++++++
 Makefile.am |  4 ++--
 mdate.pl    | 32 ++++++++++++++++++++++++++++++++
 mdate.sh    | 59 -----------------------------------------------------------
 4 files changed, 53 insertions(+), 61 deletions(-)
 create mode 100755 mdate.pl
 delete mode 100755 mdate.sh

diff --git a/ChangeLog b/ChangeLog
index e0263e6c..c73e23c6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2017-02-18  Colin Watson  <address@hidden>
+
+       mdate.sh: rewrite in Perl
+
+       groff already requires perl to build.  This version is much shorter
+       and easier to understand than the shell/awk version: we don't have
+       to worry about convincing ls to produce output that we can parse,
+       and we don't have to play games with the way that the same field may
+       contain either the year or the time depending on how old the file
+       is.
+
+       While I'm at it, this version also adds `SOURCE_DATE_EPOCH' support
+       for reproducible builds: when `SOURCE_DATE_EPOCH' is set, files are
+       considered to have been last modified at that time.
+
+       * mdate.sh: Rewrite in Perl, moving to ...
+       * mdate.pl: ... this new file.
+       * Makefile.am (EXTRA_DIST, .man): Update references.
+
 2017-02-16  Ingo Schwarze  <address@hidden>
 
        mdoc %T: use typographic quotes
diff --git a/Makefile.am b/Makefile.am
index 2697115c..76e53525 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -834,7 +834,7 @@ EXTRA_DIST += \
   INSTALL.gen \
   LICENSES \
   MANIFEST \
-  mdate.sh \
+  mdate.pl \
   MORE.STUFF \
   PROBLEMS \
   PROJECTS \
@@ -879,7 +879,7 @@ SUFFIXES += .man
             -e "s|address@hidden@]|$(man1ext)|g" \
             -e "s|address@hidden@]|$(man5ext)|g" \
             -e "s|address@hidden@]|$(man7ext)|g" \
-            -e "s|address@hidden@]|`$(SHELL) $(top_srcdir)/mdate.sh $<`|g" \
+            -e "s|address@hidden@]|`$(PERL) $(top_srcdir)/mdate.pl $<`|g" \
             -e "s|address@hidden@]|$(oldfontdir)|g" \
             -e "s|address@hidden@]|$(pdfdocdir)|g" \
             -e "s|address@hidden@]|$(systemtmacdir)|g" \
diff --git a/mdate.pl b/mdate.pl
new file mode 100755
index 00000000..c698f983
--- /dev/null
+++ b/mdate.pl
@@ -0,0 +1,32 @@
+#! /usr/bin/env perl
+#
+# Copyright (C) 1991-2017 Free Software Foundation, Inc.
+# 
+# This file is part of groff.
+# 
+# groff is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# groff is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Print the modification date of $1 `nicely'.
+
+use warnings;
+use strict;
+use POSIX qw(LC_ALL setlocale strftime);
+
+# Don't want localized dates.
+setlocale(LC_ALL, "C");
+
+my @mtime = gmtime($ENV{SOURCE_DATE_EPOCH} || (stat $ARGV[0])[9]);
+my $mdate = strftime("%e %B %Y", @mtime);
+$mdate =~ s/^ //;
+print "$mdate\n";
diff --git a/mdate.sh b/mdate.sh
deleted file mode 100755
index 2c24a009..00000000
--- a/mdate.sh
+++ /dev/null
@@ -1,59 +0,0 @@
-#! /bin/sh
-#
-# Copyright (C) 1991-2014 Free Software Foundation, Inc.
-# 
-# This file is part of groff.
-# 
-# groff is free software; you can redistribute it and/or modify it under
-# the terms of the GNU General Public License as published by the Free
-# Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-# 
-# groff is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-# for more details.
-# 
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-#
-# Print the modification date of $1 `nicely'.
-
-# Don't want foreign dates.
-
-LANGUAGE=
-LC_ALL=C; export LC_ALL
-
-
-(date;
-if ls -L /dev/null 1>/dev/null 2>&1; then ls -L -l $1; else ls -l $1; fi
-) | awk '
-BEGIN {
-       full["Jan"] = "January"; number["Jan"] = 1;
-       full["Feb"] = "February"; number["Feb"] = 2;
-       full["Mar"] = "March"; number["Mar"] = 3;
-       full["Apr"] = "April"; number["Apr"] = 4;
-       full["May"] = "May"; number["May"] = 5;
-       full["Jun"] = "June"; number["Jun"] = 6;
-       full["Jul"] = "July"; number["Jul"] = 7;
-       full["Aug"] = "August"; number["Aug"] = 8;
-       full["Sep"] = "September"; number["Sep"] = 9;
-       full["Oct"] = "October"; number["Oct"] = 10;
-       full["Nov"] = "November"; number["Nov"] = 11;
-       full["Dec"] = "December"; number["Dec"] = 12;
-}
-
-NR == 1 {
-       month = $2;
-       year = $NF;
-}
-
-NR == 2 {
-       if ($(NF-1) ~ /:/) {
-               if (number[$(NF-3)] > number[month])
-                       year--;
-       }
-       else
-               year = $(NF-1);
-       print $(NF-2), full[$(NF-3)], year
-}'
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]