BASH Time and Date

From Indie IT Wiki

Time Between File Timestamps

NEW

function displaytime () {
  local T=$1
  local D=$((T/60/60/24))
  local H=$((T/60/60%24))
  local M=$((T/60%60))
  local S=$((T%60))
  (( $D > 0 )) && printf '%d days ' $D
  (( $H > 0 )) && printf '%d hours ' $H
  (( $M > 0 )) && printf '%d minutes ' $M
  (( $D > 0 || $H > 0 || $M > 0 )) && printf 'and '
  printf '%d seconds\n' $S
}

# displaytime 666

SECONDS=$((stat --printf '%Y' "${FINISHED}"; printf ' - '; stat --printf '%Y\n' "${STARTED}") | bc -lq) && 
TIMETAKEN=`displaytime $SECONDS`
printf "${TITLE}\n\n${INPUTFILESIZE}\n\n${OUTPUTFILESIZE}\n\n${TIMETAKEN}" |mail -s "ffmpeg" paully.littlefield@gmail.com && 

OLD

SECONDS=$((stat --printf '%Y' /home/paully/hb.finished; printf ' - '; stat --printf '%Y\n' /home/paully/hb.started) | bc -lq) && 
TIMETAKEN=$(date -ud "@$SECONDS" +'$((%s/3600/24)) days %H hours %M minutes %S seconds') && 
printf "${INPUTFILESIZE}\n\n${OUTPUTFILESIZE}\n\n${SECONDS}\n\n${TIMETAKEN}"

Ping With Time And Date Stamp

ping -i1 -c10 192.168.1.1 | while read pong; do echo "$(date '+%Y-%m-%d-%H:%M:%S')    $pong"; done

Time

Checking NTP

timedatectl status
ntpstat
ntpq -pn

Thanks to Cyberciti

Convert Standard Date And Time To Epoch Time

date +%s

Convert Epoch Time To Standard Date And Time

date --date='@1335309835'

Date

Today

date

Yesterday

date -d "yesterday"

Number Of Days Between Two Dates

echo $"(( $(date --date="031122" +%s) - $(date --date="021020" +%s) ))/(60*60*24)"|bc

Number Of Days After

date -d "230 days"
or
date -d "24 weeks 2 days 4 hour 6 minutes"

Number Of Days Before

date -d "230 days ago"
or
date -d "24 weeks 2 days 4 hour 6 minutes ago"

Date Years From Date

TZ='Europe/London' date -d "1987-01-01+99 years"