在 10 年跨度和 1 秒分辨率下,什么时间戳具有最佳空间效率?
对于低端嵌入式微控制器(8 位),哪种时间戳结构最小?我也在考虑定制。由于计算能力非常有限,因此读取秒、小时或天等的速度也应该很快,这一点也很重要。
这个问题涵盖了我的问题,但我需要以第二个决议来代表未来至少 10 年。
更新: 我将在有限的 EEPROM 空间中存储许多时间戳。因此,尺寸效率对我来说更重要。计算(确定当前时间戳是否大于另一个 2 或 3 个时间戳,在定制设计的 LCD 上显示当前时间戳)通常每秒进行一次。
For a low end embedded microcontroller (8-bit), which timestamp structure would be smallest? I'm considering custom ones too. Because of computing power is very limited, it's also important to reading second, hour, or day etc. should be fast too.
This question covers my question, but i need to represent next minimum 10 years with resolution of second.
Update:
I will store many timestamps in limited EEPROM space. So size efficiency has more weight on my case. Calculations (determining current timestamp is greater than another 2 or 3 one, displaying current timestamp on a custom design lcd) generally takes place on every second.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用
int32_t
,您可以使用这些时间戳直到 2038 年,而使用uint32_t
您可以覆盖到 2106 年。如果需要,可以使用localtime
、gmtime
等将它们转换为struct tm
并提取日期、月份等。If you use an
int32_t
you're good until 2038 with these timestamps, and withuint32_t
you can cover until 2106. Usinglocaltime
,gmtime
and such you can convert them intostruct tm
if you need and extract the day, month etc.这在一定程度上取决于您想要对所述时间戳执行什么操作以及它来自哪里。
通常在嵌入式情况下,系统会有一个 RTC(实时时钟),您有吗?或者您是否使用处理器时钟和/或 1Hz 计时器来跟踪时间?如果您确实有 RTC,我会倾向于使用时钟的格式并保存任何进一步的处理。
同样相关的是您是否需要在本地处理这个时间戳?如果您需要在微型计算机本身上使用它,那么以与您需要的格式类似的格式保存它将会有明显的好处。例如,如果您需要在屏幕上显示日期,则将其保存为类似于 您已经链接的答案是有道理的。
一般来说,尽管对于大多数嵌入式工作,我发现正如已经建议的那样,使用表示您选择的任何纪元的秒数的 32 位无符号整数是最好的。如果您必须比较值,这是一个不错的选择,因为它是简单的算术比较。
根据 BCD 十进制,关于将 BCD 转换为 示例,虽然该问题最初是 C# 的,但 C 中的答案应该几乎相同。
This depends a bit on what you want to do with said timestamp and where it will come from.
Often in embedded situations the system will have a RTC (Real-Time Clock), do you have one? Or are you keeping track of time using the processors clock and or a 1Hz timer? If you do have an RTC I would be inclined to use the format from the clock and save any further processing.
Also relevant is whether you need to process this timestamp locally? If you need to work with it on the micro itself there will be distinct benefits to saving it in a format similar to that you need. For example if you need to display the date on a screen saving it in the packed format similar to that of the answer you already linked makes sense.
Generally, though for most embedded work I find as already suggested, using a 32-bit unsigned integer representing seconds from whatever epoch you choose is best. This is a good choice if you have to compare values as it is simple arithmetic comparison.
As per the BCD decimal there are quite a few questions about converting out of BCD for example, whilst that question was originally C# the answer should be almost identical in C.
由于 π 秒是一个纳世纪(或 3100 万秒是一年),因此您需要能够表示高达 3.2×108 的值,才能存储长达 10 年的数据。因此,您至少需要 29 位来存储此类值,这使得 32 位数字成为显而易见的选择。您需要考虑如何定义纪元 - 时间 0 的开始日期。但是 32 位有符号数字可以存储长达 68 年的秒数(想想 Unix;
time_t 的 32 位有符号值
支持范围 1970-2037),这个范围很大。Since π seconds is a nano-century (or 31 million seconds is one year), you need to be able to represent values up to 3.2×108 to store up to 10 years worth of data. So, you need at least 29 bits to store such values, which makes a 32-bit number the obvious choice. You need to consider how you are going to define your epoch - the start date for time 0. But a 32-bit signed number can store up to 68 years worth of seconds (think Unix; 32-bit signed values for
time_t
supports the range 1970-2037), which is plenty of range.一天有 86,400 秒。
暂时忽略闰年(以及闰秒),一年有 31,536,000 秒。
10 年内达到 315,360,000 人。
添加 10 年跨度内可能出现的三个闰日,您将得到: 315,619,200
该数字需要 29 位来表示,因此您不妨使用 32 位表示。
There are 86,400 seconds in a day.
Ignoring leap years for a moment (and leap seconds altogether), there are 31,536,000 seconds in a year.
That makes 315,360,000 in a 10 year span.
Add in the three leap days that could occur in a 10 year span and you get: 315,619,200
That number requires 29 bits to represent, so you might as well use a 32 bit representation.
我会使用无符号的 32 位。编写一个例程,将当前日期/时间转换为自开始时间以来的秒数,并编写一个例程,将秒数转换为日期/时间。
每个设备都可以将 0 作为第一个输入设备的日期/时间。然后,每当输入或输出日期/时间时,都会使用正确的函数对其进行处理。用户永远不知道自设置日期/时间以来,这些值在内部存储为秒。
I would use an unsigned 32 bit. Write a routine to convert current date/time to seconds since the start time and one to convert seconds to date/time.
Each device can make 0 be the first date/time input into device. Then whenever the date/time is input or output it is massaged using the correct function. The user does not ever know that internally the values are stored as seconds since date/time were set.