DateTime calculations with RTClib

Hi All

I need to make a timer in hours which is user selectable from 1 to 23 hours.
When the timer is reached it should open the windows.

When I use more the 9 hours it seams that there is a overflow somewhere.

Please let me know what is the correct way to use the DateTime function.

To set the timer I use:

futureWindowOpen = (now.unixtime() + (WRS * 3600 ) ); //Get time when window opens for fresh air, //in hours

To check the timer I use:
if( ( futureWindowOpen.unixtime() <= now.unixtime() ) && (WindowAtTopFlag == 1) ) //reset window
{
//Open window here
}

Here is the sample code for the RTClib:

// Simple date conversions and calculations

#include <Wire.h>
#include "RTClib.h"

void showDate(const char* txt, const DateTime& dt) {
    Serial.print(txt);
    Serial.print(' ');
    Serial.print(dt.year(), DEC);
    Serial.print('/');
    Serial.print(dt.month(), DEC);
    Serial.print('/');
    Serial.print(dt.day(), DEC);
    Serial.print(' ');
    Serial.print(dt.hour(), DEC);
    Serial.print(':');
    Serial.print(dt.minute(), DEC);
    Serial.print(':');
    Serial.print(dt.second(), DEC);
    
    Serial.print(" = ");
    Serial.print(dt.unixtime());
    Serial.print("s / ");
    Serial.print(dt.unixtime() / 86400L);
    Serial.print("d since 1970");
    
    Serial.println();
}

void setup () {
    Serial.begin(57600);
    

    DateTime dt6 (2011, 05, 17, 0, 0, 0);
    showDate("dt6", dt6);

    DateTime dt7 (dt6.unixtime() + 3600); // one hour later
    showDate("dt7", dt7);

    DateTime dt8 (dt6.unixtime() + (3600 * 3 )); // 3  hours later
    showDate("dt8", dt8);

    DateTime dt9 (dt6.unixtime() + (3600 * 8 )); // 8  hours later
    showDate("dt9", dt9);


    DateTime dt11 (dt6.unixtime() + (3600 * 9 )); // 9  hours later
    showDate("dt11", dt11);
    
     DateTime dt12 (dt6.unixtime() + (3600 * 10)); // 10  hours later
    showDate("dt12", dt12);

    DateTime dt13 (dt6.unixtime() + (3600 * 11 )); // 11  hours later
    showDate("dt13", dt13);
    
    DateTime dt14 (dt6.unixtime() + (3600 * 12 )); // 12  hours later
    showDate("dt14", dt14);
}

void loop () {
}

And here is the results:

dt6 2011/5/17 0:0:0 = 1305590400s / 15111d since 1970
dt7 2011/5/17 1:0:0 = 1305594000s / 15111d since 1970
dt8 2011/5/17 3:0:0 = 1305601200s / 15111d since 1970
dt9 2011/5/17 8:0:0 = 1305619200s / 15111d since 1970
dt11 2011/5/17 9:0:0 = 1305622800s / 15111d since 1970
dt12 2011/5/16 15:47:44 = 1305560864s / 15110d since 1970
dt13 2011/5/16 16:47:44 = 1305564464s / 15110d since 1970
dt14 2011/5/16 17:47:44 = 1305568064s / 15110d since 1970

As you can see the last 3 values is worg.

Thank you in advanced

Regards

Luan

PLease modify your post; and remove the space in [code ] =>

----
this line will catch you:

futureWindowOpen = (now.unixtime() + (WRS * 3600 ) ); 

should be

futureWindowOpen = (now.unixtime() + (WRS * 3600L ) );   // the L makes the datatype long iso int; prevents overflow.

when wrs = 0..8 it will work file as 9 * 3600 <  32767   
10 * 3600 = 36000 => -3233 => overflow error!

Hi Rob

Thank you for the Quick reply.

It seems to be working now I will see tomorrow if it worked.

Lekker Slaap

Regards

Luan