Cannot set 12hour mode on DS3121 using RTClib

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
veditor78
 
Posts: 2
Joined: Sat Jul 20, 2019 9:46 pm

Cannot set 12hour mode on DS3121 using RTClib

Post by veditor78 »

I am working on a project that I want to include a clock on the display. I have the clock displaying in 24 hour mode, but I want 12hr with AM/PM.

I cannot for the life of me get it to switch to 12hr mode, it seems to be stuck in 24hr.

I've also tried to use other librarie's example code with the same result, it refuses to switch to 12hr.

Here is the relevant code from my sketch:

Code: Select all

#include <RTClib.h>  // required for RTC
#define TIME_24_HOUR false;
RTC_DS3231 rtc;

void setup() {
if (!rtc.begin()) {
    Serial.println("Couldn't find clock!");
    while (1)
      ;
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //set time to compile timestamp
}

void displayUpdate() {
  DateTime now = rtc.now();
  char timeStr[] = "hh:mm";
  char dateStr[] = "DDD, MMM DD YYYY";

  u8g2.setFont(u8g2_font_crox3cb_mn);
  u8g2.setCursor(30, 45);
  u8g2.print(now.toString(timeStr));
  u8g2.setFont(u8g2_font_5x7_mf);
  u8g2.setCursor(20, 60);
  u8g2.print(now.toString(dateStr));
  u8g2.sendBuffer();
}

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Cannot set 12hour mode on DS3121 using RTClib

Post by mikeysklar »

There is 12 hour support, but it still requires another functional to twelveHour() to decode it. The time is always stored as 24 hour.

Code: Select all

  // Fetching the hour assumes 24 hour time (never 12)
  // because this library exclusively stores the time
  // in 24 hour format. Note that the DS3231 supports
  // 12 hour storage, and sets bits to indicate the type
  // that is stored.
Convert to 12 hour.

Code: Select all

/**************************************************************************/
/*!
      @brief  Return the hour in 12-hour format.
      @return Hour (1--12).
*/
/**************************************************************************/
uint8_t DateTime::twelveHour() const {
  if (hh == 0 || hh == 12) { // midnight or noon
    return 12;
  } else if (hh > 12) { // 1 o'clock or later
    return hh - 12;
  } else { // morning
    return hh;
  }
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”