日期时间作为键
我正在尝试使用日期时间类型作为 b 树 BerkeleyDB 数据库中的键。我的目标:
- 按日期进行日期时间存储键比较的最小开销
- (检索范围)
- 合理的速度
如何以最紧凑的形式表示日期时间并使用默认的 bsddb 的键比较算法?
用 C 语言执行此操作并为此类任务创建小型 Python 扩展是否很难?我没有 C 经验,只能理解小的 C 代码片段(并复制粘贴它们)。
I'm trying to use datetime type as a key in b-tree BerkeleyDB database. My goals:
- minimum overhead for datetime storage
- key comparison by date (to retrieve range)
- reasonable speed
How to represent datetime in most compact form and use default bsddb's key comparison algorithm?
Is it hard to do this in C and create small Python extension for such tasks? I'm not experienced in C and only able to understand small C snippets (and copy-paste them).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对什么范围的日期时间值感兴趣?当时的决议是什么?
正如评论中指出的 fge 所示,如果您希望在 1902-2037 的时间内获得 1 秒的分辨率,那么您可以使用一个 32 位有符号整数和自 Unix 纪元以来的秒数,即 1970-01-01 00:00:00 +00:00(午夜UTC 时间 1970 年 1 月 1 日)。如果您想要更宽的范围,那么您可能应该使用相对于 Unix Epoch 的 64 位有符号整数。如果您想要亚秒级精度,请存储一个 32 位有符号整数,即纳秒数。请注意,对于负时间(1970 年之前),秒的小数部分也应该为负数。
建议使用这些表示法的原因之一是可以通过标准 Unix (POSIX) 接口轻松找到该值,例如用于 1 秒分辨率的 time() 和 Clock_gettime() 接口对于纳秒分辨率或
gettimeofday()
对于微秒分辨率。What range of datetime values are you interested in? And what resolution on the time?
As fge indicated in a comment, if you want 1 second resolution over a period limited to 1902-2037, then you can use a 32-bit signed integer and the number of seconds since the Unix Epoch, which is 1970-01-01 00:00:00 +00:00 (midnight on 1st January 1970 in UTC). If you want a wider range, then you should probably use a 64-bit signed integer relative to the Unix Epoch. If you want sub-second accuracy, store a 32-bit signed integer which is the number of nanoseconds. Note that for a negative time (before 1970), the fractional seconds should be negative too.
One reason for suggesting these representations is that the value can easily be found via standard Unix (POSIX) interfaces, such as
time()
for 1-second resolution andclock_gettime()
for nanosecond resolution orgettimeofday()
for microsecond resolution.