Improve this code? - date/time

I have this code and it's working, but I feel there is a way of combining the arrays to make it simpler. But I can't seem to figure it out.

It's to print day of week, date and time to a TFT display.

Credits to original posts:
Day of week code
Date/Time code

Here is the code, called every 1 second:

void setDateTime()
{
 
 //Get day of week
  char const * wdays[] = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
  int day_ = weekday() % 7;
  
//Get date and time
  char date_time [22];
  sprintf (date_time, "%02d/%02d/%04d    %02d:%02d", day(), month(), year(), hour(), minute());
  
//Print results to TFT screen
  tft.setTextSize(2);
  tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  tft.setCursor(30, 15);
  tft.print(wdays[day_]);
  tft.print(" ");
  tft.print(date_time);
}

It results in the correct display " Tues 02/04/2019 15:55"

I just feel it could be simpler somehow..

You can do your wdays calculation prior to your sprintf statement, then pass it along with the other variables and the " " and print the whole buffer in a single statement.

@Perehama thank you, I first see the " " was totally not needed so have edited my original post to remove that.

I am a little unsure that if I add the wdays value into the sprintf, will I have to [22] value ?

char date_time [22];

877:
@Perehama thank you, I first see the " " was totally not needed so have edited my original post to remove that.

Please don't do that.

AWOL:
Please don't do that.

OK I understand, I've returned the post back to the original..

void setDateTime()
{
  //Setup day of week
  char const * wdays[] = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
  int day_ = weekday() % 7;
  
  //Setup date and time
  char date_time [22];
  sprintf (date_time, "%03d %02d/%02d/%04d    %02d:%02d", (wdays[day_]), day(), month(), year(), hour(), minute()  );
  
  //Print results to TFT screen
  tft.setTextSize(2);
  tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  tft.setCursor(30, 15);
  tft.print(date_time);
}

Gives “1073645105 02/04/2019 16:28

Obviously the “(wdays[day_])” does not fit well in the sprintf code.

Any pointers please?

… “%03d” expects an not but you give it a cstring

arduino_new:
... "%03d" expects an not but you give it a cstring

Thanks that's just the clue I needed :slight_smile:

I obviously need to understand this a little more, working code below..

void setDateTime()
{
  //Setup day of week
  char const * wdays[] = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
  int day_ = weekday() % 7;
  
  //Setup date and time
  char date_time [22];
  sprintf (date_time, "%s %02d/%02d/%04d    %02d:%02d", wdays[day_], day(), month(), year(), hour(), minute()  );
  
  //Print results to TFT screen
  tft.setTextSize(2);
  tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  tft.setCursor(25, 15);
  tft.print(date_time);
}

877:
Thanks that's just the clue I needed :slight_smile:

I obviously need to understand this a little more, working code below..

void setDateTime()

{
  //Setup day of week
  char const * wdays = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
  int day_ = weekday() % 7;
 
  //Setup date and time
  char date_time [22];
  sprintf (date_time, "%s %02d/%02d/%04d    %02d:%02d", wdays[day_], day(), month(), year(), hour(), minute()  );
 
  //Print results to TFT screen
  tft.setTextSize(2);
  tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  tft.setCursor(25, 15);
  tft.print(date_time);
}

dam autocorrect!

I understood what you meant though! :wink: