예제 #1
0
def date_util_test():
    """ Simple test of correctly transforming a timestamp to a python datetime,
        and back to a timestamp
    """
    
    # test a not-very-random sequence of times
    for ts in range(0, 1250000000, 321321):
        # simply see if we can round-trip the timestamp and get the same result
        dt = dt_from_timestamp(ts)
        ts2 = int(dt_to_timestamp(dt))
        assert ts2 == ts
예제 #2
0
def cast_for_json(val):
    if val is None:
        return None
    if type_util.is_primitive(val):
        return val
    if isinstance(val, collections.Mapping):
        return {
            cast_for_json(key): cast_for_json(subval)
            for key, subval in val.items()
        }
    elif isinstance(val, collections.Iterable):
        return [cast_for_json(subval) for subval in val]
    elif isinstance(val, datetime.datetime):
        return date_util.dt_to_timestamp(val)
    elif isinstance(val, decimal.Decimal):
        return float(val)
    elif isinstance(val, uuid.UUID):
        return str(val)
    else:
        return str(val)
예제 #3
0
def row_to_dict(row, datetime_to_int=False, keys_to_ignore=None):
    """
    Transform a psycopg2 row into a dict or a list of dict

    :param row:                 The psycopg2 row
    :type row:                  Row
    :param datetime_to_int:     Do you want to transform datetime object into int ? Optional, default False
    :type datetime_to_int:      bool
    :param keys_to_ignore:      Keys to not load. Optional, default None
    :type keys_to_ignore:       None|list[str]
    :return:                    A readable representation of the results
    :rtype:                     dict[str, any]
    """
    if row is None:
        return None
    result = {}
    for k in row.keys():
        if datetime_to_int and isinstance(row[k], datetime.datetime):
            result[k] = date_util.dt_to_timestamp(row[k])
        else:
            result[k] = row[k]
    return result
예제 #4
0
 def _set_time_display(self, val):
     trans_time = datetime.strptime(self._get_date_display()+val, "%m/%d/%Y%H:%M:%S")
     trans_time = trans_time.replace(tzinfo=Eastern)
     self.trans_date = dt_to_timestamp(trans_time)
     return