Completed
Push — master ( 0c702e...f32b73 )
by P.R.
01:15
created

DateCleaner   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 17

1 Method

Rating   Name   Duplication   Size   Complexity  
F clean() 0 35 17
1
import re
2
3
4
class DateCleaner:
5
    """
6
    Utility class for converting dates in miscellaneous formats to ISO-8601 (YYYY-MM-DD) format.
7
    """
8
9
    # ------------------------------------------------------------------------------------------------------------------
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
46
# ----------------------------------------------------------------------------------------------------------------------
47