13 Calendar

 

It wasn’t just the Mayans who invented their own calendar. Just about every early culture featured some form of classification for the passage of days. The Mayans gained notoriety in 2012 because it was the end of one of their great calendrical cycles—the long count, or b’ak’tun. It wasn’t the end of the world—more like turning the page on one of those cheap insurance company calendars. Bummer.

Most cultures start with lunar calendars and eventually switch to solar calendars, either fully or reluctantly. Hebrew, Muslim, Eastern Orthodox, and Chinese calendars are still used today, with different year values and lunar features. Julius Caesar took a stab at updating the Roman calendar system—before the Senate took various stabs at him. And Pope Gregory introduced our modern calendar system in the year 1582.

Even with calendar utilities handy, coding your own calendar tools helps hone your time programming skills in C and more. In this chapter, you learn to:

  • Appreciate the cal program
  • Calculate holidays
  • Code week, month, and year utilities
  • Output color text
  • Color-code important dates

Yes, Unix has featured the cal program since the steam-powered days. Still, understanding date-and-time programming is important for all C coders. By practicing on these utilities, you can better code your own, custom date programs. You can also use the techniques presented here in any program that relies upon date calculations.

13.1 The calendar program

13.2 Good dates to know

13.2.1 Creating constants and enumerating dates

13.2.2 Finding the day of the week

13.2.3 Calculating the first day of the month

13.2.4 Identifying leap years

13.2.5 Getting the time zone correct

13.3 Calendar utilities

13.3.1 Generating a week

13.3.2 Showing a month

13.3.3 Displaying a full year

13.3.4 Putting the full year into a grid

13.4 A calendar in color