Example #1
0
def guess(s, parse=True, is_gmt=False, set_gmt=False,
          try_iso=True, try_num=True, try_en=True):
    """Guess the format, and optionally parse, the input string.

    If 'is_gmt' is True, assume timezone is GMT when not given.
    Otherwise, assume localtime.
    If 'set_gmt' is True then set the timezone to GMT, otherwise
    set it to localtime.

    The answer is a pair containing the guessed format and, if the 'parse'
    flag was given, the parsed value as seconds since the epoch, otherwise None.

    The format is a constant defined in this module:
      UNKNOWN - Cannot guess the format (associated value is None)
      ISO8601 - This is a prefix of the ISO8601 format accepted by completeISO()
      ENGLISH - This is an natural English-language format accepted by makeISO()
      SECONDS - This is seconds since the UNIX epoch (Midnight on 1970/1/1).
    """
    if not s:
        return UNKNOWN, None
    if try_num and isinstance(s, float):
        return SECONDS, s
    sec = None
    s = s.strip()
    # try ISO8601
    if try_iso:
        m = ISO_DATE_PARTS.match(s)
        if m and m.start() == 0 and m.end() == len(s):
            if parse:
                if s[-1] == 'Z':
                    # explicit timezone overrides option
                    is_gmt = True
                iso_s = complete_iso(s, is_gmt=is_gmt, set_gmt=set_gmt)
                sec = parse_iso(iso_s)
            return ISO8601, sec
            # try number
    if try_num:
        m = NUMBER_DATE.match(s)
        if m and m.start() == 0 and m.end() == len(s):
            if parse:
                sec = float(s)
            return SECONDS, sec
            # try natural language
    if try_en:
        # noinspection PyBroadException
        try:
            d = magic.magic_date(s)
        except Exception:
            d = None
        if d is not None:
            if parse:
                partial_iso = d.isoformat()
                iso = complete_iso(partial_iso, is_gmt=False, set_gmt=set_gmt)
                sec = parse_iso(iso)
            return ENGLISH, sec
            # default: unknown
    return UNKNOWN, None
Example #2
0
def make_iso(value, is_gmt=False, set_gmt=False):
    """If value is a tuple, assume it is the one returned by time.gmtime() or time.localtime()
    Otherwise, assume value is an English language description (for partial ISO
    strings, use completeISO() instead).

    Return an ISO8601 string, with timezone set to GMT or localtime.
    """
    # assume GMT
    tz_str = 'Z'
    if isinstance(value, tuple) or isinstance(value, list):
        fmt = ("%04d", "-%02d", "-%02d", "T%02d", ":%02d", ":%02d")
        s = ''.join([f % v for f, v in zip(fmt, value)])
        if not is_gmt:
            tz_str = get_localtime_iso(value)
        iso = s + tz_str
    else:
        try:
            d = magic.magic_date(value)
        except Exception:
            raise ValueError("magic date cannot parse '%s'" % value)
        partial_iso = d.isoformat()
        iso = complete_iso(partial_iso, is_gmt=is_gmt, set_gmt=set_gmt)
    return iso