Hi,
I have written this Date/Time function to provide some date handling and I am looking to see if any knowledgable person can give me any advice on improving this to improve the memory footprint or coding.
The date is collected from a RTC (DS3231) and then formatted to several types depending on the supplied Datatype variable.
Thank you
tDate = getDateTime("SDDate");
String getDateTime(String DateType) {
Serial.println(DateType);
String TSYear;
String TSMonth;
String TSDayName;
String TSDay;
String TSHour;
String TSMinute;
String TSSecond;
String TSDate;
byte zero = 0x00;
//delay(1000);
Wire.beginTransmission(DS3231_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESS, 7);
// TSDayName 1-7 -> Monday to Sunday
TSSecond = ChangeDate(bcdToDec(Wire.read() & 0x7f));
TSMinute = String( ChangeDate(bcdToDec(Wire.read())));
TSHour = String( ChangeDate(bcdToDec(Wire.read() & 0x3f)));
TSDayName = String( ChangeDate(bcdToDec(Wire.read())));
TSDay = String( ChangeDate(bcdToDec(Wire.read())));
TSMonth = String( ChangeDate(bcdToDec(Wire.read())));
TSYear = "20" + String( ChangeDate(bcdToDec(Wire.read())));
TSDate = TSYear + TSMonth + TSDay + TSHour + TSMinute + TSSecond;
if (DateType == "Date") {
return TSYear + TSMonth + TSDay;
} else if (DateType == "Time") {
return TSHour + TSMinute + TSSecond;
} else if (DateType == "Year") {
return TSYear;
} else if (DateType == "Month") {
return TSMonth;
} else if (DateType == "Day") {
return TSDay;
} else if (DateType == "SDHeader") {
return TSYear + "_" + TSMonth + "_" + TSDay + " @ " + TSHour + ":" + TSMinute + ":" + TSSecond;
} else if (DateType == "SDFilename") {
return TSYear + "_" + TSMonth + "_" + TSDay + "-" + TSHour + "_" + TSMinute + "_" + TSSecond;
} else if (DateType == "SDDate") {
return TSYear + TSMonth + TSDay;
} else {
// DateTimestamp
return TSYear + TSMonth + TSDay + TSHour + TSMinute + TSSecond;
}
}