Re: function returning days of the week



On Dec 31, 1:10 am, ssylee <sta***...@xxxxxxxxx> wrote:
I need to write a function that would read in a byte that would return
a number between 1 to 7, 1 being Sunday, 2 being Monday, etc. I want
to return an actual string that says "Sunday", or "Monday", etc.
corresponding to the number. I know that the best method to implement
a lookup conversion table would be using switch(variable ) ... case
x: .... structure. However, I may need to pass an array as one of the
parameters in order to access the text itself. Is there anything
inefficient in passing a character array as a parameter based on
memory consumption on an embedded microprocessor system? Thanks.

There is a C FAQ on Zeller's congruence.
It is trivial to add 1 and do a table lookup.

From the C-FAQ:
20.31: How can I find the day of the week given the date?

A: Use mktime() or localtime() (see questions 13.13 and 13.14,
but
beware of DST adjustments if tm_hour is 0), or Zeller's
congruence (see the sci.math FAQ list), or this elegant code
by
Tomohiko Sakamoto:

int dayofweek(int y, int m, int d) /* 0 = Sunday
*/
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4,
6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d)
% 7;
}

See also questions 13.14 and 20.32.

References: ISO Sec. 7.12.2.3.

I'll leave the obvious bit for you.
.


Quantcast