forked from aks/bash-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-test-dates.sh
executable file
·62 lines (54 loc) · 1.26 KB
/
gen-test-dates.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
#!/usr/bin/env bash
# gen-test-dates.sh
#
# generate random test dates and corresponding jdates and adate
# to be used with date tests.
#
test_date_count=500
#
random_year() {
local year=0
while (( year == 0 )) ; do
year=$(( 1 + (2050 * RANDOM / 32768) ))
done
echo $year
}
random_month() {
local month=0
while (( month == 0 )); do
month=$(( 1 + (12 * RANDOM / 32768) ))
done
echo $month
}
# Ja Fe Mar Apr May Jun Jul Aug Sep Oct Nov Dec
month_days=( 0 31 28 31 30 31 30 31 31 30 31 30 31 )
random_day() {
local mx=${1:-$month}
local max=$(( month_days[$mx] ))
local day=0
while (( day == 0 )); do
day=$(( 1 + (max * RANDOM / 32768) ))
done
echo "$day"
}
TESTDATA="test-dates.dat"
echo "Generating $test_data_count test dates to $TESTDATA .."
rm -f $TESTDATA
touch $TESTDATA
declare -A days
for (( i=0; i<test_date_count; i++ )); do
if (( i % 10 == 0 )); then printf "%d.." $i ; fi
year=`random_year`
month=`random_month`
day=`random_day $month`
date=`printf "%04d-%02d-%02d" $year $month $day`
adays=`calfunc a $date`
jdays=`calfunc j $date`
if [[ -z "${days[$adays]}" ]]; then
printf "%s\t%s\t%s\n" "$date" "$adays" "$jdays" >>$TESTDATA
let days[adays]=1
fi
done
echo ''
echo "Done"
exit