#!/bin/bash #.Y / 70 83 / / / / 105 / / / / / / ;A4_QUER.Y #.YT 3 2 0 0 3 3 1 / / / #.H!a!0:linux//time/THIS!0.!c # # copyright : (C) 2012 by Thomas Bruecker =: tbrue # email : public0x05bf@bluewin.ch # # tbrue 14jun2012 # # time THIS : time-functions # ++++++++++++++++++++++++++ # # MyHash("linux//time/THIS") = 0x0XYZH # # * for every function: # * if returned result is not commented, the function returns 0 on no-error. SCRIPTS=root/bin . /$SCRIPTS/string_/THIS time_Test() { # time_2canonicalDate xxx # time_canonicalDaytime0_2Time "$1" # time_canonicalTime0_2Time "$1" time_2cronTime "$1" # time_2canonicalTime0 "$1" } time_2st() { time_2sortableTime1 "$1" } time_2canonicalDate() # $1 : date; format as needed as input for the "date"-command. # convert the input-date to a canonical date (e.g. "14jun2012") and write # it to "stdout". { local Date Result Date="$(date -d "$1" +%d%b%Y)" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else string_2LowCase "$Date" return 0 } time_2canonicalDaytime0() # $1 : date; format as needed as input for the "date"-command. # convert the input-date to canonical daytime0 (e.g. "10h36m02") and write it # to "stdout". { date -d "$1" +%Hh%Mm%S return # return result of previous # command. } time_2canonicalTime0() # $1 : date; format as needed as input for the "date"-command. # convert the input-date to canonical time0 (e.g. "14jun2012-10h36m02") and # write it to "stdout". { local Date Result Date="$(time_2canonicalDate "$1")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else Date="${Date}-$(time_2canonicalDaytime0 "$1")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else echo "$Date" return # return result of previous # command. } time_2cronTime() # $1 : date; format as needed as input for the "date"-command. # convert the input-date to the time in crontab-format: # " " and write it to "stdout". { date -d "$1" +"%M %H %d %m *" return # return result of previous # command. } time_2sortableDate0() # $1 : date; format as needed as input for the "date"-command. # convert the input-date to sortable date0 (e.g. "20120714") and write it # to "stdout". { date -d "$1" +%Y%m%d return # return result of previous # command. } time_2sortableDate1() # $1 : date; format as needed as input for the "date"-command. # convert the input-date to sortable date1 (e.g. "20120714_14jun2012") and # write it to "stdout". { local Date Result Date="$(time_2sortableDate0 "$1")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else Date="${Date}_$(time_2canonicalDate "$1")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else echo "$Date" return # return result of previous # command. } time_2sortableDaytime0() # $1 : date; format as needed as input for the "date"-command. # convert the input-date to sortable daytime0 (e.g. "103602") and write it # to "stdout". { date -d "$1" +%H%M%S return # return result of previous # command. } time_2sortableTime0() # $1 : date; format as needed as input for the "date"-command. # convert the input-date to sortable time0 (e.g. "20120714103602") and # write it to "stdout". { local Date Result Date="$(time_2sortableDate0 "$1")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else Date="${Date}$(time_2sortableDaytime0 "$1")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else echo "$Date" return # return result of previous # command. } time_2sortableTime1() # $1 : date; format as needed as input for the "date"-command. # convert the input-date to sortable time1 # (e.g. "20120714103602_14jun2012-10h36m02") and write it to "stdout". { local Date Result Date="$(time_2sortableTime0 "$1")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else Date="${Date}_$(time_2canonicalDate "$1")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else Date="${Date}-$(time_2canonicalDaytime0 "$1")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else echo "$Date" return # return result of previous # command. } time_canonicalDaytime0_2Time() # $1 : canonical daytime0 (e.g. "10h36m02"). # convert the input-time to the format as needed as input for the "date"- # command and write it to "stdout". { if ! echo "$1" | grep -q "..h..m"; then echo "time_canonicalDaytime0_2Time: invalid time '$1'" > /dev/stderr return 1 fi # else # check for correctness: date -d "$( echo "$1" | sed 's/h/:/' | sed 's/m/:/' )" \ +%H:%M:%S return # return result of previous # command. } time_canonicalTime0_2Time() # $1 : canonical time0 (e.g. "14jun2012-10h36m02"). # convert the input-time to the format as needed as input for the "date"- # command and write it to "stdout". { local Date Daytime Result Time if ! echo "$1" | grep -q "-"; then echo "time_canonicalTime0_2Time: invalid time '$1'" > /dev/stderr return 1 fi # else # split date and daytime: # delete spaces: Time="$( echo "$1" | sed 's/ //g' | sed 's/-/ /' )" Date="$( echo $Time | sed 's/ .*$//' )" # delete end of string. Daytime="$( echo $Time | sed 's/^.* //' )" # delete start of string. Time="${Date} $(time_canonicalDaytime0_2Time "$Daytime")" Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else date -d "$Time" >> /dev/null # just test date. Result=$? if [ 0 != $Result ]; then # error in date command. return $Result fi # else echo "$Time" return # return result of previous # command. }