doc: update Vala documentation
[automake.git] / lib / mdate-sh
blob88f306d709ff6ed29253e496c6510a684eab9b2b
1 #!/bin/sh
2 # Get modification time of a file or directory and pretty-print it.
4 scriptversion=2023-11-23.18; # UTC
6 # Copyright (C) 1995-2024 Free Software Foundation, Inc.
7 # written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, June 1995
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
12 # any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22 # As a special exception to the GNU General Public License, if you
23 # distribute this file as part of a program that contains a
24 # configuration script generated by Autoconf, you may include it under
25 # the same distribution terms that you use for the rest of that program.
27 # This file is maintained in Automake, please report
28 # bugs to <bug-automake@gnu.org> or send patches to
29 # <automake-patches@gnu.org>.
31 if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
32 emulate sh
33 NULLCMD=:
34 # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which
35 # is contrary to our usage. Disable this feature.
36 alias -g '${1+"$@"}'='"$@"'
37 setopt NO_GLOB_SUBST
40 case $1 in
41 '')
42 echo "$0: No file. Try '$0 --help' for more information." 1>&2
43 exit 1;
45 -h | --h*)
46 cat <<\EOF
47 Usage: mdate-sh [--help] [--version] FILE
49 Pretty-print the modification day of FILE, in the format:
50 1 January 1970
52 Report bugs to <bug-automake@gnu.org>.
53 GNU Automake home page: <https://www.gnu.org/software/automake/>.
54 General help using GNU software: <https://www.gnu.org/gethelp/>.
55 EOF
56 exit $?
58 -v | --v*)
59 echo "mdate-sh $scriptversion"
60 exit $?
62 esac
64 error ()
66 echo "$0: $1" >&2
67 exit 1
71 # Prevent date giving response in another language.
72 LANG=C
73 export LANG
74 LC_ALL=C
75 export LC_ALL
76 LC_TIME=C
77 export LC_TIME
79 # Use UTC to get reproducible result.
80 TZ=UTC0
81 export TZ
83 # GNU ls changes its time format in response to the TIME_STYLE
84 # variable. Since we cannot assume 'unset' works, revert this
85 # variable to its documented default.
86 if test "${TIME_STYLE+set}" = set; then
87 TIME_STYLE=posix-long-iso
88 export TIME_STYLE
91 save_arg1=$1
93 # Find out how to get the extended ls output of a file or directory.
94 if ls -L /dev/null 1>/dev/null 2>&1; then
95 ls_command='ls -L -l -d'
96 else
97 ls_command='ls -l -d'
99 # Avoid user/group names that might have spaces, when possible.
100 if ls -n /dev/null 1>/dev/null 2>&1; then
101 ls_command="$ls_command -n"
104 # A 'ls -l' line looks as follows on OS/2.
105 # drwxrwx--- 0 Aug 11 2001 foo
106 # This differs from Unix, which adds ownership information.
107 # drwxrwx--- 2 root root 4096 Aug 11 2001 foo
109 # To find the date, we split the line on spaces and iterate on words
110 # until we find a month. This cannot work with files whose owner is a
111 # user named "Jan", or "Feb", etc. However, it's unlikely that '/'
112 # will be owned by a user whose name is a month. So we first look at
113 # the extended ls output of the root directory to decide how many
114 # words should be skipped to get the date.
116 # On HPUX /bin/sh, "set" interprets "-rw-r--r--" as options, so the "x" below.
117 set x`$ls_command /`
119 # Find which argument is the month.
120 month=
121 command=
122 until test $month
124 test $# -gt 0 || error "failed parsing '$ls_command /' output"
125 shift
126 # Add another shift to the command.
127 command="$command shift;"
128 case $1 in
129 Jan) month=January; nummonth=1;;
130 Feb) month=February; nummonth=2;;
131 Mar) month=March; nummonth=3;;
132 Apr) month=April; nummonth=4;;
133 May) month=May; nummonth=5;;
134 Jun) month=June; nummonth=6;;
135 Jul) month=July; nummonth=7;;
136 Aug) month=August; nummonth=8;;
137 Sep) month=September; nummonth=9;;
138 Oct) month=October; nummonth=10;;
139 Nov) month=November; nummonth=11;;
140 Dec) month=December; nummonth=12;;
141 esac
142 done
144 test -n "$month" || error "failed parsing '$ls_command /' output"
146 # Get the extended ls output of the file or directory.
147 set dummy x`eval "$ls_command \"\\\$save_arg1\""`
149 # Remove all preceding arguments
150 eval $command
152 # Because of the dummy argument above, month is in $2.
154 # On a POSIX system, we should have
156 # $# = 5
157 # $1 = file size
158 # $2 = month
159 # $3 = day
160 # $4 = year or time
161 # $5 = filename
163 # On Darwin 7.7.0 and 7.6.0, we have
165 # $# = 4
166 # $1 = day
167 # $2 = month
168 # $3 = year or time
169 # $4 = filename
171 # Get the month.
172 case $2 in
173 Jan) month=January; nummonth=1;;
174 Feb) month=February; nummonth=2;;
175 Mar) month=March; nummonth=3;;
176 Apr) month=April; nummonth=4;;
177 May) month=May; nummonth=5;;
178 Jun) month=June; nummonth=6;;
179 Jul) month=July; nummonth=7;;
180 Aug) month=August; nummonth=8;;
181 Sep) month=September; nummonth=9;;
182 Oct) month=October; nummonth=10;;
183 Nov) month=November; nummonth=11;;
184 Dec) month=December; nummonth=12;;
185 esac
187 case $3 in
188 ???*) day=$1;;
189 *) day=$3; shift;;
190 esac
192 # Here we have to deal with the problem that the ls output gives either
193 # the time of day or the year.
194 case $3 in
195 *:*) set `date`; eval year=\$$#
196 case $2 in
197 Jan) nummonthtod=1;;
198 Feb) nummonthtod=2;;
199 Mar) nummonthtod=3;;
200 Apr) nummonthtod=4;;
201 May) nummonthtod=5;;
202 Jun) nummonthtod=6;;
203 Jul) nummonthtod=7;;
204 Aug) nummonthtod=8;;
205 Sep) nummonthtod=9;;
206 Oct) nummonthtod=10;;
207 Nov) nummonthtod=11;;
208 Dec) nummonthtod=12;;
209 esac
210 # For the first six month of the year the time notation can also
211 # be used for files modified in the last year.
212 if (expr $nummonth \> $nummonthtod) > /dev/null;
213 then
214 year=`expr $year - 1`
215 fi;;
216 *) year=$3;;
217 esac
219 # The result.
220 echo $day $month $year
222 # Local Variables:
223 # mode: shell-script
224 # sh-indentation: 2
225 # eval: (add-hook 'before-save-hook 'time-stamp)
226 # time-stamp-start: "scriptversion="
227 # time-stamp-format: "%:y-%02m-%02d.%02H"
228 # time-stamp-time-zone: "UTC0"
229 # time-stamp-end: "; # UTC"
230 # End: