Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the functionallity to get the date #190

Open
Neoxxgr opened this issue May 6, 2023 · 1 comment · May be fixed by #94, #207 or #161
Open

Add the functionallity to get the date #190

Neoxxgr opened this issue May 6, 2023 · 1 comment · May be fixed by #94, #207 or #161
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement

Comments

@Neoxxgr
Copy link

Neoxxgr commented May 6, 2023

With the code below you can add the function to get the date from the rawTime
this is a test code and i have not tested it very well
NTPClient.h

/**
 * @return Date formatted like `dd/mm/yyyy`
*/
String getFormattedDate() const;

NTPClient.cpp

String NTPClient::getFormattedDate() const {
   unsigned long rawTime = this->getEpochTime();
  // Define constants for seconds in a day, year, and leap year
  #define SEC_IN_DAY 86400
  #define SEC_IN_YEAR 31536000
  #define SEC_IN_LEAP_YEAR 31622400

  // Define an array for the number of days in each month
  int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  // Initialize the date to January 1st, 1970
  unsigned long years = 1970;
  unsigned long months = 1;
  unsigned long days = 1;

  // Add the number of years elapsed since the epoch
  while (rawTime >= SEC_IN_YEAR) 
  {
    if ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) 
	{
      if (rawTime >= SEC_IN_LEAP_YEAR) 
	  {
        rawTime -= SEC_IN_LEAP_YEAR;
        years++;
      } else 
	  {
        break; // not enough seconds for a leap year
      }
    } else 
	{
      rawTime -= SEC_IN_YEAR;
      years++;
    }
  }

  // Add the number of months elapsed in the current year
  while (rawTime >= SEC_IN_DAY) 
  {
    if (rawTime >= days_in_month[months - 1] * SEC_IN_DAY) 
	{
      rawTime -= days_in_month[months - 1] * SEC_IN_DAY;
      months++;
    } else 
	{
      break; // not enough seconds for a month
    }
    // Adjust for February in a leap year
    if (months == 2 && ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0))) 
	{
      rawTime -= SEC_IN_DAY;
    }  

    // Adjust for year boundary
    if (months >12) {
      months = months -12;
      years++;
    }
  }
	
	// Add the number of days elapsed in the current month 
	while (rawTime >= SEC_IN_DAY) { rawTime -= SEC_IN_DAY; days++; }

	// Convert the date components to strings with leading zeros if needed  
	String yearStr = String(years);
	String monthStr = months <10 ? "0" + String(months) : String(months);
	String dayStr = days <10 ? "0" + String(days) : String(days);

	// Return the date in dd/mm/yyyy format 
	return dayStr + "/" + monthStr + "/" + yearStr; 
	}
@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels May 15, 2023
@per1234
Copy link
Contributor

per1234 commented May 15, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
2 participants