utc (1190B)
1 #!/bin/sh 2 # convert input date time string to YYYYMMDDHHMMSS 3 # USAGE: utc +format [datetime] 4 # utc -r +format [datetime] 5 # format: 6 # %d: day from 01 to 31 7 # %H: hour from 00 to 23 8 # %m: month number from 01 to 12 9 # %M: minite from 00 to 59 10 # %S: second from 00 to 60 (leap second?) 11 # %Y: year in 4-letter style 12 # default value is "+%d/%m/%Y:%H:%M:%S" 13 # 14 15 conv(){ 16 mat='s/%Y/([0-9]{4})/;s/%./([0-9]{2})/g' 17 18 pat=$(echo "$format" | 19 sed "$mat" | 20 sed 's/^\+//' | 21 sed 's/^/^.*[^0-9]?/' | 22 sed 's/$/.*$/' | 23 sed 's/!/\\!/g' 24 ) 25 sub=$(echo "$format" | 26 sed -E 's/[^%]*%(.)/\1 /g' | 27 awk '{ 28 for(i=1;i<=NF;i++){ 29 printf "%s=\\%d;", $i, i 30 } 31 printf "printf \"%%04d%%02d%%02d%%02d%%02d%%02d\\\\\\\\n\" \ 32 \"$Y\" \"$m\" \"$d\" \"$H\" \"$M\" \"$S\"\n" 33 }') 34 sed -E 's!'"$pat"'!'"$sub"'!' | sh -s 35 } 36 37 conv_r(){ 38 sub=$(echo "$format" | 39 sed 's/!/\\!/g' | 40 sed 's/^+//;s/%Y/\\1/;s/%m/\\2/;s/%d/\\3/;s/%H/\\4/;s/%M/\\5/;s/%S/\\6/') 41 sed -E 's!(....)(..)(..)(..)(..)(..)!'"$sub"'!' 42 } 43 44 format="+%d/%m/%Y:%H:%M:%S" 45 46 if [ "$1" = "-r" ]; then 47 [ -n "$2" ] && format=$2 48 conv_r 49 else 50 [ -n "$1" ] && format=$1 51 conv 52 fi