]> Pileus Git - lackey/blob - src/util.c
55cd265a14b45812ea66a743f39ff139f83303f5
[lackey] / src / util.c
1 /* Time Keeping Bugs Abound! */
2
3 #include <stdio.h>
4
5 #include "util.h"
6
7 /* Helper functions */
8 static int is_leap_year(year_t year)
9 {
10         return (year % 400 == 0) ? 1 :
11                (year % 100 == 0) ? 0 :
12                (year % 4   == 0) ? 1 : 0;
13 }
14
15 static wday_t day_of_week(year_t year, month_t month, day_t day)
16 {
17         static int tmp[] = {0, 3, 2, 5, 0, 3,
18                             5, 1, 4, 6, 2, 4};
19         if (month < 3)
20                 year--;
21         int start = year + year / 4
22                          - year / 100
23                          + year / 400
24                          + tmp[month];
25         return (start + day) % 7;
26 }
27
28 /* Time functions */
29 int days_in_year(year_t year)
30 {
31         return 365 + is_leap_year(year);
32 }
33
34 int days_in_month(year_t year, month_t month)
35 {
36         static int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
37         int days = mdays[month];
38         if (month == FEB)
39                 days += is_leap_year(year);
40         return days;
41 }
42
43 int weeks_in_month(year_t year, month_t month)
44 {
45         int start = start_of_month(year, month);
46         int days  = days_in_month(year, month);
47         return ((start + days)-1) / 7 + 1;
48 }
49
50 wday_t start_of_month(year_t year, month_t month)
51 {
52         return day_of_week(year, month, 1);
53 }
54
55 /* Debug functions */
56 const char *month_to_str(month_t month)
57 {
58         static const char *map[] =
59                 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
60                   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
61         return map[month];
62 }
63 const char *month_to_string(month_t month)
64 {
65         static const char *map[] =
66                 { "January",   "February", "March",    "April",
67                   "May",       "June",     "July",     "August",
68                   "September", "October",  "November", "December" };
69         return map[month];
70 }
71
72 const char *day_to_st(wday_t day)
73 {
74         static const char *map[] =
75                 { "Su","Mo", "Tu", "We", "Th", "Fr", "Sa" };
76         return map[day];
77 }
78 const char *day_to_str(wday_t day)
79 {
80         static const char *map[] =
81                 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
82         return map[day];
83 }
84 const char *day_to_string(wday_t day)
85 {
86         static const char *map[] =
87                 { "Sunday",   "Monday", "Tuesday", "Wednesday",
88                   "Thursday", "Friday", "Saturday" };
89         return map[day];
90 }
91
92 /* Test functions */
93 void test_time(void)
94 {
95         printf("Year Month     Start Weeks Days\n");
96         for (int y = 2012; y <= 2012; y++)
97         for (int m = JAN;  m <= DEC;  m++) {
98                 printf("%-5d",  y);
99                 printf("%-10s", month_to_string(m));
100                 printf("%-6s",  day_to_str(start_of_month(y,m)));
101                 printf("%-6d",  weeks_in_month(y,m));
102                 printf("%-2d",  days_in_month(y,m));
103                 printf("\n");
104         }
105 }