-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
executable file
·196 lines (159 loc) · 5.96 KB
/
main.sh
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
bs_equiv=(2000 9 17)
ad_equiv=(1944 1 1)
function Usage {
echo -e "Usage: \tnepdate.sh [OPTIONS]"
echo -e "\t--ad2bs\tConvert from A.D. to B.S."
echo -e "\t--bs2ad\tConvert from B.S. to A.D."
}
function leap_year {
# flag is 1 for leap year & 0 for normal year
if [ $((month%400)) == 0 ]; then
flag_leap_year=1
elif [ $((month%100)) !=0 && $((month%4)) == 0 ]; then
flg_leap_year=1
else
flag_leap_year=0
fi
}
function count_ad_days {
# Calculates the number of days between the two given A.D. dates.
# begin_ad_date : An array in the format (year, month, day) that specify
delta_days=$(($(($(date -d "${bs_equiv[0]}-${bs_equiv[1]}-${bs_equiv[2]}" "+%s") - $(date -d "${ad_date[0]}-${ad_date[1]}-${ad_date[2]}" "+%s"))) / 86400))
# echo $(($(($(date -d "${bs_equiv[0]}-${bs_equiv[1]}-${bs_equiv[2]}" "+%s") - $(date -d "${ad_date[0]}-${ad_date[1]}-${ad_date[2]}" "+%s"))) / 86400))
echo $delta_days
}
function add_days_to_ad {
# STILL WORKING ON
bs_days=$(date -d "2010-01-29 +$delta_days days" +%Y-%m-%d)
# date -d "2010-01-29 +9 days" +%Y-%m-%d
echo $bs_days
}
function add_days_to_bs {
# Adds the given number of days to the given B.S. date and returns it as a tuple in the format (year,month,day)
# bs_date : a tuple in the format (year,month,day)
# delta_days : Number of days to add to the given date
# Algorithm:
# 1) Add the total number of days to the original days
# 2) Until the number of days becomes applicable to the current month, subtract the days by the number of days in the current month and increase the month
# 3) If month reaches 12, increase the year by 1 and set the month to 1
# Note:
# Tuple in the dictionary starts from 0
year=${bs_equiv[0]}
month=${bs_equiv[1]}
day=${bs_equiv[2]}
#1) Add the total number of days to the original days
$day=$day+$delta_days
#2) Until the number of days becomes applicable to the current month, subtract the days by the number of days in the current month and increase the month
while $day>$(jshon -e $year -e (($month-1)) < bs_date.json); do
delta
day=day-$(jshon -e $year -e (($month-1)) < bs_date.json)
month++
#3) If month reaches 12, increase the year by 1 and set the month to 1
if month > 12; then
month=1
$year++
fi
done
echo $year -$month - $day
}
function count_bs_days {
# Returns the number of days between the two given B.S. dates.
# begin_ad_date : A tuple in the format (year,month,day) that specify the date to start counting from.
# end_ad_date : A tuple in the format (year,month,day) that specify the date to end counting.
# Algorithm:
# Its not the piece of algorithm, but it works for this program..
# 1) First add total days in all the years
# 2) Subtract the days from first (n-1) months of the beginning year
# 3) Add the number of days from the last month of the beginning year
# 4) Subtract the days from the last months from the end year
# 5) Add the beginning days excluding the day itself
# 6) Add the last remaining days excluding the day itself
# NOTE:
# Tuple in the dictionary starts from 0
# The range(a,b) function starts from a and ends at b-1
delta_days=0
#1) First add total days in all the years
for ((year=bs_equiv[0]; year<=bs_date[0]; year++)) {
for ((month=0; month<12 ; month++)) {
month_day=$(jshon -e $year -e $month < bs_date.json)
((delta_days=$delta_days+$month_day))
}
}
#2) Subtract the days from first (n-1) months of the beginning year
for ((month=0; month<bs_equiv[1]; month++)) {
month_day=$(jshon -e $bs_equiv[0] -e $month <bs_date.json)
((delta_days=$delta_days-$month_day))
}
#3) Add the number of days from the last month of the beginning year
((delta_days=$delta_days+$(jshon -e $bs_equiv[0] -e 11 <bs_date.json)))
#4) Subtract the days from the last months from the end year
for ((month=bs_date[1]; month<11; month++)) {
month_day=$(jshon -e $bs_equiv[0] -e $month <bs_date.json)
((delta_days=$delta_days-$month_day))
}
#5) Add the beginning days excluding the day itself
delta_days=$delta_days-$bs_equiv[2] - 1
}
function bs2ad {
# Returns the A.D. equivalent date as a tuple in the format yyyy-mm-dd if the date is within range, else returns None
if [[ ${bs_date[0]} < 2000 || ${bs_date[0]} > 2089 || ${bs_date[1]} < 1 || ${bs_date[1]} > 12 || ${bs_date[2]} < 1 || ${bs_date[0]} > 32 ]] ; then
echo "Invalid input"
exit
else
count_bs_days
add_days_to_ad
fi
}
function ad2bs {
# Returns the A.D. equivalent date as a tuple in the format yyyy-mm-dd if the date is within range, else returns None
if [[ ${ad_date[0]} < 1994 || ${ad_date[0]} > 2033 || ${ad_date[1]} < 1 || ${ad_date[1]} > 12 || ${ad_date[2]} < 1 || ${ad_date[0]} > 31 ]] ; then
echo "Invalid input"
exit
else
count_bs_days
add_days_to_ad
fi
}
#checking arguments
# if [ $# -eq 0 ]; then
# Usage;
# exit 1;
# fi
# TEMP=$(getopt --long ad2bs:,bs2ad:\
# -n "nepdate" -- "$@")
# if [ $? != "0" ]; then
# exit 1
# fi
# eval set -- "$TEMP"
case $1 in
# ad2bs part
ad2bs)
y=$(echo $2 | sed 's/\(.*\)-\(.*\)-\(.*\)/\1/p' -n)
m=$(echo $2 | sed 's/\(.*\)-\(.*\)-\(.*\)/\2/p' -n)
d=$(echo $2 | sed 's/\(.*\)-\(.*\)-\(.*\)/\3/p' -n)
ad_date=($y $m $d)
echo ad_date ${ad_date[0]}/${ad_date[1]}/${ad_date[2]}
ad2bs
;;
# bs2ad part
bs2ad)
y=$(echo $2 | sed 's/\(.*\)-\(.*\)-\(.*\)/\1/p' -n)
m=$(echo $2 | sed 's/\(.*\)-\(.*\)-\(.*\)/\2/p' -n)
d=$(echo $2 | sed 's/\(.*\)-\(.*\)-\(.*\)/\3/p' -n)
bs_date=($y $m $d)
echo bs_date ${bs_date[0]}/${bs_date[1]}/${bs_date[2]}
bs2ad
;;
*)
Usage
;;
esac
echo 0 $0
echo 1 $1
echo 2 $2
# a = 9
# date -d "2010-01-29 +$a days" +%Y-%m-%d
#
# subtract days
# echo $(($(($(date -d "2010-06-01" "+%s") - $(date -d "2010-05-15" "+%s"))) / 86400))