Unix time and RTC Real Time Clock Module

Our range of tiny modules are designed to make it easy for you to quickly add functionality to your projects. [Module List]
Post Reply
hoolio
Posts: 3
Joined: Tue Jul 10, 2012 11:45 am

Unix time and RTC Real Time Clock Module

Post by hoolio » Mon Jan 18, 2016 4:05 am

Hi All,

I have followed the Freetronics RTC doco at http://www.freetronics.com.au/pages/rtc ... pd32pN95E4,
First, retrieve the time data from the RTC with

Code: Select all

rtc.readTime(&time);
then you can work with the time parameters by using the byte variables

Code: Select all

time.hour
time.minute
time.second
and it works as expected. What i need however is a library that works with the RTC Module in the same way, and returns unix time (seconds since epoch). Having tried several libraries already i seem to be missing something. I'd like to be able to just;

Code: Select all

git clone http://github.com/somedude/<library>
and then in my code;

Code: Select all

#include <library>
rtc.readTime(&time);
<library>.unixtime
I have tried other libraries such as https://github.com/PaulStoffregen/Time.git but either i don't know what i'm doing, or it doesn't work.

pls help :) kind regards, Hoolio

hoolio
Posts: 3
Joined: Tue Jul 10, 2012 11:45 am

Re: Unix time and RTC Real Time Clock Module

Post by hoolio » Tue Jan 19, 2016 10:00 pm

hoolio wrote: I have tried other libraries such as https://github.com/PaulStoffregen/Time.git but either i don't know what i'm doing, or it doesn't work.
Ok, i have fixed it. While i had compilation errors even with the examples from PaulStoffregen/Time.git, hidden in there was a specific function which i was able to tailor to suit my needs, with the added advantage of not having to include a whole other library. Now performing time calculations is simple arithmetic, as it should be.

So to be clear, this fragment will return unix time (seconds since epoch) eg; 1453174405 for the Freetronics DS3232 Real Time Clock Module. I have compared the the output using http://www.unixtimestamp.com/index.php and the results were acceptably within the expected range, given that I'm not at UTC and that site is.

Code: Select all

#include <SoftI2C.h>
#include <DS3232RTC.h>

SoftI2C i2c(A4, A5);
DS3232RTC rtc(i2c);

#define LEAP_YEAR(Y)     ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) )
#define SECS_PER_DAY 86400
#define SECS_PER_HOUR 3600
#define SECS_PER_MIN 60

static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0

void setup() {
    Serial.begin(9600);
}

void loop() {

    uint32_t epoch = time_t();

    Serial.println(epoch);
    delay(1000);
}

uint32_t time_t(){
    // assemble time elements into time_t 

    RTCTime time;
    RTCDate date;
    rtc.readTime(&time);
    rtc.readDate(&date);

    int i;
    uint32_t seconds;

    // seconds from 1970 till 1 jan 00:00:00 of the given year
    seconds = (date.year-1970)*(SECS_PER_DAY * 365);
    //Serial.print("      years: ");  Serial.println (seconds);  

    for (i = 1971; i < date.year; i++) {
        if (LEAP_YEAR(i)) {
            seconds += SECS_PER_DAY;   // add extra days for leap years
            //Serial.print(" leap "); Serial.print(seconds);
        }
    }
    //Serial.print(" plus leaps: ");  Serial.println (seconds);  

    // add days for this year, months start from 1
    for (i = 1; i < date.month; i++) {
        //Serial.println("for");
        if ( (i == 2) && LEAP_YEAR(date.year)) { 
            seconds += SECS_PER_DAY * 29;
            //Serial.println("if");
        } else {
            seconds += SECS_PER_DAY * monthDays[i];  //monthDay array starts from 0
            //Serial.println("else");
        }
    }

    //Serial.print(" plus months: ");  Serial.println (seconds);  
    seconds+= (date.day-1) * SECS_PER_DAY;       //Serial.print("  plus days: ");  Serial.println (seconds);  
    seconds+= time.hour * SECS_PER_HOUR;         //Serial.print(" plus hours: ");  Serial.println (seconds);  
    seconds+= time.minute * SECS_PER_MIN;         //Serial.print(" plus mins: ");  Serial.println (seconds);  
    seconds+= time.second;                      //Serial.print(" plus secs: ");  Serial.println (seconds);  
    //Serial.println("end"); Serial.println();

    return seconds; 
}
and i've uploaded it to https://github.com/hooliowobbits/arduin ... ixTime.ino. Please feel free to use this at your pleasure :)

Post Reply