def datetime2milli(d, type): try: if type == datetime: diff = d - datetime(1970, 1, 1) else: diff = d - date(1970, 1, 1) return long(diff.total_seconds()) * long(1000) + long(diff.microseconds / 1000) except Exception as e: problem_serializing(d, e)
def datetime2unix(d): try: if d == None: return None elif isinstance(d, datetime.datetime): epoch = datetime.datetime(1970, 1, 1) elif isinstance(d, datetime.date): epoch = datetime.date(1970, 1, 1) else: Log.error("Can not convert {{value}} of type {{type}}", value= d, type= d.__class__) diff = d - epoch return Decimal(long(diff.total_seconds() * 1000000)) / 1000000 except Exception as e: Log.error("Can not convert {{value}}", value=d, cause=e)
def ibytes2icompressed(source): yield (b'\037\213\010\000' + # Gzip file, deflate, no filename struct.pack('<L', long(time.time())) + # compression start time b'\002\377' # maximum compression, no OS specified ) crc = zlib.crc32(b"") length = 0 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0) for d in source: crc = zlib.crc32(d, crc) & 0xffffffff length += len(d) chunk = compressor.compress(d) if chunk: yield chunk yield compressor.flush() yield struct.pack("<2L", crc, length & 0xffffffff)
def ibytes2icompressed(source): yield ( b'\037\213\010\000' + # Gzip file, deflate, no filename struct.pack('<L', long(time.time())) + # compression start time b'\002\377' # maximum compression, no OS specified ) crc = zlib.crc32(b"") length = 0 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0) for d in source: crc = zlib.crc32(d, crc) & 0xffffffff length += len(d) chunk = compressor.compress(d) if chunk: yield chunk yield compressor.flush() yield struct.pack("<2L", crc, length & 0xffffffff)