| Conditions | 17 |
| Total Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DateCleaner.clean() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import re |
||
| 10 | @staticmethod |
||
| 11 | def clean(date): |
||
| 12 | """ |
||
| 13 | Converts a date in miscellaneous format to ISO-8601 (YYYY-MM-DD) format. |
||
| 14 | |||
| 15 | :param str date: The input date. |
||
| 16 | :rtype str: |
||
| 17 | """ |
||
| 18 | # Return empty input immediately. |
||
| 19 | if not date: |
||
| 20 | return date |
||
| 21 | |||
| 22 | parts = re.split('[\-/\. ]', date) |
||
| 23 | |||
| 24 | if len(parts) == 3 or (len(parts) == 4 and (parts[3] in ('00:00:00', '0:00:00'))): |
||
| 25 | if len(parts[0]) == 4 and len(parts[1]) <= 2 and len(parts[2]) <= 2: |
||
| 26 | # Assume date is in YYYY-MM-DD of YYYY-M-D format. |
||
| 27 | return parts[0] + '-' + ('00' + parts[1])[-2:] + '-' + ('00' + parts[2])[-2:] |
||
| 28 | |||
| 29 | if len(parts[0]) <= 2 and len(parts[1]) <= 2 and len(parts[2]) == 4: |
||
| 30 | # Assume date is in DD-MM-YYYY or D-M-YYYY format. |
||
| 31 | return parts[2] + '-' + ('00' + parts[1])[-2:] + '-' + ('00' + parts[0])[-2:] |
||
| 32 | |||
| 33 | if len(parts[0]) <= 2 and len(parts[1]) <= 2 and len(parts[2]) == 2: |
||
| 34 | # Assume date is in DD-MM-YY or D-M-YY format. |
||
| 35 | year = '19' + parts[2] if parts[2] >= '20' else '20' + parts[2]; |
||
| 36 | |||
| 37 | return year + '-' + ('00' + parts[1])[-2:] + '-' + ('00' + parts[0])[-2:] |
||
| 38 | |||
| 39 | if len(parts) == 1 and len(date) == 8: |
||
| 40 | # Assume date is in YYYYMMDD format. |
||
| 41 | return date[0:4] + '-' + date[4:2] + '-' + date[6:2] |
||
| 42 | |||
| 43 | # Format not recognized. Just return the original string. |
||
| 44 | return date |
||
| 45 | |||
| 47 |