too many RTC libraries?

I am back on my simple date logging project, completely confused, I have looked at many libraries like Yet-Another DS1337 RTC library, and many of them look like I could do the simple task I require, but one uses unix , another based on year 2000, and I can't quite get it working. I have rethought the basic idea, after failing to get the upper and lower bytes of unix days stored and read back from the 1307 RTC ram. The new idea would work out much better than what I was trying, so I have scrapped the old sketch. I have three LED displays,

1 shows the number of days since the last event ( 2 digits 0-99)

2 shows the previous best ( longest) number of days since the last event ( 2 digits 0-99 )

3 shows the day, month, and year of the last event 2-2-2 digits.

The data for each is stored in the DS1307 ram in case power goes off I have a loop checking the date every minute, and if the day has changed, it recalculates the elapsed days, and refreshes the ram. I have buttons on a remote to set the displayed previous days month and year ( as well as to set the previous best display, and one to register an event ) If an event is registered, I calculate the larger of the "elapsed" and "previous best" days which becomes " previous best", "elapsed" is set to zero, and the current date becomes the previous date displayed.

What I want to try, is have the "elapsed days display" calculated on the fly ( in each loop ) from the difference between the current date and the previous date, with each press of the set date buttons - i.e. each loop.

I will need to store the previous date in the ram, ( which is where I had all the trouble using unix divided into days as a longint split into highByte and Low byte ) so if I can just store it as 3 integers :- "prevday" "prevmonth" "prevyear"

I need to be able to subtract current "day" "month" "year" from the RTC readDate ( or whichever library I use ) from the "prevday" "prevmonth" "prevyear" .

Can anyone point me to a library ( or simple instructions in Time.h ) or an example ,whereby I can convert any given YYMMDD to unix (or eclipse? or whatever) and back again after doing the subtraction?

Do you have some code allready?

You should stick to one timeframe - either unix or year2000 - it does not make a difference (until overflow).

unsigned long prevDay = timestamp in seconds; // unsigned long toDay = other timestamp in seconds;

unsigned long DaysPassed = (toDay - prevDay) / 86400L;

Most RTC libs have some conversion class. - http://www.ladyada.net/make/logshield/rtc.html -

they use now.unixTime() to get the time in seconds...

Does this help?

Thanks Rob, yes I was using unix on the abandoned sketch, divided by 86400 to work in days, but its still a an unsigned long, and I had problems simply storing these numbers in the ram of the 1307. I was splitting into upper and lower bytes.

The new idea I want to try ( presetting the date and letting the micro work out the elapsed days ) would suit me much better.

I have looked at the adafruit librabry ( amongst many many others ) and it seems I want a backward version of

DateTime now = RTC.now(); where I can get DateTime then and give a unix value, and back again.

I have no problem storing the individual bytes of DDMMYY and the 0-99 value for previous best.

unsigned long DaysPassed = (toDay - prevDay) / 86400L;

But as you are only interested in 0..99 days you can cast this value to byte of course, making things lot easier for you.

uint8_t DaysPassed = (uint8_t) ((toDay - prevDay) / 86400L);

Thanks I will try that, have been all afternoon trying to get the internet access going again !

I still need to store the previous date in eprom, but I will try the upper lower bytes again, unix converted to days so it will only be 2 bytes.

It would still be easier if I could just store the dd mm yy ( which I need for the display anyway ) and find how to convert that to unix time so I could do a straight subtraction for the days elapsed.

Any change of the old dd mm yy with the remote, would automatically display the days elapsed ...

I saw the help you gave re the same question a ferw weeks back i.e.

unsigned long timeDiff( tmElements_t &tm1, tmElements_t &tm2)
{
return makeTime(tm2) - makeTime(tm1);
}

But it doesnt mean anything to a newbie like me :slight_smile: for today would tmElements_t &tm1 be for example, 09,10,11&tm1 ?

The easiest way to do timebased applications is working in seconds. That is the time you do all your math with. Any RTC can provide a second based time model. Best known are year2000 and unix. They differ 30 years in startdate thats all.

If you want to show something on an LCD you do some math to make it presentable in a nice form (YYYYMMDD etc) that is where DateTime classes come in. These are made for converting seconds since 1-1-1970 into something human readable.

unsigned long timeDiff( tmElements_t &tm1, tmElements_t &tm2) { return makeTime(tm2) - makeTime(tm1); }

The code above takes 2 tmElements_t which contains fields like Days, Hours, minute, seconds etc (IIRC) and convert these to a unix second model by means of the makeTime function. Then it becomes trivial to subtract both to get the difference in seconds..

That's all,

more clear now ?

I understand the seconds, etc, it was just the syntax of " tmElements_t &tm1, tmElements_t &tm2 " that confused me a bit, I am sure I can manage it now. Thanks for the help, sometimes reading through libraries like Time have too many unknowns for learners to see what is happening.

After 3 days of confused attempts, I did at one time manage to get the conversion from unix to yymmdd working, though I have lost my sketch when the pc crashed again :0 but I think I can get that going again.

The other half is still eluding me, just converting yymmgg to unix. I just want to create an unsigned long that I can use for calculations.

The elapsed time is not needed, I can do this with the unix time, but I need to be able to use the results of both conversions independantly.

I have read all the libraries, but I usually end up with "tm not declared " , or many other errors, I do not understand" &tm" , or find any basic explanations of use of the time elements.

( Just posting this in case somebody gets to this thread from a search and wants to see the outcome )

Finally its all working great, I went back to breaking the long unidays ( unix/86400 ) into higher and lower bytes to store in the RTC eprom, joining them again to make the previous events " unix days"

The elapsed days are simply a subtraction of the old and new days,

I convert that back to seconds to extract the DDMMYY to display as the previous event day with:-

prevunisecs = prevunidays * 86400; time_t t = prevunisecs; dayint = day(t); monthint = month (t); yearint = year (t);

The beauty now this way is that I have an up and down button on the remote, that increments ( or decrements ) the prevunidays ( and stores it each push ) and the user sees the elapsed days and the displayed date changing in unison with automatic month adjustments on the date, and when the elapsed days reaches 99 it freezes, but the date continues decreasing.

Just when you think you are getting on well with learning this new stuff, something as simple as this project, ( and the time I have spent on it,) reminds you that you know so little ! But I am enjoying the learning...