Example #1
0
def convert_to_date(x):
    """Convert value to date.

    Exceptions:
      - ValueError: if string date is invalid.
    """
    result = x
    if isinstance(x,float):
        result = datetime.utcfromtimestamp(x)
    elif isinstance(x,str):
        sec = parseDate(x)
        result = datetime.utcfromtimestamp(sec)
    return result
Example #2
0
def convert_to_date(x):
    """Convert value to date.

    Exceptions:
      - ValueError: if string date is invalid.
    """
    result = x
    if isinstance(x, float):
        result = datetime.utcfromtimestamp(x)
    elif isinstance(x, str):
        sec = parseDate(x)
        result = datetime.utcfromtimestamp(sec)
    return result
Example #3
0
def parseDatetime(d, utc=False):
    """Parse a datetime object, or anything that formats itself
    with isoformat(), to number of seconds since epoch.
    """
    from Pegasus.netlogger.parsers.base import parseDate
    if d is None:
        raise ValueError("date is None")
    iso = d.isoformat()
    # add 'midnight' time if none given
    if 'T' not in iso:
        iso += 'T00:00:00'
    # only append timezone if none is there already        
    if not iso.endswith('Z') and not re.match('.*[+-]\d\d:\d\d$', iso):
        if utc:
            iso += 'Z'
        else:
            iso += tzstr()
    return parseDate(iso)
Example #4
0
def parseDatetime(d, utc=False):
    """Parse a datetime object, or anything that formats itself
    with isoformat(), to number of seconds since epoch.
    """
    from Pegasus.netlogger.parsers.base import parseDate
    if d is None:
        raise ValueError("date is None")
    iso = d.isoformat()
    # add 'midnight' time if none given
    if 'T' not in iso:
        iso += 'T00:00:00'
    # only append timezone if none is there already        
    if not iso.endswith('Z') and not re.match('.*[+-]\d\d:\d\d$', iso):
        if utc:
            iso += 'Z'
        else:
            iso += tzstr()
    return parseDate(iso)