1

There is a ANSWER to this question on this site: "Convert to and from Unix Timestamp" The ANSWER includes a code example. In the code example there is an array named "days[][]". What is this or where is this declared and populated?

  • Please, link to the answer. – Edgar Bonet Aug 11 '17 at 12:14
  • You must ask that as a comment to the original question, not start a new question. – user31481 Aug 11 '17 at 12:22
1

Mike, After looking at the question you are referring to (Convert to and from Unix Timestamp) your question makes a lot more sense.

That code is incomplete and its difficult to be certain what anything is for. My best guess is days[][] would look like this:

byte days [4][13] =
{
    {366, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {365, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {365, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {365, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
}

I derived this from:

  for (year=3; year>0; year--)     // 3 to 0 is a 4 year cycle, which looks like leap year checking.
  {
      if (epoch >= days[year][0])  // Checking the epoch against the first element in the array, which implies that's number of days per year.
          break;
  }

And the other elements in the array I guess from this

  for (month=11; month>0; month--)
  {
      if (epoch >= days[year][month])
          break;
  }

However The above way trades storage for performance, it uses 52 bytes to hold the days[][] rather than use extra CPU cycles. A less storage intensive version would be:

byte daysInMonth = 31;
if (month == 2)
{
  if (year%4 == 0)
    daysInMonth = 29;
  else
    daysInMonth = 28;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
  daysInMonth = 30;
}
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.