Exemple #1
0
def strftime(time_format, seconds, microseconds=0, timezone=None):
    ret_dt = datetime_from_timestamp(seconds) + datetime.timedelta(
        microseconds=microseconds)
    ret_dt = ret_dt.replace(tzinfo=UTC())
    if timezone:
        ret_dt = ret_dt.astimezone(timezone)
    try:
        return ret_dt.strftime(time_format)
    except ValueError:
        # CASSANDRA-13185: if the date cannot be formatted as a string, return a string with the milliseconds
        # since the epoch. cqlsh does the exact same thing for values below datetime.MINYEAR (1) or above
        # datetime.MAXYEAR (9999). Some versions of strftime() also have problems for dates between MIN_YEAR and 1900.
        # cqlsh COPY assumes milliseconds from the epoch if it fails to parse a datetime string, and so it is
        # able to correctly import timestamps exported as milliseconds since the epoch.
        return '%d' % (seconds * 1000.0)
    def test_datetime_from_timestamp(self):
        self.assertEqual(util.datetime_from_timestamp(0), datetime.datetime(1970, 1, 1))
        # large negative; test PYTHON-110 workaround for windows
        self.assertEqual(util.datetime_from_timestamp(-62135596800), datetime.datetime(1, 1, 1))
        self.assertEqual(util.datetime_from_timestamp(-62135596199), datetime.datetime(1, 1, 1, 0, 10, 1))

        self.assertEqual(util.datetime_from_timestamp(253402300799), datetime.datetime(9999, 12, 31, 23, 59, 59))

        self.assertEqual(util.datetime_from_timestamp(0.123456), datetime.datetime(1970, 1, 1, 0, 0, 0, 123456))

        self.assertEqual(util.datetime_from_timestamp(2177403010.123456), datetime.datetime(2038, 12, 31, 10, 10, 10, 123456))
    def test_uuid_from_time(self):
        t = time.time()
        seq = 0x2aa5
        node = uuid.getnode()
        u = util.uuid_from_time(t, node, seq)
        # using AlmostEqual because time precision is different for
        # some platforms
        self.assertAlmostEqual(util.unix_time_from_uuid1(u), t, 4)
        self.assertEqual(u.node, node)
        self.assertEqual(u.clock_seq, seq)

        # random node
        u1 = util.uuid_from_time(t, clock_seq=seq)
        u2 = util.uuid_from_time(t, clock_seq=seq)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u1), t, 4)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u2), t, 4)
        self.assertEqual(u.clock_seq, seq)
        # not impossible, but we shouldn't get the same value twice
        self.assertNotEqual(u1.node, u2.node)

        # random seq
        u1 = util.uuid_from_time(t, node=node)
        u2 = util.uuid_from_time(t, node=node)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u1), t, 4)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u2), t, 4)
        self.assertEqual(u.node, node)
        # not impossible, but we shouldn't get the same value twice
        self.assertNotEqual(u1.clock_seq, u2.clock_seq)

        # node too large
        with self.assertRaises(ValueError):
            u = util.uuid_from_time(t, node=2 ** 48)

        # clock_seq too large
        with self.assertRaises(ValueError):
            u = util.uuid_from_time(t, clock_seq=0x4000)

        # construct from datetime
        dt = util.datetime_from_timestamp(t)
        u = util.uuid_from_time(dt, node, seq)
        self.assertAlmostEqual(util.unix_time_from_uuid1(u), t, 4)
        self.assertEqual(u.node, node)
        self.assertEqual(u.clock_seq, seq)
Exemple #4
0
 def deserialize(byts, protocol_version):
     timestamp = int64_unpack(byts) / 1000.0
     return util.datetime_from_timestamp(timestamp)