-
Notifications
You must be signed in to change notification settings - Fork 0
/
doy2ymd
executable file
·54 lines (43 loc) · 1007 Bytes
/
doy2ymd
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
#!/bin/bash
# Name
# doy2ymd
#
# Purpose
# Convert Year and serial day of year to calendar year-month-day
#
# Syntax
# doy2ymd yyyyddd
# doy2ymd yyyy ddd
#
# Example
# doy2ymd 2012171
# doy2ymd 2012 171
# Both the commands above should return same result: 20120619
#
# yaswant.pradhan
usage(){
cat<<EOF
Usage: doy2ymd YYYYddd [or]
doy2ymd yyyy ddd [or]
doy2ymd yyyy ddd fmt-specifier
This script works on GNU-based system.
Serial day 0 will return last day of the previous year.
EOF
}
[[ $# -eq 0 || $# -gt 3 ]] && usage && exit
# Parse One paramater argument:
if [[ $# -eq 1 ]]; then
[[ "${#1}" -ne 7 ]] && usage && exit 1
yyyy=${1%???}
ddd=${1#$yyyy}
fi
# Parse Two paramater arguments:
if [[ $# -eq 2 || $# -eq 3 ]]; then
[[ "${#1}" -ne 4 || "${#2}" -ne 3 ]] && usage && exit 1
yyyy=${1}
ddd=${2}
fi
fmt=${3-%Y%m%d}
# Return calendar date:
# date -d "$yyyy-01-01 +$ddd days -1 day" +"%Y%m%d"
date -d "$yyyy-01-01 +$ddd days -1 day" +$fmt