Skip to content

get year, month, day #36

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

Closed
lutfisan opened this issue Jul 20, 2017 · 19 comments
Closed

get year, month, day #36

lutfisan opened this issue Jul 20, 2017 · 19 comments
Labels
conclusion: resolved Issue was resolved type: support OT: Request for help using the project

Comments

@lutfisan
Copy link

how to get year, month, and day?

@bvanelli
Copy link

bvanelli commented Jul 22, 2017

I added a function to do exactly that, the only dependency being #include <time.h>.

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:

2017-07-22 19:20:25

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.

@lutfisan
Copy link
Author

lutfisan commented Jul 22, 2017

it's work perfect, i think you should add it to your code

you might add this

int NTPClient::getYear() {
  time_t rawtime = this->getEpochTime();
  struct tm * ti;
  ti = localtime (&rawtime);
  int year = ti->tm_year + 1900;
  
  return year;
}

int NTPClient::getMonth() {
  time_t rawtime = this->getEpochTime();
  struct tm * ti;
  ti = localtime (&rawtime);
  int month = (ti->tm_mon + 1) < 10 ? 0 + (ti->tm_mon + 1) : (ti->tm_mon + 1);
  
  return month;
}

int NTPClient::getDate() {
  time_t rawtime = this->getEpochTime();
  struct tm * ti;
  ti = localtime (&rawtime);
  int month = (ti->tm_mday) < 10 ? 0 + (ti->tm_mday) : (ti->tm_mday);
  
  return month;
}

@Testato
Copy link
Contributor

Testato commented Jul 23, 2017

#3

@bvanelli
Copy link

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 getFormattedTime() runs in about 100 microseconds, whereas getFullFormattedTime() is about 450 microseconds. Not very good, but not bad either.

@pwrsoft
Copy link

pwrsoft commented Sep 19, 2017

@sandeepmistry Could you please add the suggested changed to the library?

@Gund77
Copy link

Gund77 commented Oct 4, 2017

Hi Everyone, i had a problem while adding the new public method to the NTPclient.cpp and NTPClient.h
once i added the code, the compiler gets error when processing this file.

I added the functions to the .cpp file and the header into the public section of .h file

How can i fix it?

thanks

NTPClient.zip

@bvanelli
Copy link

bvanelli commented Oct 4, 2017

@Gund77 , as I stated, my workaround makes use of the C standard time library.

You can fix it by adding #include <time.h> in your header file. Your NTPClient.h's first 6 lines should look like this:

#pragma once

#include "Arduino.h"
#include <time.h>

#include <Udp.h>

@bvanelli
Copy link

bvanelli commented Oct 4, 2017

For anyone that still comes to this topic seeking for help, please use the fork implementation on Github taranais/NTPClient. He implements the function getFormattedDate() that returns the time formatted in ISO 8601.

@Gund77
Copy link

Gund77 commented Oct 4, 2017

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.
Anyway, switching the inclusions position the problem persist. i got error again. It might be a sintax error, but i cant find where... can you please send me your NTP Client.h and .cpp files?

thank you so much.

@bvanelli
Copy link

bvanelli commented Oct 4, 2017

@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();

Working version of NTPClient.zip

@thiagoarreguy
Copy link

thiagoarreguy commented Jan 31, 2018

the function getFullFormattedTime ir returnning time formatted like YYYY:MM:DD hh:mm:ss

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.

@icandura
Copy link

@thiagoarreguy I consider that replacing an NTP server is a good choice.

@Moerbius
Copy link

Moerbius commented Oct 1, 2018

Hi,

Does it take into account the leap years?

@paysan18
Copy link

paysan18 commented Nov 9, 2018

Thanks! I was looking for a feature like this!
Please add this to the main file. When will it be standard available?
In the mean time I got it working.....

@kaefert
Copy link

kaefert commented Nov 17, 2018

Since I didn't want to modify the library, I adapted the code snippet to work inside main.cpp:

// https://github.com/arduino-libraries/NTPClient/issues/36
String getTimeStampString() {
   time_t rawtime = timeClient.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;
}

@aentinger
Copy link
Contributor

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.

@jimmisitepu88
Copy link

Hi Everyone, i had a problem while adding the new public method to the NTPclient.cpp and NTPClient.h
once i added the code, the compiler gets error when processing this file.

I added the functions to the .cpp file and the header into the public section of .h file

How can i fix it?

thanks

NTPClient.zip

I have fix your code, you can download this file
NTPClient.tar.gz

@id1402
Copy link

id1402 commented Mar 16, 2023

Hello.
I tried NTPClient.tar.gz made by jimmisitepu88.

Unfortunately timeClient.getFormattedTime() results in wrong date:
Yesterday: 00:21:53.687 -> 2053-03-15
Should be 2023-03-15.

Today: 12:56:50.757 -> 2053-03-15
Should be 2023-03-16.

The time shown left is local time UTC+1, when timeClient.getFormattedTime() is called.
It would be great if some brave soul would fix this.

@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself type: support OT: Request for help using the project conclusion: resolved Issue was resolved and removed type: enhancement Proposed improvement topic: code Related to content of the project itself labels May 12, 2023
@iAvoe
Copy link

iAvoe commented Nov 10, 2024

For those who is developing a IoT project right now without replacing NTPClient, and without much C++ background, just put this in between the #define s and void setup(), or it should be similar to this:

const long myUTCTimeOffset = <UTC zone code> + 3600;
WiFiUDP ntpUDP;
const char* myNTPServer = "time.google.com";

/**
 * Adding support to year, month, day from NTPClient
 * Credit: https://github.com/arduino-libraries/NTPClient/issues/36
 * Credit: https://www.geeksforgeeks.org/inheritance-in-c/
 */
class ExtendedNTPClient : public NTPClient {
public:
    ExtendedNTPClient(WiFiUDP &udp, const char *server, long timeOffset)
        : NTPClient(udp, server, timeOffset) {}

    int getYear() {
        time_t rawtime = this->getEpochTime();
        struct tm *ti;
        ti = localtime(&rawtime);
        int year = ti->tm_year + 1900;
        return year;
    }

    int getMonth() {
        time_t rawtime = this->getEpochTime();
        struct tm *ti;
        ti = localtime(&rawtime);
        int month = ti->tm_mon + 1; // tm_mon starts from 0
        return month;
    }

    int getDay() {
        time_t rawtime = this->getEpochTime();
        struct tm *ti;
        ti = localtime(&rawtime);
        int day = ti->tm_mday;
        return day;
    }
};
ExtendedNTPClient timeClient(myNTPUDP, myNTPServer, myUTCTimeOffset);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: resolved Issue was resolved type: support OT: Request for help using the project
Projects
None yet
Development

No branches or pull requests