diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2019-03-16 01:43:40 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2019-03-16 01:43:40 (EDT) |
commit | ca09e590ef48e2f30e7f5387e88b7904fa455be9 (patch) | |
tree | d88dec15cb924d7ed610328d93e7409c39fd641c | |
parent | 2c9fde033eb2adfe306a2dbefda143015f4ca448 (diff) |
tools/mtime.sh: New file
-rwxr-xr-x | tools/mtime.sh | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tools/mtime.sh b/tools/mtime.sh new file mode 100755 index 0000000..c2f9dfa --- /dev/null +++ b/tools/mtime.sh @@ -0,0 +1,71 @@ +#!/bin/sh +# +# Utility to portably get a file's modification time +# +# Copyright (C) 2018, 2019 Patrick McDermott +# +# This program 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. +# +# This program 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/>. + +set -u + +get_mtime() +{ + local file="${1}" + shift 1 + local m= + local d= + local y= + local now_m= + local now_y= + + read m d y <<-EOF + $(LC_ALL=POSIX ls -l "${file}" | cut -d ' ' -f 6-8) + EOF + case "${m}" in + 'Jan') m=1;; 'Feb') m=2;; 'Mar') m=3;; 'Apr') m=4;; + 'May') m=5;; 'Jun') m=6;; 'Jul') m=7;; 'Aug') m=8;; + 'Sep') m=9;; 'Oct') m=10;; 'Nov') m=11;; 'Dec') m=12;; + esac + case "${y}" in *':'*) + read now_m now_y <<-EOF + $(date '+%m %Y') + EOF + now_m="${now_m#0}" + if [ ${now_m} -ge ${m} ]; then + y=${now_y} + else + y=$((${now_y} - 1)) + fi + esac + + printf '%d-%02d-%02d\n' ${y} ${m} ${d} + return 0 +} + +usage() +{ + printf 'Usage: %s <file>\n' "${0}" +} + +main() +{ + if [ ${#} -lt 1 ]; then + usage >&2 + exit 1 + fi + + get_mtime "${1}" +} + +main "${@}" |