I’m playing with displaying time on an ssd1306 display using lambdas… trying a simple fix to remove the leading 0 the hour variable (for %I from now()), and for some reason it doesn’t want to print a simple blank space for me:
That seems like it should work, and I get an X in the right spot does if I replace " " with “X”. I know I could just printf and format my own string using .hour, .minute, & .second, but that’s a lot more work, and I figured there’s some simpler way I could fool it to actually print the blank space over that leading 0. Is there a way, or do I have to just slog through string formatting?
[edit: LOL, nevermind I see what’s going on here. I think the space is actually printing, but the graphic code just “adds on pixels”, and doesn’t “turn off” any pixels. That means a space does nothing to the display! I should have figured that out when the X was overlaying the 1 in my example mentioned above. Looks like there’s no way around strings… honestly a bit annoying that the %I has a leading zero… in what case is that desired?! ]
Google landed me here. So this is to help the next guy.
Here’s a single line I got to work based on what you have above. The space did work for me.
time = id(my_time).now();
if (time.is_valid()) {
it.strftime(0, 3, "%a %b %e %I:%M %p", time);
// ugly fix for leading zero with AM and PM, time.hour is [0-59]
if ((time.hour > 0 && time.hour < 10) || (time.hour > 12 && time.hour < 22)) it.print(12, 2, " ");
}
I also ended up here eon’s later and came up with this super simple solution – don’t use strftime, example:
auto cur_time = id(hass_time).now();
if (cur_time.is_valid()) {
auto hour = cur_time.hour;
// If the hour is into the 13-23 range for military time, subtract 12 to get back to non-military
if (hour > 12) hour = hour - 12;
// And if the hour is "zero", rotate that to 12 (midnight)
if (hour == 0) hour = 12;
// output with formatting that's always 5 characters long, padding minutes with zeros and the hour with space
it.printf(it.get_width()/2, 0, id(vt), TextAlign::TOP_CENTER, "%2d:%02d", hour, cur_time.minute);
} else {
it.print(0, 0, id(vt), "WIFI");
}