编辑代码

def check_datetime(s):
    assert len(s) == 19
    assert s[4] == '-'
    assert s[7] == '-'
    assert s[10] == 'T'
    assert s[13] == ':'
    assert s[16] == ':'

    year = int(s[0:0 + 4])
    month = int(s[5:5 + 2])
    mday = int(s[8:8 + 2])
    hour = int(s[11:11 + 2])
    minute = int(s[14:14 + 2])
    second = int(s[17:17 + 2])

    assert year >= 1970 and year <= 9999
    assert month >= 1 and month <= 12
    assert mday >= 1 and mday <= 31
    if month in [4, 6, 9, 10]:
        assert mday <= 30
    if month == 2:
        if is_leap_year(year):
            assert mday <= 29
        else:
            assert mday <= 28
    assert hour >= 0 and hour <= 24
    assert minute >= 0 and minute <= 59
    assert second >= 0 and second <= 59


def is_leap_year(year):
    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)


if __name__ == '__main__':
    try:
        check_datetime('2048-01-32T12:10:07')
    except(ValueError, AssertionError):
        print('格式错误')