1 // vim:ts=4:sw=4:expandtab
7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
12 /* define fixed output-Strings */
13 char *season_long[5] = {
20 char *season_short[5] = {
34 char *day_short[5] = {
41 char *holidays[10] = {
53 /* A helper-struct, taking the discordian date */
62 /* Print the date *dt in format *format */
63 static int format_output(char *outwalk, char *format, struct disc_time *dt) {
64 char *orig_outwalk = outwalk;
68 for (i = format; *i != '\0'; i++) {
74 /* Weekday in long and abbreviation */
76 outwalk += sprintf(outwalk, "%s", day_long[dt->week_day]);
79 outwalk += sprintf(outwalk, "%s", day_short[dt->week_day]);
81 /* Season in long and abbreviation */
83 outwalk += sprintf(outwalk, "%s", season_long[dt->season]);
86 outwalk += sprintf(outwalk, "%s", season_short[dt->season]);
88 /* Day of the season (ordinal and cardinal) */
90 outwalk += sprintf(outwalk, "%d", dt->season_day + 1);
93 outwalk += sprintf(outwalk, "%d", dt->season_day + 1);
94 if (dt->season_day > 9 && dt->season_day < 13) {
95 outwalk += sprintf(outwalk, "th");
99 switch (dt->season_day % 10) {
101 outwalk += sprintf(outwalk, "st");
104 outwalk += sprintf(outwalk, "nd");
107 outwalk += sprintf(outwalk, "rd");
110 outwalk += sprintf(outwalk, "th");
116 outwalk += sprintf(outwalk, "%d", dt->year);
120 if (dt->season_day == 4) {
121 outwalk += sprintf(outwalk, "%s", holidays[dt->season]);
123 if (dt->season_day == 49) {
124 outwalk += sprintf(outwalk, "%s", holidays[dt->season + 5]);
127 /* Stop parsing the format string, except on Holidays */
129 if (dt->season_day != 4 && dt->season_day != 49) {
130 return (outwalk - orig_outwalk);
133 /* Newline- and Tabbing-characters */
135 outwalk += sprintf(outwalk, "\n");
138 outwalk += sprintf(outwalk, "\t");
140 /* The St. Tib's Day replacement */
142 tibs_end = strstr(i, "%}");
143 if (tibs_end == NULL) {
147 if (dt->st_tibs_day) {
148 /* We outpt "St. Tib's Day... */
149 outwalk += sprintf(outwalk, "St. Tib's Day");
151 /* ...or parse the substring between %{ and %} ... */
153 outwalk += format_output(outwalk, i + 2, dt);
156 /* ...and continue with the rest */
163 /* No escape-sequence, so we just skip */
164 outwalk += sprintf(outwalk, "%%%c", *(i + 1));
169 return (outwalk - orig_outwalk);
172 /* Get the current date and convert it to discordian */
173 struct disc_time *get_ddate(struct tm *current_tm) {
174 static struct disc_time dt;
176 if (current_tm == NULL)
179 /* We have to know, whether we have to insert St. Tib's Day, so whether it's a leap
180 * year in gregorian calendar */
181 int is_leap_year = !(current_tm->tm_year % 4) &&
182 (!(current_tm->tm_year % 400) || current_tm->tm_year % 100);
184 /* If St. Tib's Day has passed, it will be necessary to skip a day. */
185 int yday = current_tm->tm_yday;
187 if (is_leap_year && yday == 59) {
188 /* On St. Tibs Day we don't have to define a date */
192 if (is_leap_year && yday > 59)
195 dt.season_day = yday % 73;
196 dt.week_day = yday % 5;
198 dt.year = current_tm->tm_year + 3066;
199 dt.season = yday / 73;
203 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t) {
204 char *outwalk = buffer;
205 static char *form = NULL;
206 struct tm current_tm;
207 struct disc_time *dt;
208 set_timezone(NULL); /* Use local time. */
209 localtime_r(&t, ¤t_tm);
210 if ((dt = get_ddate(¤t_tm)) == NULL)
213 if ((form = malloc(strlen(format) + 1)) == NULL)
215 strcpy(form, format);
216 outwalk += format_output(outwalk, form, dt);
217 OUTPUT_FULL_TEXT(buffer);