forked from kodamail/gscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmonth.gsf
234 lines (209 loc) · 5.71 KB
/
cmonth.gsf
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
function cmonth( month, size )
cmonth.1 = 'January'
cmonth.2 = 'February'
cmonth.3 = 'March'
cmonth.4 = 'April'
cmonth.5 = 'May'
cmonth.6 = 'June'
cmonth.7 = 'July'
cmonth.8 = 'August'
cmonth.9 = 'September'
cmonth.10 = 'October'
cmonth.11 = 'November'
cmonth.12 = 'December'
c = substr( month, 1, 1 )
****************************************
* integer(s) -> string
****************************************
if( valnum(c) = 1 )
* month = '123', '234', ..., '212' are accepted (must be a string!).
flag = 0
if( math_strlen(month) = 3 )
if( month = '123' ) ; c.1 = 1 ; c.2 = 2 ; c.3 = 3 ; flag = 1 ; endif
if( month = '234' ) ; c.1 = 2 ; c.2 = 3 ; c.3 = 4 ; flag = 1 ; endif
if( month = '345' ) ; c.1 = 3 ; c.2 = 4 ; c.3 = 5 ; flag = 1 ; endif
if( month = '456' ) ; c.1 = 4 ; c.2 = 5 ; c.3 = 6 ; flag = 1 ; endif
if( month = '567' ) ; c.1 = 5 ; c.2 = 6 ; c.3 = 7 ; flag = 1 ; endif
if( month = '678' ) ; c.1 = 6 ; c.2 = 7 ; c.3 = 8 ; flag = 1 ; endif
if( month = '789' ) ; c.1 = 7 ; c.2 = 8 ; c.3 = 9 ; flag = 1 ; endif
if( month = '890' ) ; c.1 = 8 ; c.2 = 9 ; c.3 = 10 ; flag = 1 ; endif
if( month = '901' ) ; c.1 = 9 ; c.2 = 10 ; c.3 = 11 ; flag = 1 ; endif
if( month = '012' ) ; c.1 = 10 ; c.2 = 11 ; c.3 = 12 ; flag = 1 ; endif
if( month = '121' ) ; c.1 = 11 ; c.2 = 12 ; c.3 = 1 ; flag = 1 ; endif
if( month = '212' ) ; c.1 = 12 ; c.2 = 1 ; c.3 = 2 ; flag = 1 ; endif
endif
if( flag = 1 )
i = 1
ret = '' ; h = ''
while( i <= 3 )
c = c.i
if( size != 'size' )
ret = ret % h % substr( cmonth.c, 1, size )
else
ret = ret % h % cmonth.c
endif
if( size != 1 ) ; h = '-' ; endif
i = i + 1
endwhile
return ret
endif
* month = 1, 2, 3, ..., or 12
* month < 1 or month > 12 is also accepted
if( valnum(month) = 1 )
month = month + 0
month = math_fmod( month-1, 12 ) + 1
ret = cmonth.month
if( size != 'size' & size != -1 )
ret = substr( ret, 1, size )
endif
* month = '12,1,2' = DJF, '6,7' = 'JJ' etc
* size: length per one month
else
m = 1
month.m = ''
length = math_strlen( month )
i = 1
while( i <= length )
c = substr( month, i, 1 )
if( c = ',' )
m = m + 1
month.m = ''
else
month.m = month.m % c
endif
i = i + 1
endwhile
mmax = m
m = 1
h = ''
ret = ''
while( m <= mmax )
* say m % ': ' % month.m
mm = month.m
if( size != 'size' )
ret = ret % h % substr( cmonth.mm, 1, size )
else
ret = ret % h % cmonth.mm
endif
if( size != 1 ) ; h = '-' ; endif
m = m + 1
endwhile
endif
****************************************
* string -> integer(s)
****************************************
else
month = chcase( month, 'upper_first' )
ret = -1
len_month = math_strlen( month )
***** simgle month *****
m = 1
while( m <= 12 )
cmonth_tmp = substr( cmonth.m, 1, len_month )
if( cmonth_tmp = month )
if( ret != -1 ) ; ret = ret % '|' % m ;
else ; ret = m ; endif
endif
m = m + 1
endwhile
if( ret != -1 ) ; return ret ; endif
***** multi-month (e.g. DJF, Dec-Jan-Feb) *****
* add '-' if necessary
length = math_strlen( month )
i = 1
tmp = ''
while( i <= length )
c = substr( month, i, 1 )
if( c = '-' ) ; break ; endif
if( i = length ) ; month = tmp % c ; endif
tmp = tmp % c % '-'
i = i + 1
endwhile
* convert
m = 1
* month.m = ''
* length = math_strlen( month )
* i = 1
* while( i <= length )
* c = substr( month, i, 1 )
* if( c = '-' )
* m = m + 1
* month.m = ''
* else
* month.m = month.m % c
* endif
* i = i + 1
* endwhile
* mmax = m
while( 1 = 1 )
month.m = rgnwrd( month, m, m, '-' )
if( month.m = '' ) ; break ; endif
m = m + 1
endwhile
mmax = m - 1
m = 1
while( m <= mmax )
month.m = chcase( month.m, 'upper_first' )
month.m = cmonth( month.m )
* m2: for multiple-candidate
* length = math_strlen( month.m )
* i = 1
* m2 = 1
* month.m.m2 = ''
* while( i <= length )
* c = substr( month.m, i, 1 )
* if( c = '|' )
* m2 = m2 + 1
* month.m.m2 = ''
* else
* month.m.m2 = month.m.m2 % c
* endif
* i = i + 1
* endwhile
i = 1
* month.m.i = ''
while( 1 = 1 )
month.m.i = rgnwrd( month.m, i, i, '|' )
if( month.m.i = '' ) ; break ; endif
i = i + 1
endwhile
* m2 = m2 + 1
* month.m.m2 = ''
m = m + 1
endwhile
* check for continuity
sm = 1
while( month.1.sm != '' & ret = -1 )
now = month.1.sm
cont = 1
if( size = 1 )
ret = math_fmod( month.1.sm, 10 )
else
ret = month.1.sm
endif
m = 2
while( m <= mmax )
* now = math_fmod( now + 1 + 1, 12 ) - 1
now = math_fmod( now, 12 ) + 1
m2 = 1
while( month.m.m2 != '' )
if( month.m.m2 = now )
if( size = 1 )
ret = ret % math_fmod( month.m.m2, 10 )
else
ret = ret % ',' % month.m.m2
endif
break
endif
m2 = m2 + 1
if( month.m.m2 = '' ) ; cont = 0 ; endif
endwhile
m = m + 1
endwhile
*say cont
if( cont != 1 ) ; ret = -1 ; endif
sm = sm + 1
endwhile
if( ret != -1 ) ; return ret ; endif
endif
return ret