Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What timestamp would have the best space efficiency with a 10 year span and 1 second resolution?

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.

like image 363
useraged Avatar asked Aug 23 '11 13:08

useraged


People also ask

How many bits is a timestamp?

Seconds and fraction of seconds: timestamps are represented as a 64-bit unsigned fixed-point number, in seconds relative to 00:00:00 January 1, 1970, UTC.

How many bytes is Unix timestamp?

The internal representation of a timestamp is a string of between 7 and 13 bytes. Each byte consists of 2 packed decimal digits. The first 4 bytes represent the date, the next 3 bytes the time, and the last 0 to 6 bytes the fractional seconds.

How many bits are used to store the day in date?

60 Minutes/Hour means you'd need at least 6 bits to store the minute (since 59th minute == 111011b), while 24 Hours/Day means another 5 bits (23rd hour == 10111b). If you want to account for any of the (possibly) 366 Days/Year, you'd need 9 more bits (366th day (365 when day 1 == 0) == 101101101b).


2 Answers

i need to represent next minimum 10 years with resolution of second

If you use an uint32_t you're good until 2038 with these timestamps. Using localtime, gmtime and such you can convert them into struct tm if you need and extract the day, month etc.

like image 72
cnicutar Avatar answered Sep 27 '22 17:09

cnicutar


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.

like image 35
Charles Keepax Avatar answered Sep 27 '22 17:09

Charles Keepax