"weekday(t)+1" Problem when day is Saturday!

I need this piece of code for weather forecasts. The problem is that I'm not finding easy to print the days after. Today is Thursday (weekday=5), so, doing weekday+3, weekday(local)=8, instead of 1! How can I change this in time library, or even in the code?

tft.print(dayShortStr(weekday(local)+1));
tft.setCursor(7, 95);
tft.print(dayShortStr(weekday(local)+2));
tft.setCursor(7, 105);        
tft.print(dayShortStr(weekday(local)+3));

of course I cannot do if(wekday(local)==8) weekday(local)=1;

See the basic operators of C : the modulo operator is for your.

For instance adding 4 days modulo 7 give :

x = 6 ;
x = (x + 4) % 7 ;

now x is 3 :)

your code will look like :

tft.print((dayShortStr(weekday(local)+1))%7);

@jgarbasi I totally forgot to do that, thanks :)

But now imagining sunday, and using this, weekday will be 0, but is supposed to be 1.. sunday=1. anyway, I'll give it a try! :D

I don't know whis is your dayShortStr ,but you should rather make it start from 0 (0 = saturday ) . It's much easier to manage a 0-6 range than 1-7 with C language . Don't forget arrays start from 0. :)

@jgarbasi dayShortStr is defined in Time Libray, so I think it's not that easy to change..

ok but did you ever try to see what is the result of dayShortStr(0) ? or others values like 8 , 9. I didn't try it but maybe this function already use the modulo function internaly. In this case, you can even shorten your code , just by removing the % .

@jgarbasi ,

tft.print(dayShortStr(0)) prints "Err"; tft.print(dayShortStr(8)) and so on prints nothing

In the library DateStrings there's this:

const char dayStr0[] PROGMEM = "Err";
const char dayStr1[] PROGMEM = "Sunday";
const char dayStr2[] PROGMEM = "Monday";
const char dayStr3[] PROGMEM = "Tuesday";
const char dayStr4[] PROGMEM = "Wednesday";
const char dayStr5[] PROGMEM = "Thursday";
const char dayStr6[] PROGMEM = "Friday";
const char dayStr7[] PROGMEM = "Saturday";

How are you determining the day? I use the time library, and use the epoch value to compute dates and days. Works well. Get the current epoch, add or subtract the number of seconds to compute the new date/time/day, and there you have it.

edit: If you have access to the epoch value, I can post code I use for Daylight Saving Time so you can see how it works.

Then why did you take such a messy library, just for week days names.

You can create your own array, like this sample .

const char * dayName[] = {"Sunday", "Monday", "Tuesday", "Wedensday", "Thursday", "Friday", "Saturday"};

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:


Serial.println(dayName[0]);

}

Be carefull with many libraries, they can waste a lot of memory for poor results. you don't have too much memory on an Arduino Uno. Look at the warnings when compiling code with Arduino IDE.

jgarbasi: Then why did you take such a messy library, just for week days names.

You can create your own array, like this sample .

const char * dayName[] = {"Sunday", "Monday", "Tuesday", "Wedensday", "Thursday", "Friday", "Saturday"};

void setup() {   // put your setup code here, to run once: Serial.begin(9600); }

void loop() {   // put your main code here, to run repeatedly:

Serial.println(dayName[0]);

}




Be carefull with many libraries, they can waste a lot of memory for poor results.
you don't have too much memory on an Arduino Uno. Look at the warnings when compiling code with Arduino IDE.

This is complete baloney. The compiler only uses the code from a library that you actually use, because of optimization. If you #include the Time library and only use the day names array, that is all the code overhead that will be added to your memory allotment.

Also, there is nothing "messy" about the Time library. It is far and beyond the vast majority of libraries for efficiency and correctness. I've used it many, many times and never found any bug.

Some libraries do eat up memory, but only because the application requires most of the code to even run, and nothing useful can be done with a subset. TFT libraries, for example.

The OP has wisely chosen to use the Time library, and Surfer Tim has offered the correct and simple way of handling the problem.