JChristensen / DS3232RTC

Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks
GNU General Public License v3.0
392 stars 135 forks source link

error: 'class DS3232RTC' has no member named 'now' #99

Closed australopitheque closed 2 years ago

australopitheque commented 2 years ago

Good morning, I know that this is not really an error but would it be possible to add the subclass "Now()" in DS3231RTC. It would avoid adding a library that could add errors since this one works perfectly.

example that does not pass:

DateTime now = myRTC.now();

JChristensen commented 2 years ago

I'm not familiar with the DateTime data type. Would you provide a reference please?

australopitheque commented 2 years ago

DateTime and now() is in the RTClib.CPP library (sorry for formatting)

DateTime DS3231::now() { WIRE.beginTransmission(DS3231_ADDRESS); WIRE.write(DS3231_TIME_ADDR); WIRE.endTransmission(); WIRE.requestFrom(DS3231_ADDRESS, 7); uint8_t ss = bcd2bin(WIRE.read() & 0x7F); uint8_t mm = bcd2bin(WIRE.read()); uint8_t hh = bcd2bin(WIRE.read()); WIRE.read(); uint8_t d = bcd2bin(WIRE.read()); uint8_t cen = WIRE.read(); uint8_t m = bcd2bin(cen & 0x1F); uint16_t y = bcd2bin(WIRE.read()); if ((cen & 0x80) >> 7 == 1) { y += 2000; } else { y += 1900; } return DateTime(y, m, d, hh, mm, ss); } and in the RTClib.h

`// Simple general-purpose date/time class (no TZ / DST / leap second handling!) class DateTime { public: char format(char ret); char tostr(char charr); DateTime(uint32_t t = 0); DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour = 0, uint8_t min = 0, uint8_t sec = 0); DateTime(const char date, const char time); DateTime(const FlashStringHelper* date, const FlashStringHelper time); DateTime(const char sdate); // Do we really need this? uint16_t year() const { return 2000 + yOff; } uint8_t month() const { return m; } uint8_t day() const { return d; } uint8_t hour() const { return hh; } uint8_t minute() const { return mm; } uint8_t second() const { return ss; } uint8_t dayOfWeek() const; void SetTime(const char time); void SetDate(const char date); void setyear(uint16_t year) { yOff = year - (year >= 2000 ? 2000 : 0); } void setmonth(uint8_t month) { m = month; } void setday(uint8_t day) { d = day; } void sethour(uint8_t hour) { hh = hour % 24; } void setminute(uint8_t minute) { mm = minute % 60; } void setsecond(uint8_t second) { ss = second % 60; } // 32-bit UNIX timestamp // An uint32_t should be able to store up to 2106, // which is beyond most chip's upper bound 2099 void setunixtime(uint32_t t); uint32_t unixtime() const; bool operator==(const DateTime& date) const; bool operator==(const char sdate) const; bool operator!=(const DateTime& date) const; bool operator!=(const char sdate) const; bool operator<(const DateTime& date) const; bool operator>(const DateTime& date) const; bool operator<=(const DateTime& date) const; bool operator>=(const DateTime& date) const;

DateTime operator+(uint32_t t) const;
DateTime operator+(const TimeDelta& delta) const;
DateTime operator-(uint32_t t) const;
DateTime operator-(const TimeDelta& delta) const;
TimeDelta operator-(const DateTime& date) const;

DateTime& operator+=(uint32_t t);
DateTime& operator+=(const TimeDelta& delta);
DateTime& operator-=(uint32_t t);
DateTime& operator-=(const TimeDelta& delta);

protected: uint8_t yOff, m, d, hh, mm, ss; }; `

JChristensen commented 2 years ago

There are at least a couple libraries with that name. I can't guess which it is. Please provide a link.

australopitheque commented 2 years ago

https://github.com/NeiroNx/RTCLib?utm_source=platformio&utm_medium=piohome

now() https://github.com/adafruit/RTClib/blob/master/src/RTC_DS3231.cpp for DateTime: https://github.com/adafruit/RTClib/blob/master/src/RTClib.h

JChristensen commented 2 years ago

Which library are you using? Both define classes named DateTime but they are different.

australopitheque commented 2 years ago

I use the adafruit one for myRTC.now() i replaced it with myRTC.get();

JChristensen commented 2 years ago

If you're just wanting Unix time, then that should work. Example sketch below. If you're wanting the DateTime class replicated in my library, then I don't think that will happen. If you're wanting something else, then let me know what it is.

#include <DS3232RTC.h>      // https://github.com/JChristensen/DS3232RTC

DS3232RTC myRTC;

void setup()
{
    Serial.begin(115200);
    myRTC.begin();
}

void loop()
{
    time_t now = myRTC.get();
    Serial.print(year(now), DEC);
    Serial.print('/');
    Serial.print(month(now), DEC);
    Serial.print('/');
    Serial.print(day(now), DEC);
    Serial.print(" (");
    Serial.print(dayShortStr(weekday(now)));
    Serial.print(") ");
    Serial.print(hour(now), DEC);
    Serial.print(':');
    Serial.print(minute(now), DEC);
    Serial.print(':');
    Serial.print(second(now), DEC);
    Serial.println();
    delay(3000);
}
australopitheque commented 2 years ago

je vous remercie beaucoup thanks you very much

JChristensen commented 2 years ago

Glad to have helped. Best regards from the USA!