如何在4个字节内保存日期时间戳 [英] How to save date time stamp within 4 bytes
问题描述
我想将嵌入式系统的开始时间和日期记录到内存中。
在这里我想知道有什么标准可以在4个字节内保存日期时间戳。
该系统在具有免费rtos的arm7上工作。董事会包含RTC
谢谢。
I want to log my embedded system start time and date in to memory.
here I want to know that there is any standard to save datetime stamp within 4 bytes.
The system is worked on arm7 with free rtos. The board contains RTC
Thanks.
推荐答案
以下结构显示了三种存储时间和日期的方法在4个字节(或32位)之内。
The structures below show three methods of storing time and date within 4 bytes (or 32 bits).
#pragma pack(1)
typedef struct TIMESTAMP_32_A_S
{
uint8_t seconds : 6; // 0-60 (0-63 max)
uint8_t minutes : 6; // 0-60 (0-63 max)
uint8_t hours24 : 5; // 0-23 (0-31 max)
uint8_t dayOfMonth : 5; // 1-31 (0-31 max)
uint8_t month : 4; // 1-12 (0-15 max)
uint8_t year : 6; // Epoch start: 2014, Range: 2014 thru 2077
} TIMESTAMP_32_A_T;
#pragma pack()
以上结构假定<$ c $的粒度c> seconds 对应用程序很重要。它还假设在 year
2014之前或 year
2077之后不会执行该应用程序(此时时间,年
会追溯到2014年。)
The structure above assumes that the granularity of seconds
is important to the application. It also assumes that the application will not be executed prior to the year
2014, or after the year
2077 (at which time, year
will roll back to 2014).
#pragma pack(1)
typedef struct TIMESTAMP_32_B_S
{
uint8_t minutes : 6; // 0-60 (0-63 max)
uint8_t hours24 : 5; // 0-23 (0-31 max)
uint8_t dayOfMonth : 5; // 1-31 (0-31 max)
uint8_t month : 4; // 1-12 (0-15 max)
uint16_t year : 12; // Epoch start: 2014, Range: 2014 thru 4061
} TIMESTAMP_32_B_T;
#pragma pack()
以上结构假定秒
与该应用程序无关。它还假定在 year
2014之前或 year
4061之后不会执行该应用程序(此时时间,年
将回溯到2014年)。
The structure above assumes that seconds
are irrelevant to the application. It also assumes that the application will not be executed prior to the year
2014, or after the year
4061 (at which time, year
will roll back to 2014).
#pragma pack(1)
typedef struct TIMESTAMP_32_C_S
{
uint8_t secondsBy2 : 5; // 0-30 (0-31 max)
uint8_t minutes : 6; // 0-60 (0-63 max)
uint8_t hours24 : 5; // 0-23 (0-31 max)
uint8_t dayOfMonth : 5; // 1-31 (0-31 max)
uint8_t month : 4; // 1-12 (0-15 max)
uint8_t year : 7; // Epoch start: 2014, Range: 2014 thru 2141
} TIMESTAMP_32_C_T;
#pragma pack()
以上结构假定 2秒间隔的秒数足以满足应用程序的需要。它还假定在
year
2014之前或 year
2141之后不会执行该应用程序。时间,年
将回溯到2014年)。 MSDOS使用了类似的结构。
The structure above assumes that a seconds
granularity of 2-second intervals are is sufficient to the application. It also assumes that the application will not be executed prior to the year
2014, or after the year
2141 (at which time, year
will roll back to 2014). MSDOS used a similar structure.
这篇关于如何在4个字节内保存日期时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!