TimeLib setting the day of week

hi all,

I was wondering if there is a way to set the day of the week when using TimeLib.h library.
my project should have DOW 1 for Monday and TimeLib uses 1 for Sunday…

I know i can set other values like this:

setTime(hr,min,sec,day,mnth,yr);

but can not find option to set the DayofWeek.
I am using DS3231 RTC

Many thanks,
Alek

Just use the DOW from TimeLib.h and adjust it as required

can you please elaborate just a little bit more, please :slight_smile:

from TimeLib.h documentation:
weekday(); // day of the week (1-7), Sunday is day 1

Many thanks,
Alek

Suppose that it is Monday and TimeLib says DOW is 2 but you want DOW to be 1 then could you calculate your required value from the TimeLib value ?

yes, suppose i need to do some math, not so difficult but what i have is a DCF77 code that should set the DOW per DCF standard (iso). I can easily just use DCF variable for DOW but i want to link it to TimeLib DOW so if i lose DCF signal for whatever reason, i would like my DOW to continue running from DS3231.

So the ideal option would be if i could somehow set TimeLib DOW by using DCF77 DOW data.

so something like:

dcfDOW = weekday() -1
then from this i could have a new variable name… kind of tangled in this and cant see the solution, i guess it simple but i cant see it

many thanks,
Alek

if i got it right, TimeLib.h doesnt even use the DOW capability of DS3231 but rather it calculates it based on the date… so no point of setting the DOW to the DS3231 in this case…

seems like math is the way to go

thanks,
Alek

ok, i’ve started with this:

int myWeekDay = (weekday() - 1);

but, what happens when sunday comes (1) than i will get 0, while i should get 7.
OR, should i make a “switch” case, for each TimeLib DOW number to assign a number fo myWeekDay

A function like this

int myWeekday()
{
  int weekdays[] = {1, 2, 3, 4, 5, 6, 0};
  int convertedWeekday = weekdays[weekday()];
  return convertedWeekday;
}

will do the conversion

NOTE : I have lost track of the exact conversion required so the array may need to be adjusted

thanks UKHeliBob

my code goes like this:

if (myWeekDay() == 1) {
    MaximMATRIX.setRow(MonTueDisplay,1,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,2,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,3,B11111111); 
  } else {
    MaximMATRIX.setRow(MonTueDisplay,1,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,2,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,3,B00000000);
  }

  if (myWeekDay() == 2) {
    MaximMATRIX.setRow(MonTueDisplay,5,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,6,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,7,B11111111); 
  } else {
    MaximMATRIX.setRow(MonTueDisplay,5,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,6,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,7,B00000000);
  }

  if (myWeekDay() == 3) {
    MaximMATRIX.setRow(WedThuDisplay,1,B11111111);
    MaximMATRIX.setRow(WedThuDisplay,2,B11111111);
    MaximMATRIX.setRow(WedThuDisplay,3,B11111111); 
  } else {
    MaximMATRIX.setRow(WedThuDisplay,1,B00000000);
    MaximMATRIX.setRow(WedThuDisplay,2,B00000000);
    MaximMATRIX.setRow(WedThuDisplay,3,B00000000);
  }
...........

but now i am getting +1, so today Monday is no longer 2 but now 3… while i need it to be 1.

So Monday 1, Tuesday 2… Sunday 7

many thanks,
Alek

I think this formula will shift the days as you want

(tm.Wday() + 5)%7 + 1

I resisted the temptation to suggest using the modulo operator as I have always understood that it is expensive in terms of processor usage

Change the values in the array - I did warn you !

I can’t tell which way you trying to go.

myDay = yourDay - 1;
if (myDay == 0) myDay = 7;

Or

myDay = yourDay + 1;
if  (myDay == 8) myDay = 1;

I don’t know what’s expensive or not, I’ve used the below instead of the modulo operator:

hours += someNumberOfHours;
while (hours >= 24) hours -= 24;

and for wrangling angles going around 360.

I’m thinking any/all of these wouldn’t be so slow as to need worrying about. The one that looks prettiest wins so a matter of opinion.

a7

thanks guys!

this works now:

int myWeekday()
{
  int weekdays[] = {6, 7, 1, 2, 3, 4, 5};
  int convertedWeekday = weekdays[weekday()];
  return convertedWeekday;
}


void displayData(void)
{
  
  if (myWeekday() == 1) {
    MaximMATRIX.setRow(MonTueDisplay,1,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,2,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,3,B11111111); 
  } else {
    MaximMATRIX.setRow(MonTueDisplay,1,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,2,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,3,B00000000);
  }

  if (myWeekday() == 2) {
    MaximMATRIX.setRow(MonTueDisplay,5,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,6,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,7,B11111111); 
  } else {
    MaximMATRIX.setRow(MonTueDisplay,5,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,6,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,7,B00000000);
  }
........

seems simple and elegant

(for Gods sake, why would anyone start a week on sunday!?)

Please post the complete displayData() function. It may be possible to simplify it, but no promises

here it is, messy and unnecessary long, that should have been my next task to try and minify:

void displayData(void)
{
  
  if (myWeekday() == 1) {
    MaximMATRIX.setRow(MonTueDisplay,1,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,2,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,3,B11111111); 
  } else {
    MaximMATRIX.setRow(MonTueDisplay,1,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,2,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,3,B00000000);
  }

  if (myWeekday() == 2) {
    MaximMATRIX.setRow(MonTueDisplay,5,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,6,B11111111);
    MaximMATRIX.setRow(MonTueDisplay,7,B11111111); 
  } else {
    MaximMATRIX.setRow(MonTueDisplay,5,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,6,B00000000);
    MaximMATRIX.setRow(MonTueDisplay,7,B00000000);
  }

  if (myWeekday() == 3) {
    MaximMATRIX.setRow(WedThuDisplay,1,B11111111);
    MaximMATRIX.setRow(WedThuDisplay,2,B11111111);
    MaximMATRIX.setRow(WedThuDisplay,3,B11111111); 
  } else {
    MaximMATRIX.setRow(WedThuDisplay,1,B00000000);
    MaximMATRIX.setRow(WedThuDisplay,2,B00000000);
    MaximMATRIX.setRow(WedThuDisplay,3,B00000000);
  }

  if (myWeekday() == 4) {
    MaximMATRIX.setRow(WedThuDisplay,5,B11111111);
    MaximMATRIX.setRow(WedThuDisplay,6,B11111111);
    MaximMATRIX.setRow(WedThuDisplay,7,B11111111); 
  } else {
    MaximMATRIX.setRow(WedThuDisplay,5,B00000000);
    MaximMATRIX.setRow(WedThuDisplay,6,B00000000);
    MaximMATRIX.setRow(WedThuDisplay,7,B00000000);
  }

  if (myWeekday() == 5) {
    MaximMATRIX.setRow(FriSatDisplay,1,B11111111);
    MaximMATRIX.setRow(FriSatDisplay,2,B11111111);
    MaximMATRIX.setRow(FriSatDisplay,3,B11111111);
  } else {
    MaximMATRIX.setRow(FriSatDisplay,1,B00000000);
    MaximMATRIX.setRow(FriSatDisplay,2,B00000000);
    MaximMATRIX.setRow(FriSatDisplay,3,B00000000);
  }

  if (myWeekday() == 6) {
    MaximMATRIX.setRow(FriSatDisplay,5,B11111111);
    MaximMATRIX.setRow(FriSatDisplay,6,B11111111);
    MaximMATRIX.setRow(FriSatDisplay,7,B11111111); 
  } else {
    MaximMATRIX.setRow(FriSatDisplay,5,B00000000);
    MaximMATRIX.setRow(FriSatDisplay,6,B00000000);
    MaximMATRIX.setRow(FriSatDisplay,7,B00000000);
  }

  if (myWeekday() == 7) {
    MaximMATRIX.setRow(SunDisplay,1,B11111111);
    MaximMATRIX.setRow(SunDisplay,2,B11111111);
    MaximMATRIX.setRow(SunDisplay,3,B11111111); 
  } else {
    MaximMATRIX.setRow(SunDisplay,1,B00000000);
    MaximMATRIX.setRow(SunDisplay,2,B00000000);
    MaximMATRIX.setRow(SunDisplay,3,B00000000);
  }

  MaximCC.setChar(DisplaySecondsWeekNumber, 1, weekNumber / 10, false);
  MaximCC.setChar(DisplaySecondsWeekNumber, 0, weekNumber % 10, false);
  
  // display Date - with dashes between D-M-Y
  
  MaximCC.setChar(DisplayDate, 7, day() / 10, false);
  MaximCC.setChar(DisplayDate, 6, day() % 10, false);
  MaximCC.setChar(DisplayDate, 5, '-', false);
  MaximCC.setChar(DisplayDate, 4, month() / 10, false);
  MaximCC.setChar(DisplayDate, 3, month() % 10, false);
  MaximCC.setChar(DisplayDate, 2, '-', false);
  MaximCC.setChar(DisplayDate, 1, (year()/10) %10, false);
  MaximCC.setChar(DisplayDate, 0, year() % 10, false); 


  // display Summer- or Wintertime LED
  if (dcfDST == 1)
  {
    //digitalWrite(LED_CEST, HIGH);
    MaximMATRIX.setRow(CEST_CET,0,B11111111);
    MaximMATRIX.setRow(CEST_CET,1,B11111111);
    //digitalWrite(LED_CET, LOW);
    MaximMATRIX.setRow(CEST_CET,3,B00000000);
    MaximMATRIX.setRow(CEST_CET,4,B00000000);
  }
  else
  {
    //digitalWrite(LED_CET, HIGH);
    MaximMATRIX.setRow(CEST_CET,3,B11111111);
    MaximMATRIX.setRow(CEST_CET,4,B11111111);
    //digitalWrite(LED_CEST, LOW);
    MaximMATRIX.setRow(CEST_CET,0,B00000000);
    MaximMATRIX.setRow(CEST_CET,1,B00000000);
  }

  // display Leap Year LED
  if (leapYear == 1)
  {
    MaximMATRIX.setRow(CEST_CET,6,B11111111);
    MaximMATRIX.setRow(CEST_CET,7,B11111111);
    //digitalWrite(LED_LEAPYEAR, HIGH);
  }
  else
  {
    MaximMATRIX.setRow(CEST_CET,6,B00000000);
    MaximMATRIX.setRow(CEST_CET,7,B00000000);
    //digitalWrite(LED_LEAPYEAR, LOW);
  }
}

many thanks,
Alek

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.