如何将 BSON::Timestamp 时间转换为 ruby 时间,反之亦然
How to convert BSON::Timestamp to ruby time and vice versa
我有一个ruby散列
input = {"dateCreated"=>#<BSON::Timestamp:0x00007fe0b482ca98 @increment=3745289216, @seconds=229>}
- 我们如何从 BSON 时间戳中提取时间 class。
尝试输入["dateCreated"].as_json
=> {"$timestamp"=>{"t"=>244, "i"=>664779776}}
不确定如何进行。
- 如果有关于如何使用 ruby 驱动程序
根据时间戳值过滤到 MongoDB 的任何指示,也将不胜感激
.find()
方法
您似乎有一个这种类型的对象:
https://www.rubydoc.info/github/mongodb/bson-ruby/BSON/Timestamp
从这个gem
和这个object
[6] pry(main)> bt = BSON::Timestamp.new(229, 3745289216)
=> #<BSON::Timestamp:0x00007fe338a79680 @increment=3745289216, @seconds=229>
[7] pry(main)> # you allready have when begin the object in second 229
[8] pry(main)> # you allready have when finish the even in secons in atribute increment 3745289216
可能这个值是变化的,因为时间从 1970 年开始翻译设置并使用方便的 gem 来操纵 rails 中使用的时间:
[10] pry(main)> DateTime.strptime(bt.seconds.to_s,'%s')
=> #<DateTime: 1970-01-01T00:03:49+00:00 ((2440588j,229s,0n),+0s,2299161j)>
[13] pry(main)> require 'active_support/core_ext/numeric/time.rb'
=> true
[14] pry(main)> DateTime.strptime(bt.seconds.to_s,'%s') + bt.increment.seconds
=> Mon, 06 Sep 2088 06:10:45 +0000
您可以使用 #to_bson
方法转换 BSON::Timestamp
to a BSON::ByteBuffer
。
然后您可以将 BSON::ByteBuffer
转换为表示自纪元以来的毫秒数的整数 (#get_int64
)。
然后使用Time::at
将该整数转换为Time
对象
date_time = DateTime.new(2021,8,30)
date_time.to_time
#=> 2021-08-30 00:00:00 +0000
date_time.to_time.to_i
#=> 1630281600
timestamp = BSON::Timestamp.from_bson(date_time.to_bson)
#=> #<BSON::Timestamp:0x00007fffe31da4a8 @seconds=379, @increment=2488994816>
timestamp.to_bson.get_int64 / 1000
#=> 1630281600
Time.at(timestamp.to_bson.get_int64 / 1000).utc
#=> 2021-08-30 00:00:00 UTC
我有一个ruby散列
input = {"dateCreated"=>#<BSON::Timestamp:0x00007fe0b482ca98 @increment=3745289216, @seconds=229>}
- 我们如何从 BSON 时间戳中提取时间 class。
尝试输入["dateCreated"].as_json
=> {"$timestamp"=>{"t"=>244, "i"=>664779776}}
不确定如何进行。
- 如果有关于如何使用 ruby 驱动程序 根据时间戳值过滤到 MongoDB 的任何指示,也将不胜感激
.find()
方法
您似乎有一个这种类型的对象:
https://www.rubydoc.info/github/mongodb/bson-ruby/BSON/Timestamp
从这个gem
和这个object
[6] pry(main)> bt = BSON::Timestamp.new(229, 3745289216)
=> #<BSON::Timestamp:0x00007fe338a79680 @increment=3745289216, @seconds=229>
[7] pry(main)> # you allready have when begin the object in second 229
[8] pry(main)> # you allready have when finish the even in secons in atribute increment 3745289216
可能这个值是变化的,因为时间从 1970 年开始翻译设置并使用方便的 gem 来操纵 rails 中使用的时间:
[10] pry(main)> DateTime.strptime(bt.seconds.to_s,'%s')
=> #<DateTime: 1970-01-01T00:03:49+00:00 ((2440588j,229s,0n),+0s,2299161j)>
[13] pry(main)> require 'active_support/core_ext/numeric/time.rb'
=> true
[14] pry(main)> DateTime.strptime(bt.seconds.to_s,'%s') + bt.increment.seconds
=> Mon, 06 Sep 2088 06:10:45 +0000
您可以使用 #to_bson
方法转换 BSON::Timestamp
to a BSON::ByteBuffer
。
然后您可以将 BSON::ByteBuffer
转换为表示自纪元以来的毫秒数的整数 (#get_int64
)。
然后使用Time::at
将该整数转换为Time
对象
date_time = DateTime.new(2021,8,30)
date_time.to_time
#=> 2021-08-30 00:00:00 +0000
date_time.to_time.to_i
#=> 1630281600
timestamp = BSON::Timestamp.from_bson(date_time.to_bson)
#=> #<BSON::Timestamp:0x00007fffe31da4a8 @seconds=379, @increment=2488994816>
timestamp.to_bson.get_int64 / 1000
#=> 1630281600
Time.at(timestamp.to_bson.get_int64 / 1000).utc
#=> 2021-08-30 00:00:00 UTC