-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsedate.rex
117 lines (106 loc) · 3.02 KB
/
parsedate.rex
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
/* RexxRE date parsing example
*
* This was written to verify an example from the manual.
* The function parsedate takes a few common free-form
* date formats and returns the likely date in date('s')
* format.
*
* Copyright 2003, Patrick TJ McPhee
* Distributed under the Mozilla Public Licence.
* $Header: C:/ptjm/rexx/rexxre/RCS/parsedate.rex 1.1 2003/04/30 20:32:14 ptjm Rel $
*/
rcc = rxfuncadd('reloadfuncs', 'rexxre', 'reloadfuncs')
if rcc then return 1
call reloadfuncs
say parsedate('April 30 2003')
say parsedate('30 April 2003')
/* this is a cheat -- the actual output of date is `Wed Apr 30 ...' */
say parsedate('Apr 30 15:59:27 EDT 2003')
say parsedate('30/4/2003')
say parsedate('2003/4/30')
say parsedate('2003/04/30')
say parsedate('30/04/2003')
exit 0
parsedate: procedure
mwre = ReComp('[[:alpha:]]+', 'x')
mdre = ReComp('[[:digit:]]{1,2}', 'x')
yre = ReComp('[[:digit:]]{4}', 'x')
parse arg date
pdate = date
rdate = 'baddate'
/* test for April 30 2003 */
if ReParse(mwre, pdate, 'v', 'month', 'pdate') then do
if ReParse(mdre, pdate, 'v', 'day', 'pdate') then do
if ReParse(yre, pdate, 'v', 'year', 'pdate') then
rdate = year'/'mwords(month)'/'day
end
/* start over, trying 30 April 2003 */
if rdate = 'baddate' then do
pdate = date
if ReParse(mdre, pdate, 'v', 'day', 'pdate') then do
if ReParse(mwre, pdate, 'v', 'month', 'pdate') then do
if ReParse(yre, pdate, 'v', 'year', 'pdate') then
rdate = year'/'mwords(month)'/'right(day,2,'0')
end
end
end
end
else do
/* test for 2003/04/30 */
pdate = date
if ReParse(yre, pdate, 'v', 'year', 'pdate') then do
if ReParse(mdre, pdate, 'v', 'month', 'pdate') then do
if ReParse(mdre, pdate, 'v', 'day', 'pdate') then
rdate = year'/'right(month,2,'0')'/'right(day,2,'0')
end
end
/* test for 30/04/2003, then give up */
if rdate = 'baddate' then do
pdate = date
if ReParse(mdre, pdate, 'v', 'day', 'pdate') then do
if ReParse(mdre, pdate, 'v', 'month', 'pdate') then do
if ReParse(yre, pdate, 'v', 'year', 'pdate') then
rdate = year'/'right(month,2,'0')'/'right(day,2,'0')
end
end
end
end
call ReFree mwre, yre, mdre
return rdate
mwords: procedure
mw.january = '01'
mw.february = '02'
mw.march = '03'
mw.april = '04'
mw.may = '05'
mw.june = '06'
mw.july = '07'
mw.august = '08'
mw.september = '09'
mw.october = '10'
mw.november = '11'
mw.december = '12'
mw.jan = '01'
mw.feb = '02'
mw.mar = '03'
mw.apr = '04'
mw.may = '05'
mw.jun = '06'
mw.jul = '07'
mw.aug = '08'
mw.sep = '09'
mw.oct = '10'
mw.nov = '11'
mw.dec = '12'
mw.janvier = '01'
mw.fevrier = '02'
mw.fev = '02'
mw.mars = '03'
mw.avril = '04'
mw.avr = '04'
mw.mai = '05'
mw.juillet = '07'
mw.aout = '08'
mw.aou = '08'
arg mon
return mw.mon