Displaying a leading space

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:

        lambda: |-
          auto nowTime = id(ha_time).now();
          if (nowTime.hour < 10) {
            it.strftime(0, 0, id(my_fontlg), "%I:%M:%S %p", nowTime);
            it.printf(0, 11, id(my_fontlg), " ");
          }
          else  {
            it.strftime(0, 0, id(my_fontlg), "%I:%M:%S %p", nowTime);
          }

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?! :confused: ]

Actually, sorted it with a blank rectangle:

        lambda: |-
          auto nowTime = id(ha_time).now();
          it.strftime(5, 0, id(my_fontlg), "%I:%M:%S %p", nowTime);
          if (nowTime.hour < 10) {
            it.filled_rectangle(5, 0, 15, 20, COLOR_OFF);
          }
          else if(nowTime.hour < 22 && nowTime.hour > 12) {
            it.filled_rectangle(5, 0, 15, 20, COLOR_OFF);
          }
          it.print(0, 25, id(my_fontlg), "OUT:");
          it.print(0, 45, id(my_fontlg), "IN:");
          it.printf(it.get_width(), 25, id(my_fontlg), TextAlign::TOP_RIGHT, "%.1f F", id(backyard_temp).state);
          it.printf(it.get_width(), 45, id(my_fontlg), TextAlign::TOP_RIGHT, "%.1f F", id(inside_temp).state);
          if (id(button).state) {
            it.line(0, 63, 127, 63);
          }

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");
      }