-
Notifications
You must be signed in to change notification settings - Fork 10
/
enumdate
executable file
·200 lines (185 loc) · 6.28 KB
/
enumdate
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
197
198
199
200
#!/bin/sh
######################################################################
#
# ENUMDATE - Enumerate Dates from arg#1 to arg#2
#
# USAGE: enumdate <yyyymmdd1> <yyyymmdd2>
# ARGS : <yyyymmdd1> ... Date you want to enumerate from
# <yyyymmdd2> ... Date you want to enumerate to
# * The number of digits for "yyyy" part can be 1, 2, 3,
# of course 4. (e.g. "1", "201") But in case of 2 digits,
# for instance "87" will be regarded as not "1987" but "87".
# * It is OK altough <yyyymmdd1> is bigger, smaller than
# <yyyymmdd2>, or <yyyymmdd1> is equal with <yyyymmdd2>.
# RET : $?=0 (when all of the options are valid)
# stdout : HTTP Cookie string
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2020-05-06
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/misc-tools
#
######################################################################
######################################################################
# Initial Configuration
######################################################################
# === Initialize shell environment ===================================
set -u
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH 2>/dev/null)${PATH+:}${PATH-}"
case $PATH in :*) PATH=${PATH#?};; esac
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage ========================
print_usage_and_exit () {
cat <<-USAGE 1>&2
Usage : ${0##*/} <yyyymmdd1> <yyyymmdd2>
Argument: <yyyymmdd1> ... Date you want to enumerate from
<yyyymmdd2> ... Date you want to enumerate to
* The number of digits for "yyyy" part can be 1, 2, 3,
of course 4. (e.g. "1", "201") But in case of 2 digits,
for instance "87" will be regarded as not "1987" but "87".
* It is OK altough <yyyymmdd1> is bigger, smaller than
<yyyymmdd2>, or <yyyymmdd1> is equal with <yyyymmdd2>.
Version : 2020-05-06 22:42:19 JST
USAGE
exit 1
}
######################################################################
# Parse Arguments
######################################################################
case $# in
2) : ;;
*) print_usage_and_exit;;
esac
printf '%s' "$1" | grep -Eq '^[0-9]{5,8}$' || print_usage_and_exit
printf '%s' "$2" | grep -Eq '^[0-9]{5,8}$' || print_usage_and_exit
######################################################################
# Main Routine
######################################################################
MYNAME="${0##*/}" awk -v d1=$1 -v d2=$2 '
BEGIN {
i = length(d2);
date_y = substr(d2, 1 , i-4) * 1;
date_m = substr(d2, i-3, 2) * 1;
date_d = substr(d2, i-1 ) * 1;
if (! valdate()) {
printf("%s: Invalid date: %s\n",ENVIRON["MYNAME"],d2) | "cat 1>&2";
exit 1;
}
date_Y=date_y; date_M=date_m; date_D=date_d; YMD = d2*1;
i = length(d1);
date_y = substr(d1, 1 , i-4) * 1;
date_m = substr(d1, i-3, 2) * 1;
date_d = substr(d1, i-1 ) * 1;
if (! valdate()) {
printf("%s: Invalid date: %s\n",ENVIRON["MYNAME"],d2) | "cat 1>&2";
exit 1;
}
if (d1*1 < d2*1) {
while (date_y*10000+date_m*100+date_d < YMD) {
printf("%04d%02d%02d\n",date_y,date_m,date_d);
incdate();
}
} else if (d1*1 > d2*1) {
while (date_y*10000+date_m*100+date_d > YMD) {
printf("%04d%02d%02d\n",date_y,date_m,date_d);
decdate();
}
}
printf("%04d%02d%02d\n",date_y,date_m,date_d);
}
# INPUT : date_y, date_m, date_d
# OUTPUT: date_y, date_m, date_d
function incdate() {
if (date_d < 28 ) {
date_d++;
} else if (date_d ==31 ) {
if (date_m <12) {
date_m++;
} else {
date_y++;
date_m = 1;
date_d = 1;
}
date_d = 1;
} else if (date_d ==30 ) {
if ((date_m == 4) || (date_m == 6) || (date_m == 9) || (date_m ==11)) {
date_d = 1;
date_m++;
} else {
date_d = 31;
}
} else if (date_d ==29 ) {
if (date_m == 2) {
date_m = 3;
date_d = 1;
} else {
date_d++;
}
} else if (date_m != 2 ) {
date_d = 29;
} else if (date_y % 400 == 0) {
date_d = 29;
} else if (date_y % 100 == 0) {
date_m = 3;
date_d = 1;
} else if (date_y % 4 == 0) {
date_d = 29;
} else {
date_m = 3;
date_d = 1;
}
}
# INPUT : date_y, date_m, date_d
# OUTPUT: date_y, date_m, date_d
function decdate() {
if (date_d > 1 ) {
date_d--;
} else if ((date_m == 5) || (date_m == 7) || (date_m ==10) || (date_m ==12)) {
date_m--;
date_d = 30;
} else if (date_m == 1 ) {
date_y--;
date_m = 12;
date_d = 31;
} else if (date_m != 3 ) {
date_m--;
date_d = 31;
} else if (date_y % 400 == 0 ) {
date_m = 2;
date_d = 29;
} else if (date_y % 100 == 0 ) {
date_m = 2;
date_d = 28;
} else if (date_y % 4 == 0 ) {
date_m = 2;
date_d = 29;
} else {
date_m = 2;
date_d = 28;
}
}
# INPUT : date_y, date_m, date_d
# OUTPUT: (return code)
function valdate() {
if (date_y < 1 ) {return 0;}
if ((date_m < 1) || (date_m > 12)) {return 0;}
if ((date_d < 1) || (date_d > 31)) {return 0;}
if (date_d == 31 ) {
if ((date_m == 2) || (date_m == 4) || (date_m == 6)) {return 0;}
if ((date_m == 9) || (date_m ==11) ) {return 0;}
}
if ((date_d == 30) && (date_m == 2)) {return 0;}
if ((date_d == 29) && (date_m == 2)) {
if (date_y % 4 != 0 ) {return 0;}
if ((date_y % 100 == 0) && (date_y % 400 != 0)) {return 0;}
}
return 1;
}
'