#!/bin/bash ## When datestamping the Version of a computer-program, I want days to change at 8am rather than midnight; Eugene Reimer 2008-02-11 ## (changing days at 4am would be more appropriate for most people, but not for someone of my habits...) ## This ought to be dead-trivial and would be but for that silliness about a leading-zero implying Octal, and then printf not supporting 10#NN, grumble grumble... ## ## USAGE: same as for the date command (only -d -r +FMT options). ##--original arg-handling: DOP="now"; if [[ $1 == -d ]];then DOP=$2; shift;shift; fi ##support -d option, as in date command ROP=; if [[ $1 == -r ]];then ROP=$2; shift;shift; fi ##support -r option, as in date command FMT="+%Y-%m-%d %H:%M:%S"; if [[ $1 == +* ]];then FMT=$1; shift; fi ##support FMT-string as in date command if [ $ROP ];then DATEANDTIME=$(date -r "$ROP" "+%Y-%m-%d %H:%M:%S"); else DATEANDTIME=$(date -d "$DOP" "+%Y-%m-%d %H:%M:%S"); fi ##--2008-07-19: ==UNFINISHED== improved arg-handling: ##while [ $# -gt 0 ]; do ##parse the cmdline-args ## [[ $1 == --[hv]* ]] && HELP=1 ##set HELP=1 for --help or --version ## case "$1" in ## +*) FMT=$1; shift ;; ##save FMT-arg separately ## -d) ARG="$ARG -d '$2'"; shift;shift ;; ##save -d arg in ARG-string (strophs not behaving as expected here either??)) ## *) ARG="$ARG $1"; shift ;; ##save other args in ARG-string ####*) ARG="$ARG '$1'"; shift ;; ##save other args in ARG-string (strophs not behaving as expected) ## esac ##done ##echo "DEBUG ARG:$ARG FMT:$FMT HELP:$HELP"; read; ##DEBUG ##if [ $HELP ];then date $ARG; exit 0; fi ##for --help or --version: handle then exit ##DATEANDTIME=$(date $ARG "+%Y-%m-%d %H:%M:%S") ##--rest is independent of where the date came from (this may become datefixupERW): YEAR=$(date -d "$DATEANDTIME" +%Y) MNTH=$(date -d "$DATEANDTIME" +%m) DAYM=$(date -d "$DATEANDTIME" +%d) HOUR=$(date -d "$DATEANDTIME" +%H) MINU=$(date -d "$DATEANDTIME" +%M) SECO=$(date -d "$DATEANDTIME" +%S) daym=${DAYM#0}; hour=${HOUR#0}; ##remove leading-zero, to avoid octal... if ((hour<8));then ((daym=daym-1)); HOUR=23; MINU=59; SECO=59; fi; ##do arithmetic with numeric vars (clock stands still midnight-to-8am) DAYM=$(printf "%02d" $daym) ##convert back to string, adding leading-zero if needed date -d "$YEAR$MNTH$DAYM $HOUR:$MINU:$SECO" "$FMT" ##use date to reformat result, to support FMT param, and fixup day-zero... ##--tested with dateERW -d "20080101 07:22" to verify that handles day-of-month going to zero. exit CONSIDER: making FMT-default same as date-cmd; the only previous use is in INSTALL-new-version-of-nopercart--??-- 2008-07-19: copyCF needs support for -r/--reference, so I TRIED to rewrite the arg-handling to support any date-arg; GRUMBLE: the -d operand apparently must have a space between date & time when both being specified ==my reading suggests that "T" ought to work too, but it results in time-less-13-hours -- may be fixable--??-- ==2010-03: (see notes in EXAMPLES-DATE); date-cmd supports single-letter timezone-names, where "F" is -0600 (winter-time), "E" is -0500 (summer-time); == but not "J" which ought to be roughly what "T" is in ISO-8601-dates; Conclusion: it can produce ISO-8601-dates, but not accept them:-( ARG-HANDLING: ==using demonstrates problem with quotes/strophs inside strings not behaving as I thought they did--!!-- ==for more info: http://www.mpi-inf.mpg.de/~uwe/lehre/unixffb/quoting-guide.html ==!!==failed to find satisfactory solution; would involve and way too much work+mess, unless I can solve the "T" riddle==!!== ==2010-03: ANOTHER SOLUTION: make ARG an array; later use set-cmd followed by: date "$@"==??== SOLUTION: went back to old method, adding ROP for -r support ==surely there is a better way?? 2008-07-20: copyCF needs to use the EXIF-date (to handle having rotated on the laptop) -- so made separate script dateEXIF-ERW to do that; ==datefixupERW to do the shared portion -- receives YYYY-MM-YY HH:MM:SS +FMT