I need to keep hold of times - not just set and get the current date, so I'm creating a tmElements_t structure to keep hold of a date. When I do:
tmElements_t tm;
Serial.print("XX Current year is "); Serial.println(year());
tm.Year = year();
Serial.print("YY Current year is "); Serial.println(tm.Year);
I get:
XX Current year is 2014
YY Current year is 222
..this is b/c of a mis-match in data types:
int year();
vs:
typedef struct {
uint8_t Second;
uint8_t Minute;
uint8_t Hour;
uint8_t Wday; // day of week, sunday is day 1
uint8_t Day;
uint8_t Month;
uint8_t Year; // offset from 1970;
} tmElements_t, TimeElements, *tmElementsPtr_t;
I haven't used these libraries before, but if year() returns the year as an integer, like 2014, and tm.Year is a uint8_t (1 byte unsigned integer) then you need to convert in both directions:
tm.Year = year()-1970;
and to display the year from a tmElements_t structure, you would need:
int year = tm.Year + 1970;
The other poster's code was missing the first conversion for year) to a tmElements_t year.
I need to keep hold of times - not just set and get the current date, so I'm creating a tmElements_t structure to keep hold of a date. When I do:
tmElements_t tm;
Serial.print("XX Current year is "); Serial.println(year());
tm.Year = year();
Serial.print("YY Current year is "); Serial.println(tm.Year);
I get:
XX Current year is 2014
YY Current year is 222
..this is b/c of a mis-match in data types:
int year();
vs:
typedef struct {
uint8_t Second;
uint8_t Minute;
uint8_t Hour;
uint8_t Wday; // day of week, sunday is day 1
uint8_t Day;
uint8_t Month;
uint8_t Year; // offset from 1970;
} tmElements_t, TimeElements, *tmElementsPtr_t;
I originally had thought about the "from 1970" issue, but the delta I was seeing was 222 years, not 44, so I initially didn't think that was the issue (I should have known better!). I just tried the "add 1970/subtract 1970" solution DuncanC posted and it worked great.