7 /* define fixed output-Strings */
8 char *season_long[5] = {
16 char *season_short[5] = {
32 char *day_short[5] = {
40 char *holidays[10] = {
53 /* A helper-struct, taking the discordian date */
62 /* Print the date *dt in format *format */
63 int format_output(char *format, struct disc_time *dt) {
67 for (i = format; *i != '\0'; i++) {
73 /* Weekday in long and abbreviation */
75 printf("%s", day_long[dt->week_day]);
78 printf("%s", day_short[dt->week_day]);
80 /* Season in long and abbreviation */
82 printf("%s", season_long[dt->season]);
85 printf("%s", season_short[dt->season]);
87 /* Day of the season (ordinal and cardinal) */
89 printf("%d", dt->season_day + 1);
92 printf("%d", dt->season_day + 1);
93 switch (dt->season_day) {
110 printf("%d", dt->year);
114 if (dt->season_day == 4) {
115 printf(holidays[dt->season]);
117 if (dt->season_day == 49) {
118 printf(holidays[dt->season + 5]);
121 /* Stop parsing the format string, except on Holidays */
123 if (dt->season_day != 4 && dt->season_day != 49) {
127 /* Newline- and Tabbing-characters */
134 /* The St. Tib's Day replacement */
136 tibs_end = strstr(i, "%}");
137 if (tibs_end == NULL) {
141 if (dt->st_tibs_day) {
142 /* We outpt "St. Tib's Day... */
143 printf("St. Tib's Day");
145 /* ...or parse the substring between %{ and %} ... */
147 if (!format_output(i + 2, dt)) return 0;
150 /* ...and continue with the rest */
157 /* No escape-sequence, so we just skip */
158 printf("%%%c",*(i+1));
166 /* Get the current date and convert it to discordian */
167 struct disc_time *get_ddate() {
169 struct tm *current_tm;
170 static struct disc_time dt;
172 if ((current_time = time(NULL)) == (time_t)-1)
175 if ((current_tm = localtime(¤t_time)) == NULL)
178 /* We have to know, whether we have to insert St. Tib's Day, so whether it's a leap
179 year in gregorian calendar */
180 int is_leap_year = !(current_tm->tm_year % 4) &&
181 (!(current_tm->tm_year % 400) || current_tm->tm_year % 100);
183 if (is_leap_year && current_tm->tm_yday == 59) {
184 /* On St. Tibs Day we don't have to define a date */
188 dt.season_day = current_tm->tm_yday % 73;
189 if (is_leap_year && current_tm->tm_yday > 59) {
190 dt.week_day = (current_tm->tm_yday - 1) % 5;
192 dt.week_day = current_tm->tm_yday % 5;
195 dt.year = current_tm->tm_year + 3066;
196 dt.season = current_tm->tm_yday / 73;
200 void print_ddate(const char *format) {
201 static char *form = NULL;
202 struct disc_time *dt;
203 if ((dt = get_ddate()) == NULL)
206 if ((form = malloc(strlen(format) + 1)) == NULL)
208 strcpy(form, format);
209 format_output(form, dt);