-
Notifications
You must be signed in to change notification settings - Fork 0
/
age
executable file
·62 lines (52 loc) · 1.73 KB
/
age
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash -e
# age date-of-birth [format]
# date-of-birth formats:
# CCYYMMDD
# CCYY-MM-DD
# "CCYYmmDD HH:MM:SS"
# "CCYYmmDD HH:MM:SSTZ" TZ +0100 or -0530 form
#
# 2016-03-09 based on elapsed seconds
# Yaswant Pradhan
# -----------------------------------------------------------------------------
# Usage -----------------------------------------------------------------------
usage(){
cat<<EOF
SYNOPSIS
${0##*/} DOB [format]
- DOB format can take one of the following form
CCYYMMDD (default)
CCYY-MM-DD
"CCYYmmDD HH:MM:SS"
"CCYYmmDD HH:MM:SSTZ" TZ +0100 or -0530 form
- format specification is required on (MacOS) POSIX systems
DESCRIPTION
Get Age from date of birth
EOF
}
# Settings --------------------------------------------------------------------
export TZ=Europe/London
birthday=${1:-20150417}
format="$2"
if [[ "$OSTYPE" == darwin* ]]; then
[ -z "$format" ] && { echo "Syntax error: missing Format spec."; usage; exit 1; }
s1=$(date -jf "$format" "$birthday" +'%s')
else
s1=$(date -d "$birthday" +'%s')
fi
s2=$(date +'%s')
sd=$((s2 - s1))
nyears=$(echo $sd|awk '{printf "%d",$1/3.154e+7}')
nmonths=$(echo $sd|awk '{printf "%d",$1/2.628e+6}')
rdays=$(echo $sd|awk '{printf "%d\n",($1/86400)%30.42}')
# Print total age and breakdown
echo "$nyears years $((nmonths - nyears*12)) months $rdays days.";
echo -e "\n---- Breakdown ----"
printf "%0.2g years\n" "$(bc -l <<< $sd/31540000)"
printf "%0.4g months\n" "$(bc -l <<< $sd/2628000)"
printf "%0.4g weeks\n" "$(bc -l <<< $sd/604800)"
printf "%0.5g days\n" "$(bc -l <<< $sd/86400)"
printf "%0.0f hours\n" "$(bc -l <<< $sd/3600)"
printf "%0.0f minutes\n" "$(bc -l <<< $sd/60)"
printf "%0.0f seconds\n" "$sd"
echo -e "--------------------"