The two given programs are pasted here Dateh Date class de
Solution
Longdate.h:
----------------
#ifndef Date_h
#define Date_h
#include \"date.h\"
const char monthNames[] PROGMEM = \"Error\\0January\\0February\\0March\\0April\\0May\\0June\\0July\\0August\\0September\\0October\\0November\\0December\";
const byte monthNameIndex[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const char monthNamesShort[] PROGMEM = \"Err\\0Jan\\0Feb\\0Mar\\0Apr\\0May\\0Jun\\0Jul\\0Aug\\0Sep\\0Oct\\0Nov\\0Dec\";
const char dayNames[] PROGMEM = \"Error\\0Sunday\\0Monday\\0Tuesday\\0Wednesday\\0Thursday\\0Friday\\0Saturday\";
const byte dayNameIndex[] = {0, 3, 9, 13, 20, 26, 29 };
const char dayNamesShort[] PROGMEM = \"Err\\0Sun\\0Mon\\0Tue\\0Wed\\0Thu\\0Fri\\0Sat\";
class Longdate
{
private: //private decleration
int _year, _month, _day, _hour, _minute, _second, _millisec;
int parse(char number);
int parse(char* number, int characters);
String& getProgMemString(const char *progMemString, byte index);
public: //public decleration
enum Period { Year, Month, Day, Hour, Minute, Second, millisec };
enum DayOfWeek { Error, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
enum TimeSource { Compiler, NTP };
Longdate(); //LongDate function
Longdate(char* date, char* time, Longdate::TimeSource source);
Longdate(int year, int month, int day, int hour, int minute, int second, int millisec);
int year() const;
int month() const;
int day() const;
int hour() const;
int minute() const;
int second() const;
int millisec() const;
int hourTens() const;
int hourUnits() const;
int minuteTens() const;
int minuteUnits() const;
void add(int interval, Period period);
int daysInMonth();
byte monthFromString(char* string);
unsigned long totalmillisecs() const;
Longdate::DayOfWeek dayOfWeek() const;
Longdate toLocal();
boolean isLeapYear() const;
boolean isApproximatelyEqualTo(const Longdate &other) const;
boolean isEarlierThan(const Longdate &other) const;
boolean isEqualTo(const Longdate &other) const;
static void getDaylightSavingsDates(Longdate forDate, Longdate &utcStartDate, Longdate &utcEndDate);
boolean operator == (const Longdate &other) const;
boolean operator < (const Longdate &other) const;
boolean operator <= (const Longdate &other) const;
boolean operator > (const Longdate &other) const;
boolean operator >= (const Longdate &other) const;
String& toString();
String& monthToString();
String& monthToString(int month);
String& monthToShortString();
String& monthToShortString(int month);
String& dayOfWeekToString();
String& dayOfWeekToString(DayOfWeek day);
String& dayOfWeekToShortString();
String& dayOfWeekToShortString(DayOfWeek day);
};
#endif
LongDate.cpp:
#include \"LongDate.h\"
LongDate origin();
LongDate::LongDate() { //LongDate function
_year = 2000;
_month = 1;
_day = 1;
_hour = 0;
_minute = 0;
_second = 0;
_millisec = 0;
}
LongDate::LongDate(int year, int month, int day, int hour, int minute, int second, int millisec) { //Decleration part
_year = year;
_month = month;
_day = day;
_hour = hour;
_minute = minute;
_second = second;
_millisec = millisec;
}
LongDate::LongDate(char* date, char* time, LongDate::TimeSource source) {
switch (source) { //switch cases
case Compiler:
_year = parse((date + 7), 4);
_month = monthFromString(date);
_day = parse((date + 4), 2);
_hour = parse(time, 2);
_minute = parse(time + 3, 2);
_second = parse(time + 6, 2);
_millisec = 0;
break;
case NTP:
_year = 2000 + parse(date + 4, 2);
_month = parse(date + 2, 2);
_day = parse(date, 2);
_hour = parse(time, 2);
_minute = parse(time + 2, 2);
_second = parse(time + 4, 2);
_millisec = parse(time + 7, 3);
break;
}
}
byte LongDate::monthFromString(char* string) { // Using monthFromString() here
switch (*string) { //switch cases
case \'A\':
return (*(++string) == \'p\') ? 4 : 8;
break;
case \'D\':
return 12;
break;
case \'F\':
return 2;
break;
case \'J\':
return (*(++string) == \'a\') ? 1 : ((*(++string) == \'l\') ? 7 : 6); //case J
return 0;
break;
case \'M\':
return (*(string + 2) == \'r\') ? 3 : 5;
break;
case \'N\':
return 11;
break;
case \'O\':
return 10;
break;
case \'S\':
return 9;
break;
}
}
int LongDate::year() const {
return _year;
}
int LongDate::month() const {
return _month;
}
int LongDate::day() const {
return _day;
}
int LongDate::hour() const {
return _hour;
}
int LongDate::minute() const {
return _minute;
}
int LongDate::second() const {
return _second;
}
int LongDate::millisec() const {
return _millisec;
}
int LongDate::hourTens() const {
return _hour/10;
}
int LongDate::hourUnits() const {
return _hour%10;
}
int LongDate::minuteTens() const {
return _minute/10;
}
int LongDate::minuteUnits() const {
return _minute%10;
}
void LongDate::add(int interval, Period period) {
// This Should accept a positive or negative interval for any period.
if (period == millisec) {
int magnitude = this->millisec() + interval;
interval = magnitude / 1000;
_millisec = magnitude % 1000;
if (this->millisec() < 0) {
_millisec += 1000;
interval -= 1;
}
if (interval != 0) period = Second;
}
if (period == Second) {
int magnitude = this->second() + interval;
interval = magnitude / 60;
_second = magnitude % 60;
if (this->second() < 0) {
_second += 60;
interval -= 1;
}
if (interval != 0) period = Minute;
}
if (period == Minute) {
int magnitude = this->minute() + interval;
interval = magnitude / 60;
_minute = magnitude % 60;
if (this->minute() < 0) {
_minute += 60;
interval -= 1;
}
if (interval != 0) period = Hour;
}
if (period == Hour) {
int magnitude = this->hour() + interval;
interval = magnitude / 24;
_hour = magnitude % 24;
if (this->hour() < 0) {
_hour += 24;
interval -= 1;
}
if (interval != 0) period = Day;
}
if (period == Day) { //If period is same as Day
while(interval) {
int magnitude = this->day() + interval;
int daysInMonth = this->daysInMonth(); //daysInMonth() used
if (magnitude > daysInMonth) {
_day = 1;
add(1, LongDate::Month);
interval = magnitude - daysInMonth - 1;
} else if (magnitude < 1) {
int tempDay = day();
_day = 1;
add(-1, LongDate::Month);
_day = this->daysInMonth();
interval = interval + tempDay; //new interval value
} else {
_day += interval;
interval = 0;
}
}
}
if (period == Month) { //If period is same as Month
int magnitude = this->month() + interval; // 1 -1 = 0
interval = magnitude / 12;
_month = magnitude % 12;
if (!_month) { //If month is not = 12
_month = 12;
interval--;
}
if (interval != 0) period = Year;
}
if (period == Year) _year += interval;
}
int LongDate::daysInMonth() { //Using daysInMonth() function
switch (month()) { //switch cases with month()
case 1:
return 31;
break;
case 2:
return isLeapYear() ? 29 : 28; // isLeapYear() function
break;
case 3:
return 31;
break;
case 4:
return 30;
break;
case 5:
return 31;
break;
case 6:
return 30;
break;
case 7: //case 7
return 31;
break;
case 8:
return 31;
break;
case 9:
return 30;
break;
case 10:
return 31;
break;
case 11:
return 30;
break;
case 12:
return 31;
break;
}
}
int LongDate::parse(char* number, int characters) {
int result = 0;
int index = characters;
for (int index = 0; index < characters; index++) {
result *= 10;
result += parse(*(number + index)); //This will fetch the result
}
return result;
}
int LongDate::parse(char number) { //Parse
if (number == \' \') number = \'0\';
return number - 48;
}
boolean LongDate::isEarlierThan(const LongDate &other) const {
if (this->year() < other.year()) return true;
if (this->year() > other.year()) return false;
if (this->month() < other.month()) return true;
if (this->month() > other.month()) return false;
if (this->day() < other.day()) return true;
if (this->day() > other.day()) return false;
if (this->hour() < other.hour()) return true;
if (this->hour() > other.hour()) return false;
if (this->minute() < other.minute()) return true;
if (this->minute() > other.minute()) return false;
if (this->second() < other.second()) return true;
if (this->second() > other.second()) return false;
if (this->millisec() < other.millisec()) return true;
if (this->millisec() > other.millisec()) return false;
return false;
}
boolean LongDate::isEqualTo(const LongDate &other) const {
return (this->year() == other.year() &&
this->month() == other.month() &&
this->day() == other.day() &&
this->hour() == other.hour() &&
this->minute() == other.minute() &&
this->second() == other.second() &&
this->millisec() == other.millisec());
}
boolean LongDate::operator == (const LongDate &other) const {
return this->isEqualTo(other);
}
boolean LongDate::operator < (const LongDate &other) const {
return this->isEarlierThan(other);
}
boolean LongDate::operator <= (const LongDate &other) const {
return this->isEarlierThan(other) || this->isEqualTo(other);
}
boolean LongDate::operator > (const LongDate &other) const {
return other.isEarlierThan(*this);
}
boolean LongDate::operator >= (const LongDate &other) const {
return other.isEarlierThan(*this) || other.isEqualTo(*this);
}
LongDate LongDate::toLocal() {
LongDate local(*this);
LongDate dstStart;
LongDate dstEnd;
LongDate::getDaylightSavingsDates(*this, dstStart, dstEnd);
if (this->isEarlierThan(dstStart) && (dstEnd.isEarlierThan(*this) || (dstEnd.isEqualTo(*this))))
local.add(10, LongDate::Hour);
else
local.add(11, LongDate::Hour);
return local;
}
boolean LongDate::isLeapYear() const {
int year = this->year();
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}
void LongDate::getDaylightSavingsDates(LongDate forDate, LongDate &utcStartDate, LongDate &utcEndDate) { //getDaylightSavingsDates() function
int year = forDate.year();
utcStartDate = LongDate(year, 10, 1, 16, 0, 0, 0);
utcEndDate = LongDate(year, 4, 1, 16, 0, 0, 0);
if (utcStartDate.dayOfWeek() == 0)
utcStartDate.add(-1, LongDate::Day);
else
utcStartDate.add(6 - utcStartDate.dayOfWeek(), LongDate::Day);
if (utcEndDate.dayOfWeek() == 0)
utcEndDate.add(-1, LongDate::Day);
else
utcEndDate.add(6 - utcEndDate.dayOfWeek(), LongDate::Day);
}
LongDate::DayOfWeek LongDate::dayOfWeek() const {
// To calculate only for the 2000-2099 century
int century = 6;
int monthFactors[12] = { 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; //MonthsFactors declared here
int monthFactorsLeap[12] = { 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; //MonthsFactorsLeap declared here
int twoDigitYear = this->year()%100;
int yearOverFour = twoDigitYear/4;
int month = this->isLeapYear() ? monthFactorsLeap[this->month() - 1] : monthFactors[this->month() - 1];
int sum = century + twoDigitYear + yearOverFour + month + this->day();
return (LongDate::DayOfWeek)(sum % 7 + 1);
}
boolean LongDate::isApproximatelyEqualTo(const LongDate &other) const {
LongDate alpha;
LongDate alphaOne;
LongDate omega;
if (this->isEarlierThan(other)) {
alpha = (*this);
omega = other; //if omega=other
} else {
alpha = other; //omega = other
omega = (*this);
}
alphaOne = alpha; // When alphaOne = alpha
alphaOne.add(1, LongDate::Day);
if (((alpha.year() != omega.year())
|| (alpha.month() != omega.month())
|| (alpha.day() != omega.day())) // not same day
&& ((alphaOne.year() != omega.year())
|| (alphaOne.month() != omega.month())
|| (alphaOne.day() != omega.day()))) // not consecutive days
return false;
if ((omega.totalmillisecs() - alpha.totalmillisecs()) > 2000) return false;
return true;
}
unsigned long LongDate::totalmillisecs() const {
unsigned long result = 0;
result += millisec();
result += second() * 1000UL; //result in sec
result += minute() * 60UL * 1000; //result in mints
result += hour() * 60UL * 60 * 1000; //result in hrs
return result; //result will be fetched here
}
String& LongDate::toString() {
static String* output;
if (output) delete(output); //output deleted here
output = new String(); //new string accepted here
*output += dayOfWeekToShortString();
*output += \' \';
if (_day < 10) *output += \'0\'; //if day < 10
*output += _day;
*output += \' \';
*output += monthToShortString();
*output += \' \';
*output += _year;
*output += \' \';
if (_hour < 10) *output += \'0\';
*output += _hour;
*output += \':\';
if (_minute < 10) *output += \'0\';
*output += _minute;
*output += \':\';
if (_second < 10) *output += \'0\';
*output += _second;
*output += \'.\';
if (_millisec < 100) *output += \'0\';
if (_millisec < 10) *output += \'0\';
*output += _millisec;
return *output;
}
String& LongDate::monthToString() {
return monthToString(_month);
}
String& LongDate::monthToString(int month) {
return getProgMemString(monthNames, monthNameIndex[month]);
}
String& LongDate::monthToShortString() {
return monthToShortString(_month);
}
String& LongDate::monthToShortString(int month) {
return getProgMemString(monthNamesShort, (month * 4));
}
String& LongDate::dayOfWeekToString() {
return dayOfWeekToString(dayOfWeek());
}
String& LongDate::dayOfWeekToString(DayOfWeek day) {
return getProgMemString(dayNames, dayNameIndex[day]);
}
String& LongDate::dayOfWeekToShortString() {
return dayOfWeekToShortString(dayOfWeek());
}
String& LongDate::dayOfWeekToShortString(DayOfWeek day) {
return getProgMemString(dayNamesShort, (day * 4));
}
String& LongDate::getProgMemString(const char *progArray, byte index) { //getProgMemString()
char ramBuffer[10];
char* progMemCString = (char*)(progArray + (index));
strcpy_P(ramBuffer, progMemCString); // strcpy function used here
static String* output;
delete(output);
output = new String(ramBuffer);
return *output; //output fetched
}













