mdate-sh 6.0 KB

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