Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimal implementation of gmtime algorithm?

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.

like image 889
Brian McFarland Avatar asked Mar 16 '12 22:03

Brian McFarland


2 Answers

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
like image 102
R.. GitHub STOP HELPING ICE Avatar answered Sep 23 '22 23:09

R.. GitHub STOP HELPING ICE


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.

like image 29
gbulmer Avatar answered Sep 25 '22 23:09

gbulmer