In my sketch, I devised some tests to "display" time from my RTC in 12 hour format, even though the RTC operates in 24 hour format. When going through the growing pains of building this sketch last year, these tests provided me with the peaceful resolution I was seeking at the time, even though they are just a work around. That said, I don't want to unearth the reasons why I was unable to make the library function the RTC to work in 12 hour format, I am hoping to just expand my work around by concluding AM or PM. I am very new to chars and arrays and coding in general. Can someone look at the code and help me get a variable to be one of 2 possible outcomes (AM or PM)?
byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
byte zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
byte displayHour;
char meridian[3];
byte "AM";
byte "PM";
if (now.hour() == 0) // First we test if the hour reads "0"
{
displayHour = zeroHour;
meridian = "AM";
}
else if (now.hour() >= 13) // if no, Second we test if the hour reads "13 or more"
{
displayHour = twelveHour;
meridian = "PM";
}
else
{
displayHour = now.hour();
meridian = "AM";
}
12:01 (24hour clock) is one minute after noon, so logically it post-meridiem (literally after middle of day)
and should be 12:01pm. However it follows 11:59am, so it can easily be thought of as the next in the sequence
10:XXam, 11:XXam, 12:XX??
So you see 12:00pm for midday, but in spoken language we say 12 noon or midday, and its rare to be in
a situation where the meaning isn't clear from context.
Well, if we want to get unnecessarily technical about it, you could say that exactly 12:00 takes up an infinitesimal amount of time and is virtually never 12:00:000000...
So 12 is virtually always larger than EXACTLY 12, so like it may be 1 billionth of a second after 12 and thus refers to AFTER midday..."technically".
Ps991:
Well, if we want to get unnecessarily technical about it, you could say that exactly 12:00 takes up an infinitesimal amount of time and is virtually never 12:00:000000...
So 12 is virtually always larger than EXACTLY 12, so like it may be 1 billionth of a second after 12 and thus refers to AFTER midday..."technically".
Makes no sense to me. You're conflating what you could say, with what you do say. That is like saying 1 is never exactly 1 because you could say it is the repeating decimal 0.99999999999999999999...
I don't know about you, but when I say it's twelve, I don't mean twelve septillionths after twelve. I mean twelve. I realize you're not too serious about this.
Also, your reasoning could get stuck in a recursive loop. When you say "a few billionths after 12", you are referring to a fixed, infinitesimal 12. Otherwise you have no reference. If you subsequently say that 12 is really a few billionths after 12, then what you have is "a few billionths after a few billionths after 12". So after a while your time will slow down as all the billionths add up. Then you will begin to be minutes and hours late. Your boss will not buy it.
aarg, I was just saying that 12 noon is only exactly 12 for an infinitesimal amount of time, and thus, the AM/PM not applying to it only applies to that infinitesimal amount of time. This scenario doesn't really apply to simply numbers, it applies to time because time is always moving forward. Saying exactly 1 means exactly 1 because nothing is changing it, like time.
As to your last paragraph, that's not what I was implying/saying, although I do see your point about having a reference and if you are referring to that reference then it would not have an AM/PM...I'm starting to see how this is a problem...
From help received on another thread, I was able to resolve my issue. None the less, my board is an Ethermega with a chronodot ds3231 soldered to the i2c bus pins near the proto area. the device is powered via 9v wallwart. At some point down the road I will delve into my issues regarding actual 12 hour formatting, but for the sake of progressing my project, my coded work around has sufficed greatly. For the record, I have some time based calls, but I coded those in 24 hour format (in the main sketch). The coded 12 hour format always was just for display to human eyes.
This sketch is just the stripped down version of the ds1307 example that came with the RTClib library. I adjusted the sketch to my needs and used it to get these techniques applied and executable before I will migrate the success back to my main sketch. Here is the demo sketch though
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup () {
Serial.begin(57600);
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop () {
DateTime now = rtc.now();
byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
byte zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
byte displayHour;
byte MIN = now.minute();
byte SEC = now.second();
char* meridian[6];
meridian[0] = "AM";
meridian[1] = "PM";
if (now.hour() == 0) // First we test if the hour reads "0"
{
displayHour = zeroHour;
meridian[0];
}
else if (now.hour() >= 13) // if no, Second we test if the hour reads "13 or more"
{
displayHour = twelveHour;
meridian[1];
}
else
{
displayHour = now.hour();
meridian[0];
}
char timeStamp[13];
char dateStamp[11];
sprintf(timeStamp, "%02d:%02d:%02d-%02s", displayHour, MIN, SEC, meridian);
sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());
{
Serial.println(timeStamp);
Serial.println(dateStamp);
}
delay(3000);
}
I am still having an issue with the following line of code in that I can't make it print out the meridian solution. Am I in err with the sprintf formatting, or is my char* not set up properly?
I'm still too new to coding to quickly comprehend how things are supposed to be formatted. That said, I was under the impression that declaring meridian[0]=AM and meridian[1]=PM, and with the tests of now hour, I would be able to change which of the 2 (pointers?) meridian would display. The concept worked for me with displayHour, so I hoped it would also apply to the char*. Is there another way in which I can make a variable be either of 2 possible outcomes?
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup () {
Serial.begin(57600);
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop () {
DateTime now = rtc.now();
byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
byte zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
byte displayHour;
byte MIN = now.minute();
byte SEC = now.second();
char* meridian;
if (now.hour() == 0) // First we test if the hour reads "0"
{
displayHour = zeroHour;
meridian = "AM";
}
else if (now.hour() >= 13) // if no, Second we test if the hour reads "13 or more"
{
displayHour = twelveHour;
meridian = "PM";
}
else
{
displayHour = now.hour();
meridian = "AM";
}
char timeStamp[11];
char dateStamp[11];
sprintf(timeStamp, "%02d:%02d:%02d-%02s", displayHour, MIN, SEC, meridian);
sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());
{
Serial.println(timeStamp);
Serial.println(dateStamp);
}
delay(3000);
}