Converting Army Time to Standard 12 Hours

Hello I’m working on an OLED clock display and want to convert the hours to standard 12 hour time, its not a huge problem and I could probably live with it but i would rather have it set this way as I am more familiar with it.

The code:

#include <ESP8266WiFi.h>
#include <time.h>
#include <U8g2lib.h>
const char* ssid = “wifi”;
const char* password = “password”;
const char* NTP_SERVER = “0.north-america.pool.ntp.org”;
const char* TZ_INFO = “Time”; // enter your time zone (Timezone Abbreviations)
tm timeinfo;

time_t now;
long unsigned lastNTPtime;
unsigned long lastEntryTime;
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 13); //2.42 inch //2.42 inch
void setup()
{
u8g2.begin();
Serial.begin(115200);
Serial.println("\n\nNTP Time Test\n");
WiFi.begin(ssid, password);
Serial.print(“Connecting to network”);
int counter = 0;
while (WiFi.status() != WL_CONNECTED)
{
delay(200);
if (++counter > 100)
ESP.restart();
Serial.print( “.” );
}
Serial.println("\nWiFi connected\n\n");
configTime(0, 0, NTP_SERVER);
// See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region
setenv(“TZ”, TZ_INFO, 1);
if (getNTPtime(1000))
{
// wait up to 10sec to sync
}
else
{
Serial.println(“Time not set”);
ESP.restart();
}
showTime(&timeinfo);
lastNTPtime = time(&now);
lastEntryTime = millis();
}

uint8_t m = 24;

void loop()
{
getNTPtime(10);
showTime(&timeinfo);
delay(1000);
}
bool getNTPtime(int sec)
{
{
uint32_t start = millis();
do
{
time(&now);
localtime_r(&now, &timeinfo);
delay(10);
} while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900)));

if (timeinfo.tm_year <= (2016 - 1900))
return false; // the NTP call was not successful

Serial.print("Time Now: “);
Serial.println(now);
}
return true;
}
void showTime(tm *localTime)
{
//print to serial terminal
Serial.print(localTime->tm_mday);
Serial.print(’/’);
Serial.print(localTime->tm_mon + 1);
Serial.print(’/’);
Serial.print(localTime->tm_year - 100);
Serial.print(’-’);
Serial.print(localTime->tm_hour);
Serial.print(’:’);
Serial.print(localTime->tm_min);
Serial.print(’:’);
Serial.print(localTime->tm_sec);
Serial.print(” Day of Week ");
Serial.println(localTime->tm_wday);
Serial.println();
//display on OLED
char time_output[30];

sprintf(time_output, “%02d:%02d:%02d”, localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
char h_str[3];
char m_str[3];
char s_str[3];
strcpy(m_str, u8x8_u8toa(localTime->tm_min, 2)); /* convert m to a string with two digits /
strcpy(h_str, u8x8_u8toa(localTime->tm_hour, 2)); /
convert m to a string with two digits */
strcpy(s_str, u8x8_u8toa(localTime->tm_sec, 2));
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_maniac_tf);//u8g2_font_maniac_tf//u8g2_font_freedoomr25_tn
u8g2.drawStr(0,45,(h_str));
u8g2.drawStr(38,45,":");
u8g2.drawStr(47,45,m_str);
u8g2.drawStr(84, 45,":");
u8g2.drawStr(93, 45,s_str);
} while ( u8g2.nextPage() );

Serial.print(localTime->tm_hour);
Serial.print(’:’);
Serial.print(localTime->tm_min);

sprintf(time_output, “%02d:%02d:%02d”, localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
//u8x8.print(time_output);

//u8x8.setFont(u8x8_font_8x13B_1x2_f);
//u8x8.setCursor(4,4);
//sprintf(time_output, “%02d/%02d/%02d”, localTime->tm_mday, localTime->tm_mon + 1, localTime->tm_year - 100);
//u8x8.print(time_output);

//u8x8.setCursor(4,6);
//u8x8.print(getDOW(localTime->tm_wday));
}
char * getDOW(uint8_t tm_wday)
{
switch(tm_wday)
{
case 1:
return “Monday”;
break;
case 2:
return “Tuesday”;
break;
case 3:
return “Wednesday”;
break;
case 4:
return “Thursday”;
break;
case 5:
return “Friday”;
break;
case 6:
return “Saturday”;
break;
case 7:
return “Sunday”;
break;
default:
return “Error”;
break;
}
}

Please use code tags.

You mean UTC?
If hour > 12, subtract 12

Please edit your post to add code tags, as described in "How to use this forum".

If You have a 24 hour clock look at this:

if ( hours > 12 )
(
   hours -= 12;
   PM = true;
   AM = false;
}
else
{
   PM = false;
   AM = ftrue;
}

AM = ftrue; :o

Railroader:
If You have a 24 hour clock look at this:

if ( hours > 12 )

(
  hours -= 12;
  PM = true;
  AM = false;
}
else
{
  PM = false;
  AM = ftrue;
}

This code will incorrectly display 12noon as AM.

Corrected version:

  if (hours > 11) {
    pm = true;
    if (hours > 12) {
      hours -= 12;
    }
  }
  else {
    pm = false;
  }

TheMemberFormerlyKnownAsAWOL:
AM = ftrue; :o

Quick, untested and faulty… Another usual mistake is the spelling of "not true", flase…..