from datetime import datetime


def dt(dtstr):  # (parse datetime str)
    """
    Return datetime parsed from str of various accepted formats, eg:
      - '2014-02-14 16:30:45 001234'
      - '2014-02-14 16:30:45'
      - '2014-02-14 16:30'
      - '2014-02-14'
    Raise ValueError if not recognizable.
    """
    FORMATS = (
        '%Y-%m-%d %H:%M:%S %f',
        '%Y-%m-%d %H:%M:%S',
        '%Y-%m-%d %H:%M',
        '%Y-%m-%d',
    )
    for fmt in FORMATS:
        try:
            dt1 = datetime.strptime(dtstr, fmt)
            return dt1
        except ValueError as e:
            pass
    raise ValueError("time data '{dtstr}' does not match any format"
        .format(dtstr=dtstr))