sleemanj / DS3231_Simple

An Arduino Library for EASY communication with DS3231 I2C RTC Clock and Atmel AT24C32 I2C EEPROM commonly found on the same board. Implements setting, getting the time/date, setting, checking and clearing alarms, and dead-easy circular-buffered logging of data with timestamp.
MIT License
82 stars 25 forks source link

Print POSIX time #14

Open fedcas opened 6 years ago

fedcas commented 6 years ago

Hi, thanks for your library, it's the one that I'm using ;) there's just a couple of things I feel the need for.

A simple question: how can I print the timestamp in epoch format?

Thanks

P.S. opening a separate discussion for the second question ;)

TheAndi commented 3 years ago

Maybe a bit late, but maybe useful for someone finding it:

Using time.h (needs to be included) to convert to UNIX time

int32_t get_unix_time(){
    // Create vars to hold the data 
    DateTime now;
    struct tm time_struct;
    time_t unix_time;

    // Ask the clock for the data (or use any other DateTime-Object here)
    now = Clock.read();

    // modify the struct to the given date:
    time_struct.tm_year   = now.Year + 2000 - 1900; //RTC counts from 2000, UTC from 1900
    time_struct.tm_mon    = now.Month - 1;          //months since January - [0,11]
    time_struct.tm_mday   = now.Day;                //day of the month - [1,31] 
    time_struct.tm_hour   = now.Hour;               //hours since midnight - [0,23]
    time_struct.tm_min    = now.Minute;             //minutes after the hour - [0,59]
    time_struct.tm_sec    = now.Second;             //seconds after the minute - [0,59]

    // create unix time stamp from struct and return it
    return mktime(&time_struct);
}
fedcas commented 1 year ago

Maybe a bit late, but thank you! :p