ruby-on-rails - Ruby:从 Ruby 生成 Google Protobuf 时间戳?
问题描述
这是一个模型blog
。
# id :bigint(8)
# created_at :datetime not null
# updated_at :datetime not null
class Blog < ApplicationRecord
end
我想将模型的 created_at 和 updated_at 转换为 google Protobuf TimeStamp
blog = Blog.first
blog.created_at
形成protobuf消息时如何将DateTime转换为google Protobuf TimeStamp?
解决方案
Google protobuf 库有方法可以做到这一点:
require 'google/protobuf/well_known_types'
# Convert ruby Time to protobuf value
time = Time.current
Google::Protobuf::Timestamp.new.from_time(time)
# Convert protobuf value to ruby Time
data = {:nanos=>801877000, :seconds=>1618811494}
Google::Protobuf::Timestamp.new(data).to_time
推荐阅读
- sql - 将 varchar 转换为日期(德语和英语日期转换)
- c# - .NET Core Web API 身份验证和 .NET 身份
- django - 在 Windows Server 上发布 Django 项目
- java - 如何考虑超类中静态方法的行为?
- javascript - 如何从交叉构建项目中的资源文件夹中读取
- xamarin.forms - Xamarin.forms 如何添加本地提醒
- python - 为什么我的嵌套字典中出现关键错误
- java - 注册用户时遇到firebase问题
- c# - 类的实例在其他类中为空
- c# - MiniProfiler - 在控制台应用程序中复制 Web 视图信息