9 /* define fixed output-Strings */
10 char *season_long[5] = {
18 char *season_short[5] = {
34 char *day_short[5] = {
42 char *holidays[10] = {
55 /* A helper-struct, taking the discordian date */
64 /* Print the date *dt in format *format */
65 int format_output(char *format, struct disc_time *dt) {
69 for (i = format; *i != '\0'; i++) {
75 /* Weekday in long and abbreviation */
77 printf("%s", day_long[dt->week_day]);
80 printf("%s", day_short[dt->week_day]);
82 /* Season in long and abbreviation */
84 printf("%s", season_long[dt->season]);
87 printf("%s", season_short[dt->season]);
89 /* Day of the season (ordinal and cardinal) */
91 printf("%d", dt->season_day + 1);
94 printf("%d", dt->season_day + 1);
95 switch (dt->season_day) {
112 printf("%d", dt->year);
116 if (dt->season_day == 4) {
117 printf(holidays[dt->season]);
119 if (dt->season_day == 49) {
120 printf(holidays[dt->season + 5]);
123 /* Stop parsing the format string, except on Holidays */
125 if (dt->season_day != 4 && dt->season_day != 49) {
129 /* Newline- and Tabbing-characters */
136 /* The St. Tib's Day replacement */
138 tibs_end = strstr(i, "%}");
139 if (tibs_end == NULL) {
143 if (dt->st_tibs_day) {
144 /* We outpt "St. Tib's Day... */
145 printf("St. Tib's Day");
147 /* ...or parse the substring between %{ and %} ... */
149 if (!format_output(i + 2, dt)) return 0;
152 /* ...and continue with the rest */
159 /* No escape-sequence, so we just skip */
160 printf("%%%c",*(i+1));
168 /* Get the current date and convert it to discordian */
169 struct disc_time *get_ddate(struct tm *current_tm) {
170 static struct disc_time dt;
172 if (current_tm == NULL)
175 /* We have to know, whether we have to insert St. Tib's Day, so whether it's a leap
176 year in gregorian calendar */
177 int is_leap_year = !(current_tm->tm_year % 4) &&
178 (!(current_tm->tm_year % 400) || current_tm->tm_year % 100);
180 if (is_leap_year && current_tm->tm_yday == 59) {
181 /* On St. Tibs Day we don't have to define a date */
185 dt.season_day = current_tm->tm_yday % 73;
186 if (is_leap_year && current_tm->tm_yday > 59) {
187 dt.week_day = (current_tm->tm_yday - 1) % 5;
189 dt.week_day = current_tm->tm_yday % 5;
192 dt.year = current_tm->tm_year + 3066;
193 dt.season = current_tm->tm_yday / 73;
197 void print_ddate(const char *format, struct tm *current_tm) {
198 static char *form = NULL;
199 struct disc_time *dt;
200 if ((dt = get_ddate(current_tm)) == NULL)
203 if ((form = malloc(strlen(format) + 1)) == NULL)
205 if (output_format == O_I3BAR)
206 printf("{\"name\":\"ddate\", \"full_text\":\"");
207 strcpy(form, format);
208 format_output(form, dt);
209 if (output_format == O_I3BAR)