1
votes

The following shows a message received. How to convert the timestamp to datetime (python-kafka)?

ConsumerRecord(topic='topic', partition=0, offset=347, timestamp=1602086275744, timestamp_type=0, key=None, value={......}, headers=[], checksum=3460280057, serialized_key_size=-1, serialized_value_size=471, serialized_header_size=-1).

1

1 Answers

2
votes

Try this:

from datetime import datetime
datetime.fromtimestamp(1602086275744 / 1000)

Output:

datetime.datetime(2020, 10, 7, 17, 57, 55, 744000)

To keep the precision try this:

ms = 1602086275744
datetime.utcfromtimestamp(ms//1000).replace(microsecond=ms%1000*1000)

Output: datetime.datetime(2020, 10, 7, 15, 57, 55, 744000)

If you want to drop the microsecond just do this:

datetime.utcfromtimestamp(1602086275744 // 1000)
datetime.datetime(2020, 10, 7, 15, 57, 55)