-
Notifications
You must be signed in to change notification settings - Fork 384
get year, month, day #36
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I added a function to do exactly that, the only dependency being String NTPClient::getFullFormattedTime() {
time_t rawtime = this->getEpochTime();
struct tm * ti;
ti = localtime (&rawtime);
uint16_t year = ti->tm_year + 1900;
String yearStr = String(year);
uint8_t month = ti->tm_mon + 1;
String monthStr = month < 10 ? "0" + String(month) : String(month);
uint8_t day = ti->tm_mday;
String dayStr = day < 10 ? "0" + String(day) : String(day);
uint8_t hours = ti->tm_hour;
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
uint8_t minutes = ti->tm_min;
String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
uint8_t seconds = ti->tm_sec;
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
return yearStr + "-" + monthStr + "-" + dayStr + " " +
hoursStr + ":" + minuteStr + ":" + secondStr;
} The output seems right:
What you guys think? This should be added to the main code? Best Regards. EDIT: editted the function to use less memory and use the same Epoch time for every calculation. |
it's work perfect, i think you should add it to your code you might add this
|
I agree with @Testato that this is not the purpose of the library, however this conversion is pretty useful for time-stamping and it's pretty handy to have it working out of the box, without the need for other conversions. I also ran the benchmarks: the |
@sandeepmistry Could you please add the suggested changed to the library? |
Hi Everyone, i had a problem while adding the new public method to the NTPclient.cpp and NTPClient.h I added the functions to the .cpp file and the header into the public section of .h file How can i fix it? thanks |
@Gund77 , as I stated, my workaround makes use of the C standard time library. You can fix it by adding #pragma once
#include "Arduino.h"
#include <time.h>
#include <Udp.h> |
For anyone that still comes to this topic seeking for help, please use the fork implementation on Github taranais/NTPClient. He implements the function |
Sorry, My mistate. I included Time.h library as last in the list so the compiler do not find the declaration while parsing the NTP library. thank you so much. |
@Gund77 , it is a syntax error... You don't use the namespace when declaring a function inside the class declaration (header file). Instead of using: /**
* @return time formatted like `DD:MM:YYYY`
*/
String NTPClient::getFormattedDate() ;
/**
* @return time formatted like `DD:MM:YYYY hh:mm:ss`
*/
String NTPClient::getFullFormattedTime();
int NTPClient::getYear();
int NTPClient::getMonth();
int NTPClient::getDate(); You should declare them as: /**
* @return time formatted like `hh:mm:ss`
*/
String getFormattedTime();
/**
* @return time formatted like `DD:MM:YYYY`
*/
String getFormattedDate();
/**
* @return time formatted like `DD:MM:YYYY hh:mm:ss`
*/
String getFullFormattedTime();
int getYear();
int getMonth();
int getDate(); |
the function getFullFormattedTime ir returnning time formatted like and my output is in epoctime 1970-01-01 00:00:49 i dont understand why dont return the real date and time... i was debuging the code and the function forceUpdate return false by timeout. |
@thiagoarreguy I consider that replacing an NTP server is a good choice. |
Hi, Does it take into account the leap years? |
Thanks! I was looking for a feature like this! |
Since I didn't want to modify the library, I adapted the code snippet to work inside main.cpp:
|
I am closing this issue since there are solutions available as code snippets as well as in user-forks. It would be great if some brave soul would create a pull request so that we can add this functionality to this library as well. |
I have fix your code, you can download this file |
Hello. Unfortunately timeClient.getFormattedTime() results in wrong date: Today: The time shown left is local time UTC+1, when timeClient.getFormattedTime() is called. |
For those who is developing a IoT project right now without replacing NTPClient, and without much C++ background, just put this in between the
|
how to get year, month, and day?
The text was updated successfully, but these errors were encountered: