6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
11 /* define fixed output-Strings */
12 char *season_long[5] = {
20 char *season_short[5] = {
36 char *day_short[5] = {
44 char *holidays[10] = {
57 /* A helper-struct, taking the discordian date */
66 /* Print the date *dt in format *format */
67 static int format_output(char *outwalk, char *format, struct disc_time *dt) {
68 char *orig_outwalk = outwalk;
72 for (i = format; *i != '\0'; i++) {
78 /* Weekday in long and abbreviation */
80 outwalk += sprintf(outwalk, "%s", day_long[dt->week_day]);
83 outwalk += sprintf(outwalk, "%s", day_short[dt->week_day]);
85 /* Season in long and abbreviation */
87 outwalk += sprintf(outwalk, "%s", season_long[dt->season]);
90 outwalk += sprintf(outwalk, "%s", season_short[dt->season]);
92 /* Day of the season (ordinal and cardinal) */
94 outwalk += sprintf(outwalk, "%d", dt->season_day + 1);
97 outwalk += sprintf(outwalk, "%d", dt->season_day + 1);
98 if (dt->season_day > 9 && dt->season_day < 13) {
99 outwalk += sprintf(outwalk, "th");
103 switch (dt->season_day % 10) {
105 outwalk += sprintf(outwalk, "st");
108 outwalk += sprintf(outwalk, "nd");
111 outwalk += sprintf(outwalk, "rd");
114 outwalk += sprintf(outwalk, "th");
120 outwalk += sprintf(outwalk, "%d", dt->year);
124 if (dt->season_day == 4) {
125 outwalk += sprintf(outwalk, "%s", holidays[dt->season]);
127 if (dt->season_day == 49) {
128 outwalk += sprintf(outwalk, "%s", holidays[dt->season + 5]);
131 /* Stop parsing the format string, except on Holidays */
133 if (dt->season_day != 4 && dt->season_day != 49) {
134 return (outwalk - orig_outwalk);
137 /* Newline- and Tabbing-characters */
139 outwalk += sprintf(outwalk, "\n");
142 outwalk += sprintf(outwalk, "\t");
144 /* The St. Tib's Day replacement */
146 tibs_end = strstr(i, "%}");
147 if (tibs_end == NULL) {
151 if (dt->st_tibs_day) {
152 /* We outpt "St. Tib's Day... */
153 outwalk += sprintf(outwalk, "St. Tib's Day");
155 /* ...or parse the substring between %{ and %} ... */
157 outwalk += format_output(outwalk, i + 2, dt);
160 /* ...and continue with the rest */
167 /* No escape-sequence, so we just skip */
168 outwalk += sprintf(outwalk, "%%%c", *(i+1));
173 return (outwalk - orig_outwalk);
176 /* Get the current date and convert it to discordian */
177 struct disc_time *get_ddate(struct tm *current_tm) {
178 static struct disc_time dt;
180 if (current_tm == NULL)
183 /* We have to know, whether we have to insert St. Tib's Day, so whether it's a leap
184 year in gregorian calendar */
185 int is_leap_year = !(current_tm->tm_year % 4) &&
186 (!(current_tm->tm_year % 400) || current_tm->tm_year % 100);
188 /* If St. Tib's Day has passed, it will be necessary to skip a day. */
189 int yday = current_tm->tm_yday;
191 if (is_leap_year && yday == 59) {
192 /* On St. Tibs Day we don't have to define a date */
196 if (is_leap_year && yday > 59)
199 dt.season_day = yday % 73;
200 dt.week_day = yday % 5;
202 dt.year = current_tm->tm_year + 3066;
203 dt.season = yday / 73;
207 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, struct tm *current_tm) {
208 char *outwalk = buffer;
209 static char *form = NULL;
210 struct disc_time *dt;
211 if ((dt = get_ddate(current_tm)) == NULL)
214 if ((form = malloc(strlen(format) + 1)) == NULL)
216 strcpy(form, format);
217 outwalk += format_output(outwalk, form, dt);
218 OUTPUT_FULL_TEXT(buffer);