def parseDateTime(variable, pattern, text, line, mandatory=True):
    ok = re.match(pattern, text)
    if ok:
        value = ok.group('value').strip()
        try:
            variable.append(timePosition(value))
        except ValueError as e:
            if mandatory:
                errorMessage(
                    line, value,
                    "A date time in format 'YYYY-MM-DD' or 'YYYY-MM-DDThh:mmZ' is expected"
                )
            else:
                digit = re.compile(r'\d+')
                if re.match(digit, value):
                    errorMessage(
                        line, value,
                        "A date time in format 'YYYY-MM-DD' or 'YYYY-MM-DDThh:mmZ' is expected"
                    )
            variable.append(gml.TimePositionType())

        except:
            pass
        return True
    else:
        return False
def setDateTimeAttribute(target, field, pattern, text, line, mandatory=True):
    ok = re.match(pattern, text)
    if ok:
        value = ok.group('value').strip()
        try:
            setattr(target, field, timePosition(value))
        except ValueError as e:
            if mandatory:
                errorMessage(
                    line, value,
                    "A date time in format 'YYYY-MM-DD' or 'YYYY-MM-DDThh:mmZ' is expected"
                )
            else:
                digit = re.compile(r'\d+')
                if re.match(digit, value):
                    errorMessage(
                        line, value,
                        "A date time in format 'YYYY-MM-DD' or 'YYYY-MM-DDThh:mmZ' is expected"
                    )
            setattr(target, field, gml.TimePositionType())
        except:
            pass
        return True
    else:
        return False
def textToDateTime(text, line, mandatory=False):
    pattern = re.compile(r'[\(]?CCYY-MM-DD[\)]?', re.IGNORECASE)
    matched = re.match(pattern, text)
    if matched:
        if mandatory:
            errorMessage(
                line, text,
                "A date time in format 'YYYY-MM-DD' or 'YYYY-MM-DDThh:mmZ' is expected"
            )
        return gml.TimePositionType()
    else:
        try:
            dateTime = timePosition(text)
        except ValueError as e:
            errorMessage(
                line, text,
                "A date time in format 'YYYY-MM-DD' or 'YYYY-MM-DDThh:mmZ' is expected"
            )
            dateTime = gml.TimePositionType()
        except:
            dateTime = gml.TimePositionType()

        return dateTime
def timePosition(str):
    year = re.match(r'^(\d{4})-MM-DD', str)
    if year:
        str = year.group(1)
    else:
        yearMonth = re.match(r'^(\d{4}-\d{2})-DD', str)
        if yearMonth:
            str = yearMonth.group(1)

    dateTypes = [
        xsd.dateTime, dateTimeWithoutSeconds, xsd.date, xsd.gYearMonth,
        xsd.gYear
    ]
    for dateType in dateTypes:
        try:
            return gml.TimePositionType(dateType(str))
        except (xsd.SimpleTypeValueError, ValueError):
            pass

    raise ValueError('Invalid date {}'.format(str))