Simple Calendar

Creator: HazardsMind

Date: 5/19/2015

I was going through some of my old files and I found this old sketch of a calendar that I made about a year ago. I did add in the days, Monday - Sunday, with the help of a function I found on online How to make a calendar. (To the person who wrote that, Thank you)

It might be hard for some people to follow as I used pointers to make my life easier, but I don't know how that translates for other people. :/

Format looks like this "Thursday, Jan/1/2015" in the serial monitor.

#define YEAR 2015

byte D = 1;
byte M = 0;
unsigned long Y = YEAR - 1;
char Date[25] = {0};
byte days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const char *months[] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

const char * Days[] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);

  for (unsigned int i = 0, _day = determinedaycode(Y); i < 365; i++, _day++)
  {
    Calendar(i, &D, &M, &Y);
    sprintf(Date, "%s, %s/%02d/%d", Days[(_day+1)%7], months[M], D, Y);
    Serial.println(Date);
  }
}

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

int determinedaycode(int year) // Gotten from online source
{
  int daycode;
  int d1, d2, d3;

  d1 = (year - 1) / 4.0;
  d2 = (year - 1) / 100.0;
  d3 = (year - 1) / 400.0;
  daycode = (year + d1 - d2 + d3) % 7;
  return daycode;
}

void Calendar(unsigned long Day, byte *_day, byte *_month, unsigned long *_year)
{
  static int T = 31, leapYear = 0, _D, factor;

  if ((factor = (Day % 365)) == 0)
  {
    T = 31;
    *_day = 1;
    *_month = 0;
    _D = 1;
  }
  else
    _D++;

  if (*_month == 1 && (leapYear = Leapyear(*_year)))
    days_in_month[1] = 29;
  else
    days_in_month[1] = 28;

  if ( _D > T )
  {
    *_month += 1;

    if (*_month > 12)
      *_month = 0;

    T += days_in_month[*_month];
  }

  *_day = (factor == 0) ? 1 : days_in_month[*_month] - (T - _D);

  *_year += ((factor == 0) ? 1 : 0);

}

int Leapyear(unsigned long year)
{
  return ( ( (((year) % 4) == 0) && (((year) % 100) != 0) ) || (((year) % 400) == 0) );
}

Share