#!/bin/bash 
# Shell.....: bash 
# Objetivo..: Calcular diferenca entre datas
# Autor.....: Jorge Luis Gomes
# e-mail....: gomes.gomes@inpe.br
# Versao....: 1.0
# Data da criacao: 30/11/2023
# Autor.....: Jorge Luis Gomes
# e-mail....: jorge.gomes@inpe.br
#  
# Sintaxe
# diff_dates.sh date1 date2 [mm/dd/yyyy mm/dd/yyyy [-(s|m|h|d) | --(seconds|minutes|hours|days)]
#    date1 e date2 sintaxe: segue a sintaxe do comando date
#    "2023-11-30 12:00:00"
#    "11/30/2023 12:00:00"
#    
#  
#  Exemplos:
#  diff_dates.sh 11/30/2023 08/30/1967 -d
#  diff_dates.sh 11/30/2023 08/30/1967 -hour
#  diff_dates.sh 01/01/2100 01/01/1960 -hour
#  diff_dates.sh 01/01/2100 01/01/1960 -h
#  diff_dates.sh 01/01/2100 01/01/1960 -days
#  diff_dates.sh "2100/01/01 23:00:00" "1960/01/01 00:00:00" -h
#  diff_dates.sh "2100-01-01 23:59:59" "1960-01-01 00:00:00" -s

first_date=$(date -d "$1" "+%s")
second_date=$(date -d "$2" "+%s")

case "$3" in
            "--seconds" | "-s") period=1;;
            "--minutes" | "-m") period=60;;
              "--hours" | "-h") period=$((60*60));;
        """|    --days" | "-d") period=$((60*60*24));;
esac

datediff=$(( ($first_date - $second_date)/($period) ))
echo $datediff

