Skip to content

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

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
xkosm14 opened this issue Dec 28, 2019 · 4 comments
Closed

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

xkosm14 opened this issue Dec 28, 2019 · 4 comments
Assignees
Labels
conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@xkosm14
Copy link

xkosm14 commented Dec 28, 2019

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 xkosm14 changed the title NTCP: how to obtain "getHours" in 24 hrs format? NTPC: how to obtain "getHours" in 24 hrs format? Dec 28, 2019
@serkantueten
Copy link

serkantueten commented Jan 20, 2020

try:

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

  return String( hoursStr );
}

@thekunalsaini

This comment has been minimized.

@MisterAwesome23
Copy link

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?

@per1234 per1234 self-assigned this May 2, 2025
@per1234 per1234 added type: imperfection Perceived defect in any part of project topic: code Related to content of the project itself labels May 2, 2025
@per1234
Copy link
Contributor

per1234 commented May 2, 2025

As @MisterAwesome23 explained, the library's getHours() function does return the hour in 24 hour format. It did this even back in 2019. So either @xkosm14 was using a different library than the one developed in this repository, or else the conversion to 12 hour format was being done by the other code in their sketch (I guess it is from the RTClib library.

@per1234 per1234 closed this as not planned Won't fix, can't repro, duplicate, stale May 2, 2025
@per1234 per1234 added the conclusion: invalid Issue/PR not valid label May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

5 participants