Skip to content

Commit

Permalink
修改BSON的date时间精度
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed May 16, 2024
1 parent f0934e8 commit 9172eee
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 9 additions & 1 deletion extend/lbson/src/bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ namespace lbson {
return sprintf(str, "%zd", i);
}

void pack_date(lua_State* L) {
lua_getfield(L, -1, "date");
m_buffer.write<uint64_t>(lua_tointeger(L, -1) * 1000);
lua_pop(L, 1);
}

void pack_int64(lua_State* L) {
lua_getfield(L, -1, "value");
m_buffer.write<uint64_t>(lua_tointeger(L, -1));
Expand Down Expand Up @@ -355,6 +361,8 @@ namespace lbson {
pack_binary(L);
break;
case bson_type::BSON_DATE:
pack_date(L);
break;
case bson_type::BSON_INT64:
case bson_type::BSON_TIMESTAMP:
pack_int64(L);
Expand Down Expand Up @@ -520,7 +528,7 @@ namespace lbson {
lua_pushinteger(L, read_val<int32_t>(L, slice));
break;
case bson_type::BSON_DATE:
lua_pushinteger(L, read_val<int64_t>(L, slice));
lua_pushinteger(L, read_val<int64_t>(L, slice) / 1000);
break;
case bson_type::BSON_INT64:
case bson_type::BSON_TIMESTAMP:
Expand Down
2 changes: 1 addition & 1 deletion extend/lbson/src/lbson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace lbson {
return thread_bson.int64(L, value);
}
static int date(lua_State* L, int64_t value) {
return thread_bson.date(L, value);
return thread_bson.date(L, value * 1000);
}

static void init_static_bson() {
Expand Down
4 changes: 2 additions & 2 deletions script/queue/mongo_mq.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ end
function MongoMQ:send_message(target_id, event, args, ttl)
local doc = { args = args, event = event, target_id = target_id, time = quanta.now_ms, uuid = new_guid() }
if ttl then
--设置过期ttl字段, 单位毫秒
doc.ttl = bdate(quanta.now_ms + ttl * 1000)
--设置过期ttl字段
doc.ttl = bdate(quanta.now + ttl)
end
local ok = mongo_agent:insert({ self.coll_name, doc }, target_id)
if not ok then
Expand Down

0 comments on commit 9172eee

Please sign in to comment.