Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/cvmanjoo/RTC
Browse files Browse the repository at this point in the history
  • Loading branch information
cvmanjoo committed Aug 16, 2022
2 parents edeaa60 + 89c1f9a commit 1965d83
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
18 changes: 13 additions & 5 deletions src/DS1307.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ setDate
-----------------------------------------------------------*/
void DS1307::setDate(uint8_t day, uint8_t month, uint16_t year)
{
// If year is 2 digits.
if(year < 100)
year = year + 2000;
year = year % 100; //Converting to 2 Digit
Wire.beginTransmission(DS1307_ADDR);
Wire.write(0x04);
Expand Down Expand Up @@ -418,13 +421,16 @@ void DS1307::setDateTime(char* date, char* time)
setEpoch()
-----------------------------------------------------------*/

void DS1307::setEpoch(time_t epoch)
void DS1307::setEpoch(time_t epoch, bool is_unix_epoch=true)
{
time_t rawtime;
struct tm epoch_tm, * ptr_epoch_tm;
uint16_t year;
rawtime = epoch;
ptr_epoch_tm = gmtime(&rawtime);
// adjust UNIX epoch to ARDUINO epoch, otherwise `tm` struct
// is one year and one (leap) day off.
if(is_unix_epoch)
epoch = epoch - UNIX_OFFSET;

ptr_epoch_tm = gmtime(&epoch);
epoch_tm = *ptr_epoch_tm;
setSeconds(epoch_tm.tm_sec); //0x00 - Seconds
setMinutes(epoch_tm.tm_min);
Expand All @@ -439,7 +445,7 @@ void DS1307::setEpoch(time_t epoch)
/*-----------------------------------------------------------
getEpoch()
-----------------------------------------------------------*/
time_t DS1307::getEpoch()
time_t DS1307::getEpoch(bool as_unix_epoch=true)
{
time_t epoch;
struct tm epoch_tm;
Expand All @@ -451,6 +457,8 @@ time_t DS1307::getEpoch()
epoch_tm.tm_mon = getMonth() - 1;
epoch_tm.tm_year = getYear() - 1900;
epoch = mktime(&epoch_tm);
if(as_unix_epoch)
epoch += UNIX_OFFSET;
return (epoch);
}

Expand Down
23 changes: 18 additions & 5 deletions src/DS3231.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,18 @@ setDate
-----------------------------------------------------------*/
void DS3231::setDate(uint8_t day, uint8_t month, uint16_t year)
{
uint8_t century;

// If year is 2 digits.
if(year < 100)
year = year + 2000;
century = year / 100;
year = year % 100; //Converting to 2 Digit

Wire.beginTransmission(DS3231_ADDR);
Wire.write(0x04);
Wire.write(bin2bcd(day));
Wire.write(bin2bcd(month));
Wire.write(bin2bcd(month) | (century >= 20 ? 128 : 0));
Wire.write(bin2bcd(year));
Wire.endTransmission();
}
Expand Down Expand Up @@ -581,17 +587,22 @@ setEpoch()
https://en.wikipedia.org/wiki/Epoch_(computing)
-----------------------------------------------------------*/

void DS3231::setEpoch(time_t epoch)
void DS3231::setEpoch(time_t epoch, bool is_unix_epoch=true)
{
uint8_t h_mode, data, century;
uint16_t year;
struct tm epoch_tm, *ptr_epoch_tm;

// adjust UNIX epoch to ARDUINO epoch, otherwise `tm` struct
// is one year and one (leap) day off.
if (is_unix_epoch)
epoch = epoch - UNIX_OFFSET;

ptr_epoch_tm = gmtime(&epoch);
epoch_tm = *ptr_epoch_tm;

century = (epoch_tm.tm_year + 1870) / 100; // Find Century
year = (epoch_tm.tm_year + 1870) % 100; //Converting to 2 Digit
century = (epoch_tm.tm_year + 1900) / 100; // Find Century
year = (epoch_tm.tm_year + 1900) % 100; //Converting to 2 Digit


Wire.beginTransmission(DS3231_ADDR);
Expand Down Expand Up @@ -686,7 +697,7 @@ void DS3231::setEpoch(time_t epoch)
/*-----------------------------------------------------------
getEpoch()
-----------------------------------------------------------*/
time_t DS3231::getEpoch()
time_t DS3231::getEpoch(bool as_unix_epoch=true)
{
uint8_t century_bit;
uint16_t century;
Expand Down Expand Up @@ -719,6 +730,8 @@ time_t DS3231::getEpoch()
epoch_tm.tm_year = epoch_tm.tm_year + century - 1870;

epoch = mktime(&epoch_tm);
if(as_unix_epoch)
epoch += UNIX_OFFSET;
return (epoch);
}

Expand Down
8 changes: 4 additions & 4 deletions src/RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class DS1307
uint8_t getMonth();
uint16_t getYear();

void setEpoch(time_t epoch);
time_t getEpoch();
void setEpoch(time_t epoch, bool is_unix_epoch=true);
time_t getEpoch(bool as_unix_epoch=true);

void setOutPin(uint8_t mode);
bool isOutPinEnabled();
Expand Down Expand Up @@ -151,8 +151,8 @@ class DS3231 {
void setDate(uint8_t day, uint8_t month, uint16_t year);
void setTime(uint8_t hour, uint8_t minute, uint8_t second);

void setEpoch(time_t epoch);
time_t getEpoch();
void setEpoch(time_t epoch, bool is_unix_epoch=true);
time_t getEpoch(bool as_unix_epoch=true);

void setDateTime(char* date, char* time);

Expand Down

0 comments on commit 1965d83

Please sign in to comment.