-
Notifications
You must be signed in to change notification settings - Fork 0
/
doy
executable file
·114 lines (100 loc) · 2.97 KB
/
doy
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
#-----------------------------------------------------
# NAME
# doy - print Serial Day of Year
#
# SYNOPSIS
# doy [date] | [--help] |
#
# VERSION
# 0.2
# 1.0 Parse more common date formats (yp)
#
# AUTHOR
# Yaswant Pradhan
#-----------------------------------------------------
version=1.0
usage()
{
cat <<EOF
NAME
${0##*/} - show serial day of year
SYNOPSIS
${0##*/} date
Valid date formats:
CCYY-MM-DD
CCYY/MM/DD
CCYYMMDD
CCYY MM DD
DD-MM-CCYY (- can be any character in this form)
DDMMCCYY
DD MM YYYY
DESCRIPTION
Display the Serial Day of Year for a given day month year in DD MM YYYY
FORMAT, or for the system date.
-h -? --help print this message and exit
-v --version prints version information and exit
Examples:
doy Return Serial Day of Year for the current date
doy 29 06 2003 Returns DOY for 29th June 2003, which is 180
EOF
}
#------------------------------------------------------------------------------
# Parse arguments
#------------------------------------------------------------------------------
while [[ $1 == -* ]]; do
case $1 in
-v|--version)
echo -e "\n${0##*/}: v-$version\n"
shift 1 ;;
-h|--help|-?|*)
usage
exit ;;
esac
done
#------------------------------------------------------------------------------
# Parse input date and return doy
#------------------------------------------------------------------------------
case $# in
0) # Current system doy
date +'%j'; exit $?;;
1) #echo "1 arg"
case ${#1} in
8) #echo "len=8"
ret=$(date -d $1 +'%j' 2>/dev/null)
case $? in
0) echo $ret; exit 0;;
?) # Get Day Month Year in reverse order
y=$(echo $1 |cut -c5-8)
m=$(echo $1 |cut -c3-4)
d=$(echo $1 |cut -c1-2)
date -d $y$m$d +'%j'
exit $? ;;
esac
;;
10) #echo "len=10"
ret=$(date -d $1 +'%j' 2>/dev/null)
case $? in
0) echo $ret; exit 0;;
?)
y=$(echo $1 |cut -c7-10)
m=$(echo $1 |cut -c4-5)
d=$(echo $1 |cut -c1-2)
ret=$(date -d $y$m$d +'%j' 2>/dev/null)
[[ $? -eq 0 ]] && echo $ret || \
(echo "invalid date format" && exit 1)
exit $?;;
esac
;;
*) echo "invalid date"; usage; exit 1;;
esac
;;
3) #echo "3 args"
case ${#1} in
2) date -d $3$2$1 +'%j'; exit $?;;
4) date -d $1$2$3 +'%j'; exit $?;;
?) echo "invalid date"; usage; exit 1;;
esac
;;
*) usage; exit ;;
esac