#!/bin/bash ##-- date conversions by Eugene Reimer 2003-September ## PREREQ: tolower from http://ereimer.net/programs/general-purpose-scripts.htm ## USAGE: ## datecvt YearMonthDay FORMAT -- where FORMAT is same as used by the cmd -- see man date ## EXAMPLES: ## datecvt -r/books/BALLOT +%y%b%d -- get modification-date of a file, in YYmmmDD format (silly example since date-command would do the same) ## datecvt now %y%b%d -- get current-date in YYmmmDD format (another silly example) ## datecvt 010815 %y%b%d -- convert specified YYMMDD date to YYmmmDD format ## datecvt 01aug15 %y%m%d -- convert specified YYmmmDD date to YYMMDD format -- nonstd for ## datecvt 01aug %y%m -- convert specified YYmmm date to YYMM format -- nonstd for ## datecvt 01aug15 %Y-%m-%d -- convert to YYYY-MM-DD as used in [ $# -ne 2 ] && { echo "usage: datecvt DATE FORMAT"; exit; } A=$1; B=$2; shift; shift if [[ $A == [0-9]* ]]; then A=$(echo "$A" | tolower) Y=; while [ "$A" != "${A#[0-9]}" ]; do Y=$Y${A:0:1}; A=${A#[0-9]}; done M=; while [ "$A" != "${A#[a-z]}" ]; do M=$M${A:0:1}; A=${A#[a-z]}; done D=; while [ "$A" != "${A#[0-9]}" ]; do D=$D${A:0:1}; A=${A#[0-9]}; done if [ ${#Y} -lt 6 ] && [ ${#M} -eq 0 ]; then M=01; fi ##KLUDGE; use January if month omitted if [ ${#Y} -lt 6 ] && [ ${#D} -eq 0 ]; then D=01; fi ##KLUDGE; use 01 if day omitted; fixed first test 2008-04-23 if [ ${#M} -ge 3 ]; then M=$(date -d${M:0:3}01 +%m); fi ##convert alpha-month to numeric-month A=-d$Y$M$D elif [[ $A == now ]]; then A=-dnow fi if [[ $B != +* ]];then B=+$B; fi ##new param, with or without leading plus-sign date $A $B exit 2008-04-23: modified 2nd param, eg: former Ymd is now [+]%Y%m%d, so can now get %Y-%m-%d style when wanted; now using in scalepix script, for alpha-month support; 2009-11: mk-calendar-8up-wallet-sized uses scalepix which uses this; now published on my website;