Skip to content

Instantly share code, notes, and snippets.

@Marzogh
Created July 22, 2015 21:21
Show Gist options
  • Save Marzogh/f102c04bbfeec500aff4 to your computer and use it in GitHub Desktop.
Save Marzogh/f102c04bbfeec500aff4 to your computer and use it in GitHub Desktop.
#include <TinyGPS++.h> // For gps
#include <SoftwareSerial.h> // For communicating with the gps
struct dateTime { // Stores date and time for DateTime calculations
int Year;
int Month;
int Date;
};
dateTime current;
dateTime past;
dateTime timeElapsed;
void timeSince()
{
#ifdef SERIALECHO
char Date[50];
sprintf(Date, "The current date is: %d/%d/%d", current.Date, current.Month, current.Year);
Serial.println(Date);
sprintf(Date, "The previous date is: %d/%d/%d", past.Date, past.Month, past.Year);
Serial.println(Date);
#endif
//Calculate years passed
timeElapsed.Year = current.Year - past.Year;
//Calculate the months passed and correct the number of years if required
if (current.Month < past.Month) {
#ifdef SERIALECHO
Serial.println(F("Current Month < Past Month"));
#endif
timeElapsed.Year -= 1;
timeElapsed.Month = 12 - (abs(current.Month - past.Month));
}
else
timeElapsed.Month = (current.Month - past.Month);
//Calculate the days passed and correct the number of months
if (current.Date < past.Date) {
#ifdef SERIALECHO
Serial.println(F("Current Date < Past Date"));
#endif
timeElapsed.Month -= 1;
timeElapsed.Date = (calcNoOfDays(months, years)) - (abs(current.Date - past.Date));
}
else
timeElapsed.Date = (current.Date - past.Date);
#ifdef SERIALECHO
sprintf(Date, "The time elapsed is: %d days %d months %d years", timeElapsed.Date, timeElapsed.Month, timeElapsed.Year);
Serial.println(Date);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment