NTPClient icon indicating copy to clipboard operation
NTPClient copied to clipboard

NTPC: how to obtain "getHours" in 24 hrs format?

Open xkosm14 opened this issue 5 years ago • 3 comments

Please let me know if is it possible to get NTC timeClient.getHours() in 24hrs format? Always getting 12 hr format.

My piece of Arduino code: Updating RTC chip with NTF time, if current time is 11PM then i got "11" but need "23": rtc.adjust(DateTime(YEAR, MONTH, DAY, timeClient.getHours(), timeClient.getMinutes(), SE));

BTW I know there is a way to use different approach e.g. epoch time or parse getFormattedTime etc...

xkosm14 avatar Dec 28 '19 22:12 xkosm14

try:

String getHours24(){ unsigned long hours = timeClient.getHours(); String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);

return String( hoursStr ); }

serkantueten avatar Jan 20 '20 13:01 serkantueten

@xkosm14 use dedicated direct-call functions or use code :-

String getHours24(){ unsigned long hours = timeClient.getHours(); String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);

return String( hoursStr ); }

thekunalsaini avatar Mar 20 '20 07:03 thekunalsaini

In NTPClient.cpp the following snippet-

int NTPClient::getHours() const {
  return ((this->getEpochTime()  % 86400L) / 3600);
}

Should ideally be able to return 23 as well.

Eg: for EPOCH = 1585436874‬ the return value should be 23.

Any clue on this? Are we talking about some other getHour() function?

MisterAwesome23 avatar Mar 28 '20 23:03 MisterAwesome23