String SparkTime::ISODateString(uint32_t tnow) { String ISOString; ISOString += yearString(tnow); ISOString += "-"; ISOString += monthString(tnow); ISOString += "-"; ISOString += dayString(tnow); ISOString += "T"; ISOString += hourString(tnow); ISOString += ":"; ISOString += minuteString(tnow); ISOString += ":"; ISOString += secondString(tnow); int32_t offset = timeZoneDSTOffset(tnow)/3600L; // Guard against timezone problems if (offset>-24 && offset<24) { if (offset < 0) { ISOString = ISOString + "-" + _digits[-offset] + "00"; } else { ISOString = ISOString + "+" + _digits[offset] + "00"; } } return ISOString; }
uint32_t SparkTime::year(uint32_t tnow) { uint32_t dayNum = (tnow+timeZoneDSTOffset(tnow)-SPARKTIMEBASESTART)/SPARKTIMESECPERDAY; uint32_t tempYear = SPARKTIMEBASEYEAR; while(dayNum >= YEARSIZE(tempYear)) { dayNum -= YEARSIZE(tempYear); tempYear++; } return tempYear; }
uint8_t SparkTime::day(uint32_t tnow) { uint32_t dayNum = (tnow+timeZoneDSTOffset(tnow)-SPARKTIMEBASESTART)/SPARKTIMESECPERDAY; uint32_t tempYear = SPARKTIMEBASEYEAR; uint8_t tempMonth = 0; while(dayNum >= YEARSIZE(tempYear)) { dayNum -= YEARSIZE(tempYear); tempYear++; } while(dayNum >= _monthLength[LEAPYEAR(tempYear)][tempMonth]) { dayNum -= _monthLength[LEAPYEAR(tempYear)][tempMonth]; tempMonth++; } dayNum++; // correct for zero-base return (uint8_t)dayNum; }
uint8_t SparkTime::month(uint32_t tnow) { uint32_t dayNum = (tnow+timeZoneDSTOffset(tnow)-SPARKTIMEBASESTART)/SPARKTIMESECPERDAY; uint32_t tempYear = SPARKTIMEBASEYEAR; uint8_t tempMonth = 0; while(dayNum >= YEARSIZE(tempYear)) { dayNum -= YEARSIZE(tempYear); tempYear++; } while(dayNum >= _monthLength[LEAPYEAR(tempYear)][tempMonth]) { dayNum -= _monthLength[LEAPYEAR(tempYear)][tempMonth]; tempMonth++; } tempMonth++; return tempMonth; }
uint8_t SparkTime::dayOfWeek(uint32_t tnow) { uint32_t dayNum = (tnow + timeZoneDSTOffset(tnow)-SPARKTIMEEPOCHSTART)/SPARKTIMESECPERDAY; //Unix epoch day 0 was a thursday return ((dayNum+4)%7); }
uint8_t SparkTime::second(uint32_t tnow) { return ((tnow+timeZoneDSTOffset(tnow)) % 60); }
uint8_t SparkTime::minute(uint32_t tnow) { uint8_t hTemp = ((tnow+timeZoneDSTOffset(tnow)) % 3600UL)/60UL; return hTemp; // return (((tnow+timeZoneDSTOffset(tnow)) % 3600) / 60); }
uint8_t SparkTime::hour(uint32_t tnow) { uint8_t hTemp = ((tnow+timeZoneDSTOffset(tnow)) % 86400UL)/3600UL; return hTemp; }
uint8_t SparkTime::minute(uint32_t tnow) { return (((tnow+timeZoneDSTOffset(tnow)) % 3600) / 60); }