如何将google.protobuf.Timestamp转换为Ruby DateTime对象? [英] How can we convert google.protobuf.Timestamp to Ruby DateTime object?

查看:107
本文介绍了如何将google.protobuf.Timestamp转换为Ruby DateTime对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题没有太多详细信息,我收到一个kafka事件,其中时间的类型为 google.protobuf.Timestamp .这些kafka事件通过HTTP发布(通过HTTP)到我的rails应用程序,在这里我需要将时间设置为红宝石的日期时间格式,而不是 google.protobuf.Timestamp

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

推荐答案

将微秒转换为纳秒,并使用 Time.at(seconds,microseconds_with_frac)→时间

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)

供参考: http://ruby-doc.org/core-2.2.0/Time.html

这篇关于如何将google.protobuf.Timestamp转换为Ruby DateTime对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆