|
4 | 4 | ; Description ...: Converts almost any date format to a YYYYMMDDHH24MISS value.
|
5 | 5 | ; Modified ......: 2017.11.01
|
6 | 6 | ; Author ........: * Original - dougal/polyethene (original)
|
7 |
| -; ............... * 20171101 - hoppfrosch |
| 7 | +; ............... * 20171101 - hoppfrosch |
8 | 8 | ; .............................. * update to V2
|
9 | 9 | ; .............................. * added more dates to be parsed
|
10 | 10 | ; Licence .......: https://creativecommons.org/publicdomain/zero/1.0/
|
@@ -72,84 +72,120 @@ DateParse(str, americanOrder := 0) {
|
72 | 72 | , dayAndMonthName := "(?:(?<Month>" . monthNames . ")[^a-zA-Z0-9:.]*(?<Day>\d{1,2})[^a-zA-Z0-9]+|(?<Day>\d{1,2})[^a-zA-Z0-9:.]*(?<Month>" . monthNames . "))"
|
73 | 73 | , monthNameAndYear := "(?<Month>" . monthNames . ")[^a-zA-Z0-9:.]*(?<Year>(?:\d{4}|\d{2}))"
|
74 | 74 |
|
75 |
| - if RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i) { ;ISO 8601 timestamps |
| 75 | + ampm := "am" |
| 76 | + if RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", &i) { ;ISO 8601 timestamps |
76 | 77 | year := i.1, month := i.3, day := i.4, hour := i.5, minute := i.7, second := i.8
|
77 | 78 | }
|
78 |
| - else if !RegExMatch(str, "^\W*(?<Hour>\d{1,2}+)(?<Minute>\d{2})\W*$", t){ ; NOT timestring only eg 1535 |
| 79 | + else if !RegExMatch(str, "^\W*(?<Hour>\d{1,2}+)(?<Minute>\d{2})\W*$", &t){ ; NOT timestring only eg 1535 |
79 | 80 | ; Try to extract the time parts
|
80 | 81 | FoundPos := RegExMatch(str, "i)(\d{1,2})" ;hours
|
81 | 82 | . "\s*:\s*(\d{1,2})" ;minutes
|
82 | 83 | . "(?:\s*:\s*(\d{1,2}))?" ;seconds
|
83 |
| - . "(?:\s*([ap]m))?", timepart) ;am/pm |
| 84 | + . "(?:\s*([ap]m))?", &timepart) ;am/pm |
84 | 85 | if (FoundPos) {
|
85 | 86 | ; Time is already parsed correctly from striing
|
86 | 87 | hour := timepart.1
|
87 | 88 | minute := timepart.2
|
88 | 89 | second := timepart.3
|
89 | 90 | ampm:= timepart.4
|
90 | 91 | ; Remove time to parse the date part only
|
91 |
| - str := StrReplace(str, timepart.value) |
| 92 | + str := StrReplace(str, timepart.0) |
92 | 93 | }
|
93 | 94 | ; Now handle the remaining string without time and try to extract date ...
|
94 |
| - if RegExMatch(str, "Ji)" . dayAndMonthName . "[^a-zA-Z0-9]*(?<Year>(?:\d{4}|\d{2}))?", d) { ; named month eg 22May14; May 14, 2014; 22May, 2014 |
| 95 | + if RegExMatch(str, "Ji)" . dayAndMonthName . "[^a-zA-Z0-9]*(?<Year>(?:\d{4}|\d{2}))?", &d) { ; named month eg 22May14; May 14, 2014; 22May, 2014 |
95 | 96 | year := d.Year, month := d.Month, day := d.Day
|
96 | 97 | }
|
97 |
| - else if RegExMatch(str, "i)" . monthNameAndYear, d) { ; named month and year without day eg May14; May 2014 |
| 98 | + else if RegExMatch(str, "i)" . monthNameAndYear, &d) { ; named month and year without day eg May14; May 2014 |
98 | 99 | year := d.Year, month := d.Month
|
99 | 100 | }
|
100 |
| - else if RegExMatch(str, "i)" . "^\W*(?<Year>\d{4})(?<Month>\d{2})\W*$", d) { ; month and year as digit only eg 201710 |
| 101 | + else if RegExMatch(str, "i)" . "^\W*(?<Year>\d{4})(?<Month>\d{2})\W*$", &d) { ; month and year as digit only eg 201710 |
101 | 102 | year := d.Year, month := d.Month
|
102 | 103 | }
|
103 | 104 | else {
|
104 |
| - if RegExMatch(str, "i)(\d{4})[^a-zA-Z0-9:.]+" . dayAndMonth, d) { ;2004/22/03 |
| 105 | + ; Default values - if some parts are not given |
| 106 | + if (not IsSet(day) and not IsSet(month) and not IsSet(year)) { |
| 107 | + ; No datepart is given - use today |
| 108 | + year := A_YYYY |
| 109 | + month := A_MM |
| 110 | + day := A_DD |
| 111 | + } |
| 112 | + if RegExMatch(str, "i)(\d{4})[^a-zA-Z0-9:.]+" . dayAndMonth, &d) { ;2004/22/03 |
105 | 113 | year := d.1, month := d.3, day := d.2
|
106 | 114 | }
|
107 |
| - else if RegExMatch(str, "i)" . dayAndMonth . "(?:[^a-zA-Z0-9:.]+((?:\d{4}|\d{2})))?", d) { ;22/03/2004 or 22/03/04 |
| 115 | + else if RegExMatch(str, "i)" . dayAndMonth . "(?:[^a-zA-Z0-9:.]+((?:\d{4}|\d{2})))?", &d) { ;22/03/2004 or 22/03/04 |
108 | 116 | year := d.3, month := d.2, day := d.1
|
109 | 117 | }
|
110 | 118 | if (RegExMatch(day, monthNames) or americanOrder and !RegExMatch(month, monthNames) or (month > 12 and day <= 12)) { ;try to infer day/month order
|
111 | 119 | tmp := month, month := day, day := tmp
|
112 | 120 | }
|
113 | 121 | }
|
114 | 122 | }
|
115 |
| - else if RegExMatch(str, "^\W*(?<Hour>\d{1,2}+)(?<Minute>\d{2})\W*$", timepart){ ; timestring only eg 1535 |
| 123 | + else if RegExMatch(str, "^\W*(?<Hour>\d{1,2}+)(?<Minute>\d{2})\W*$", &timepart){ ; timestring only eg 1535 |
116 | 124 | hour := timepart.hour
|
117 | 125 | minute := timepart.minute
|
| 126 | + ; Default values - if some parts are not given |
| 127 | + if (not IsSet(day) and not IsSet(month) and not IsSet(year)) { |
| 128 | + ; No datepart is given - use today |
| 129 | + year := A_YYYY |
| 130 | + month := A_MM |
| 131 | + day := A_DD |
| 132 | + } |
118 | 133 | }
|
119 |
| - if (day or month or year) and not (day and month and year) { ; partial date |
120 |
| - if not month or not (day or month) or (hour and not day) { ; partial date must have month and day with time or day or year without time |
| 134 | + |
| 135 | + if (IsSet(day) or IsSet(month) or IsSet(year)) and not (IsSet(day) and IsSet(month) and IsSet(year)) { ; partial date |
| 136 | + if (IsSet(year) and not IsSet(month)) or not (IsSet(day) or IsSet(month)) or (IsSet(hour) and not IsSet(day)) { ; partial date must have month and day with time or day or year without time |
121 | 137 | return
|
122 | 138 | }
|
123 |
| - else if not day { ; without time use 1st for day if not present |
124 |
| - day := 1 |
125 |
| - } |
| 139 | + } |
| 140 | + |
| 141 | + ; Default values - if some parts are not given |
| 142 | + if (IsSet(year) and IsSet(month) and not IsSet(day)) { |
| 143 | + ; year and month given without day - use first day |
| 144 | + day := 1 |
126 | 145 | }
|
127 | 146 |
|
128 | 147 | ; Format the single parts
|
129 |
| - oYear := (StrLen(year) == 2 ? "20" . year : (year ? year : A_YYYY)) |
| 148 | + oYear := (StrLen(year) == 2 ? "20" . year : (year)) |
130 | 149 | oYear := Format("{:02.0f}", oYear)
|
131 |
| - |
132 |
| - oMonth := ((month := month + 0 ? month : InStr(monthNames, SubStr(month, 1, 3)) // 4 ) > 0 ? month + 0.0 : A_MM) |
133 |
| - oMonth := Format("{:02.0f}", oMonth) |
134 |
| - |
135 |
| - oDay := ((day += 0.0) ? day : A_DD) |
| 150 | + |
| 151 | + if (isInteger(month)) { |
| 152 | + currMonthInt := month |
| 153 | + } else { |
| 154 | + currMonthInt := InStr(monthNames, SubStr(month, 1, 3)) // 4 |
| 155 | + } |
| 156 | + ; Original: oMonth := ((month := month + 0 ? month : InStr(monthNames, SubStr(month, 1, 3)) // 4 ) > 0 ? month + 0.0 : A_MM) |
| 157 | + ; oMonth := ((month := month + 0 ? month : currMonthInt ) > 0 ? month + 0.0 : A_MM) |
| 158 | + ; oMonth := Format("{:02.0f}", oMonth) |
| 159 | + oMonth := Format("{:02.0f}", currMonthInt) |
| 160 | + |
| 161 | + oDay := day |
136 | 162 | oDay := Format("{:02.0f}", oDay)
|
137 |
| - |
138 |
| - if (hour <> "") { |
139 |
| - oHour := hour + (hour == 12 ? ampm = "am" ? -12.0 : 0.0 : ampm = "pm" ? 12.0 : 0.0) |
140 |
| - oHour := Format("{:02.0f}", oHour) |
141 |
| - |
142 |
| - if (minute <> "") { |
143 |
| - oMinute := minute + 0.0 |
144 |
| - oMinute := Format("{:02.0f}", oMinute) |
145 | 163 |
|
146 |
| - if (second <> "") { |
147 |
| - oSecond := second + 0.0 |
148 |
| - oSecond := Format("{:02.0f}", oSecond) |
| 164 | + if (IsSet(hour)) { |
| 165 | + if (hour != "") { |
| 166 | + oHour := hour + (hour == 12 ? ampm = "am" ? -12.0 : 0.0 : ampm = "pm" ? 12.0 : 0.0) |
| 167 | + oHour := Format("{:02.0f}", oHour) |
| 168 | + |
| 169 | + if (IsSet(minute)) { |
| 170 | + oMinute := minute + 0.0 |
| 171 | + oMinute := Format("{:02.0f}", oMinute) |
| 172 | + |
| 173 | + if (IsSet(second)) { |
| 174 | + if (second != "") { |
| 175 | + oSecond := second + 0.0 |
| 176 | + oSecond := Format("{:02.0f}", oSecond) |
| 177 | + } |
| 178 | + } |
149 | 179 | }
|
150 | 180 | }
|
151 | 181 | }
|
152 |
| - |
153 |
| - d := oYear . oMonth . oDay . oHour . oMinute . oSecond |
154 |
| - return d |
| 182 | + |
| 183 | + retVal := oYear . oMonth . oDay |
| 184 | + if (IsSet(oHour)){ |
| 185 | + retVal := retVal . oHour . oMinute |
| 186 | + if (IsSet(oSecond)) { |
| 187 | + retVal := retVal . oSecond |
| 188 | + } |
| 189 | + } |
| 190 | + return retVal |
155 | 191 | }
|
0 commit comments