Search code examples
rubytimeprotocol-buffersepoch

How can we convert google.protobuf.Timestamp to Ruby DateTime object?


There aren't much of details for this question, I am getting a kafka event where the time is of type google.protobuf.Timestamp. These kafka events are being posted(through HTTP) to my rails app where I need the time to be in datetime format of ruby instead of google.protobuf.Timestamp


Solution

  • Convert microseconds to nanos and use Time.at(seconds, microseconds_with_frac) → time

    epoch_micros = timestamp.nanos / 10 ** 6
    Time.at(timestamp.seconds, epoch_micros)
    

    for reference: http://ruby-doc.org/core-2.2.0/Time.html