Another improvement of gmtime_r()

Freddie Chopin
Wed May 20 14:46:00 GMT 2015


You may assume that I have some strange obsession with gmtime_r(), but 
that wouldn't be true (; I just found a very good document about date 
algorithms with very good code samples that are placed in public domain, 
so I couldn't resist checking how does that compare to current newlib's 

I've added the author of the code to CC list.

It turns out that gmtime_r() with the algorithm from civil_from_days() 
by Howard Hinnant is ~13% faster on ARM Cortex-M4 - 3 million 
calculations take 29.5s vs 34s. The code is slightly larger, but we're 
talking about ~100 bytes in that case.

I've verified the correctness of the calculations by comparing the 
modified code with results of gmtime_r() from glibc over the span of 
~1600 years (with 64-bit time_t), every 60 seconds - the results match. 
I've also compared the results of the modified function with result of 
current code in newlib and they are also equal.

I attach patch and a Changelog entry.

-------------- next part --------------
2015-05-19  Freddie Chopin  <>

	* libc/time/gmtime_r.c (gmtime_r): use faster algorithm from
	civil_from_days() by Howard Hinnant
-------------- next part --------------
A non-text attachment was scrubbed...
Name: libc-time-gmtimerc-gmtimer-use-faster-algorithm-from.patch
Type: text/x-patch
Size: 6191 bytes
Desc: not available
URL: <>

More information about the Newlib mailing list