Manually set DS1307 time w/ RTClib.h

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
MobiusRotary
 
Posts: 4
Joined: Thu Dec 05, 2013 12:34 am

Manually set DS1307 time w/ RTClib.h

Post by MobiusRotary »

Hello,

I'm using your data logging shield with the built in DS1307 clock chip, and running the RTClib.h library. What I'd hopefully like to do is to be able to set the date/time using a keypad, rather than setting it using the system time. For instances of power outage with a side of battery failure, and not having a laptop with the source code handy. I know I can do this using the Time and DS1307RTC libraries, but since I have everything already running smoothly with RTClib I would rather not go back and change everything.

Can anyone point me in the right direction to start?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Manually set DS1307 time w/ RTClib.h

Post by adafruit_support_mike »

Date code is always a mess. Facts of nature that don't care what humans think meet a pile of human assumptions dating back thousands of years. The RTCLib does some of the work for you, but leaves one of the annoying problems as an exercise for the reader.

Most time calculations are done on integers that represent the length of time since some reference point called the 'epoch'. For Unix systems, the epoch is midnight, Jan 1, 1970, and the integer is the number of seconds since that instant. Time integers used to be 32 bits long, but that will stop working around 3:14 AM Jan 19 2038 because the 32-bit integer will roll over.. the Y0xffffffff problem.

Most time systems now use a signed 64-bit integer. That makes it possible to express a span a bit larger than half a billion years with millisecond resolution. The epoch is still midnight Jan 1, 1970 for the sake of compatibility with all the existing date code out there.

The RTCLib has a function called `time2long()` which produces a 32-bit date code from human-readable values, but one of the values is 'the number of days since the epoch'. You'll have to calculate that yourself, or at least the number of days from some other reference point.

Most date calculations start on Mar 1 because there are 1461 days in a leap year cycle. Every month-day during that cycle is 365 days later than the previous one, except for the single Feb 29 which (if you arrange things that way) is the last day of the cycle. The current leap year cycle started 15400 days after the epoch (Mar 1 2012).

Actually, most time algorithms use a 146,097-day cycle which corresponds to a complete 400-year cycle of leap years, but you can probably do without that since the next divisible-by-100 leap year will be in the year 2400.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”