Date and time serial: how to bypass the lack of precision of floats

Data type is probably the first concern for any new programmer in c/c++. These language require in minimal knowledge about integers and floats (Check this thread). Although these data types look familiar to all educated people, the principle of significant figures, which should be the universal way of formatting data, is, most of the time, ambiguous, foggy, unsure, etc.. Pocket calculators are mainly responsible for this lack of understanding; at the time of slide rules, who would bother about the nth precision digit and carry these insignificant figures over a bunch of operations ? Nowadays, most students are happy to copy the full content of their display as the result of operations which lead to express physical measurements with quasi unit-less figures.

Back to to the world of 8 bits controllers, the range and precision of floats must be taken into account and results shall be carefully interpreted before releasing a code. As I was recently working on RTC drivers and applications, I faced the need for exporting time-stamped logged data to a PC based application.

The best idea I could find to express absolute date and time consists in using the “Microsoft” – nobody’s perfect – date and time serial. This float is made of an integer part which represents the number of days since the 1 Jan 1900 at 0:0:0 (date serial 1 because of the Lotus-1-2-3 bug which was agreed by Excel for compatibility reasons !); the fractional part is a fraction of 1 day, so as to say 1 s = (1 / 24 * 60 * 60). Using these rules, it is right now 43604.685417 and that’s a lot more precision than the float data type can handle. However, sticking to serial date and time makes a lot of sens as converting the serial in MS EXCEL or compatible spreadsheets such as LibreOffice Calc, is as easy as: year(A1) & “-” & month(A1) & “-” & day(A1) & ” ” & hour(A1) & “:” & minute(A1) & “:” & second(A1), with the serial data and time in cell A1.

The trick I used is rather simple: firstly, I recorded the date serial as a 32 bits integer (16 bits suffice if your are short in memory) and the time serial in a 32 bits float. Then I reconstruct the full data and time information for printing and exporting to an application which can handle doubles. Next is the simple function which concatenates both serials:

/*
concatenate a 32 bits integer with a fractional 32 bits float to print a date 
and time serial (iiiii.ffffff)
*/
void printDateAndTimeSerial(uint32_t date, float time)
{
	/* create char array buffer */
	char *ptrArray = (char*)malloc(11 * sizeof(char));
	/* convert the integer part in a char array */
	ptrArray = utoa(date, ptrArray, 10);
	/* create char array buffer for the float conversion */
	char *ptrFloat = (char*)malloc(8 * sizeof(char));
	/* convert the float in an char array */
	ptrFloat = dtostrf(time, 8, 6, ptrFloat);
	/* copy the fractional part of the float, including the decimal separator */
	memcpy(ptrArray + 5, ptrFloat + 1, 7);
	/* print result */
	Serial.print(ptrArray);
	/* free memory space */
	free(ptrArray);
	free(ptrFloat);
}

HTH

Leave a Reply

You must be logged in to post a comment.