7

Does anyone know of a simple gmtime or ctime implementation without consideration for timezone, no external dependencies, and a non-copyleft license (BSD/MIT/anything proprietary-safe)? Preferably in C, but basically anything that gives me the algorithm in its minimal form would work.

I just need seconds since "Jan 1, 1970 00:00:00" broken down into year, day, month, hr, min, sec. And it's almost time to go home on a Friday so I'm feeling a bit lazy.

Brian McFarland
  • 9,052
  • 6
  • 38
  • 56

3 Answers3

7

Reverse the algorithm defining "Seconds Since the Epoch" from POSIX:

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_15

tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
    (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
    ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • If you need this direction (not the one in the question), here is a short implementation: https://github.com/pts/minilibc686/blob/master/fyi/c_timegm.c – pts Jul 01 '23 at 13:32
2

Try newlib http://sourceware.org/newlib/ it is a BSD license.

It is designed for embedded systems, so tends to be quite small and simple.

Look at Docs -> Timefns to see if ctime or gmtime meet your needs.

gbulmer
  • 4,210
  • 18
  • 20
  • I'm already using functions from newlib, so I dunno why I didn't think to look there. `mktm_r` (called by `gmtime` via `gmtime_r`) from newlib looks like it will work quite nicely. – Brian McFarland Mar 17 '12 at 00:38
  • I think you might have answered "...why I didn't think to look there" already: "And it's almost time to go home on a Friday ..." B-) We all get a bit tired (z_z) – gbulmer Mar 17 '12 at 01:03
  • So to anyone else who stumbles upon this post and may question the license of that part of newlib since it's not explicitly declared--mktm_r is derived from tzcode which is public domain. That doesn't mean the derivation is too, but you can always re-derive if necessary. – Brian McFarland Mar 19 '12 at 14:19
  • @Brian McFarland - You have panicked me. I am unclear, are you telling me something that I need to carefully consider? I had assumed that the [newlib license](http://sourceware.org/newlib/COPYING.NEWLIB) explicitly applied BSD license terms to all source unless the file carried its own license. So are you saying that, even though that file mktm_r.c does not carry its own license statement, but only identify the authors, that it is not covered by the newlib license? And further, that parts have to be re-derived? This may be bad! I must state, that is not my understanding, by I am not a lawyer. – gbulmer Mar 19 '12 at 14:38
  • Yeah, it looks like you're right. Anything that doesn't explicitly declare another license is supposed be under the newlib BSD terms. So I guess I'm safe copying mktm_r. Sorry to cause panic. I am also not a lawyer, but like to err on the side of being extra cautious. Both out of respect for the FSF people and for fear of litigation. – Brian McFarland Mar 19 '12 at 15:27
  • @Brian McFarland - PHEW!-) Thank you very much for getting back quickly. I agree, be cautious. I became extra careful after a well-known consultant told me many of his larger IT clients spent more on their litigation department than they did on development, and that his company made more money as expert witnesses in litigation, than they did from improving companies software development capability (which was their 'real' business) :-( – gbulmer Mar 19 '12 at 15:42
  • here's a direct link to [`mktm_r.c` (rev. 1.7), to view it online](https://sourceware.org/cgi-bin/cvsweb.cgi/src/newlib/libc/time/mktm_r.c?rev=1.7&content-type=text/x-cvsweb-markup&cvsroot=src&only_with_tag=HEAD) – jfs Dec 29 '13 at 22:00
  • Here is an updated link to the source code: https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/time/gmtime_r.c;h=8bf9ee52dd1e54e39d2b1516b0208375104e4415;hb=HEAD – pts Jun 29 '23 at 22:25
  • Please note that the linked Newlib implementation doesn't conform to [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_15) for negative years (i.e. BCE). See my answer for an implementation which does. – pts Jul 01 '23 at 13:33
0

Another short C implementation with MIT license which works with time_t of any size (32 bits, 64 bits etc.): https://github.com/pts/minilibc686/blob/master/fyi/c_gmtime.c

Size-optimized i386 assembly, with 32-bit time_t: https://github.com/pts/minilibc686/blob/master/src/gmtime_r.nasm

Compiled code size comparison (size-optimized for i386, with 32-bit time_t):

pts
  • 80,836
  • 20
  • 110
  • 183