我如何建模“相对”数据库中的时间?
澄清:我并不是想通过使用时间戳和当前时间来计算友好时间(例如“8秒前”)。
我需要在数据模型中创建事件时间线,但这些事件仅彼此相关。例如,我有事件 A、B 和 C。它们按顺序发生,因此 B 可能在 A 之后 20 秒发生,而 C 发生在 20 年之后B之后。
我不在乎时间单位。对我来说,没有时间,只有相对论。
我打算将其建模为一个链接列表,其中每个事件都是一个节点:
事件
- id
- name
- prev_event
- next_event
这是建模相关事件的最有效方法吗?
Clarification: I am not trying to calculate friendly times (e.g., "8 seconds ago") by using a timestamp and the current time.
I need to create a timeline of events in my data model, but where these events are only relative to each other. For example, I have events A, B, and C. They happen in order, so it may be that B occurs 20 seconds after A, and that C occurs 20 years after B.
I don't care about the unit of time. For my purpose, there is no time, just relativity.
I intend to model this like a linked list, where each event is a node:
Event
- id
- name
- prev_event
- next_event
Is this the most efficient way to model relative events?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计算机记录的所有时间都是相对时间,名义上它是相对于纪元的,以毫秒为单位的偏移量。通常这个纪元是从 1970/01/01 开始的偏移Unix 的情况。
如果您存储正常的日常
时间戳
值,则您已经拥有事件之间的相对时间(如果它们都是连续的),您只需减去它们即可获得间隔 这是您所说的相对时间,但它们实际上是间隔。您可以使用您需要使用的任何分辨率,
毫秒
是大多数东西使用的,如果您以亚毫秒分辨率采样,您将使用纳秒
All time recorded by computers is relative time, nominally it is relative to an epoch as an offset in milliseconds. Normally this epoch is an offset from 1970/01/01 as is the case with Unix.
If you store normal everyday
timestamp
values, you already have relative time between events if they are all sequential, you just need to subtract them to get intervals which are what you are calling relative times but they are actually intervals.You can use whatever resolution you need to use,
milliseconds
is what most things use, if you are sampling things at sub-millisecond resolution, you would usenanoseconds
我认为您不需要链接到上一个和下一个事件,为什么不只使用时间戳并按时间戳排序?
如果您可以有多个同时发生的事件时间线,那么您将使用某种标识符来标识时间线(int、guid 等)并使用时间戳键入该时间线。甚至不需要 id,除非您需要通过单个数字来引用它。
像这样的东西:
事件
I don't think you need to link to previous and next event, why not just use a timestamp and order by the timestamp?
If you can have multiple, simultaneous event timelines, then you would use some kind of identifier to identify the timeline (int, guid, whatever) and key that in witht the timestamp. No id is even necessary unless you need to refer to it by an single number.
Something like this:
Event