]>
cygwin.com Git - cygwin-apps/cygutils.git/blob - src/ddate/ddate.c
1 /* $ DVCS ID: $jer|,523/lhos,KYTP!41023161\b"?" <<= DO NOT DELETE! */
3 /* ddate.c .. converts boring normal dates to fun Discordian Date -><-
4 written the 65th day of The Aftermath in the Year of Our Lady of
5 Discord 3157 by Druel the Chaotic aka Jeremy Johnson aka
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * See the COPYING file for license information.
29 and I'm not responsible if this program messes anything up (except your
30 mind, I'm responsible for that)
32 (k) YOLD 3161 and all time before and after.
33 Reprint, reuse, and recycle what you wish.
34 This program is in the public domain. Distribute freely. Or not.
35 (*) Charles Wilson changes under GPL
37 Majorly hacked, extended and bogotified/debogotified on
38 Sweetmorn, Bureaucracy 42, 3161 YOLD, by Lee H:. O:. Smith, KYTP,
39 aka Andrew Bulhak, aka acb@dev.null.org
41 and I'm not responsible if this program messes anything up (except your
42 mind, I'm responsible for that) (and that goes for me as well --lhos)
45 Bureflux 3161: First release of enhanced ddate with format strings
46 59 Bcy, 3161: PRAISE_BOB and KILL_BOB options split, other minor
49 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
50 - added Native Language Support
52 2000-03-17 Burt Holzman <bnh@iname.com>
53 - added range checks for dates
55 2002-01-26 Charles Wilson
56 - minor changes so that it compiles on cygwin
57 - removed nls.h dependency (but retain NLS capabiltiy)
58 - changes (c) 2002 Charles Wilson. Licensed under the GPL.
61 /* configuration options VVVVV READ THIS!!! */
63 /* If you wish ddate(1) to print the date in the same format as Druel's
64 * original ddate when called in immediate mode, define OLD_IMMEDIATE_FMT
67 #define OLD_IMMEDIATE_FMT
69 /* If you wish to use the US format for aneristic dates (m-d-y), as opposed to
70 * the Commonwealth format, define US_FORMAT.
73 /* #define US_FORMAT */
75 /* If you are ideologically, theologically or otherwise opposed to the
76 * Church of the SubGenius and do not wish your copy of ddate(1) to contain
77 * code for counting down to X-Day, undefine KILL_BOB */
79 #define KILL_BOB 13013
81 /* If you wish ddate(1) to contain SubGenius slogans, define PRAISE_BOB */
83 /*#define PRAISE_BOB 13013*/
90 #define PACKAGE "cygutils"
94 #define LOCALEDIR "/usr/share/locale"
101 # undef setlocale(a, b)
102 # define setlocale(a, b) /* empty */
106 # include <libintl.h>
107 # define _(Text) gettext (Text)
109 # define N_(String) gettext_noop (String)
111 # define N_(String) (String)
114 # undef bindtextdomain
115 # define bindtextdomain(Domain, Directory) /* empty */
117 # define textdomain(Domain) /* empty */
118 # define _(Text) (Text)
119 # define N_(Text) (Text)
123 #define inline /* foo */
127 int xday_countdown(int yday
, int year
);
131 /* string constants */
133 char *day_long
[5] = {
134 "Sweetmorn", "Boomtime", "Pungenday", "Prickle-Prickle", "Setting Orange"
137 char *day_short
[5] = {"SM","BT","PD","PP","SO"};
139 char *season_long
[5] = {
140 "Chaos", "Discord", "Confusion", "Bureaucracy", "The Aftermath"
143 char *season_short
[5] = {"Chs", "Dsc", "Cfn", "Bcy", "Afm"};
145 char *holyday
[5][2] = {
146 { "Mungday", "Chaoflux" },
147 { "Mojoday", "Discoflux" },
148 { "Syaday", "Confuflux" },
149 { "Zaraday", "Bureflux" },
150 { "Maladay", "Afflux" }
154 int season
; /* 0-4 */
156 int yday
; /* 0-365 */
157 int year
; /* 3066- */
161 "Hail Eris!", "All Hail Discordia!", "Kallisti!", "Fnord.", "Or not.",
162 "Wibble.", "Pzat!", "P'tang!", "Frink!",
164 "Slack!", "Praise \"Bob\"!", "Or kill me.",
165 #endif /* PRAISE_BOB */
166 /* randomness, from the Net and other places. Feel free to add (after
167 checking with the relevant authorities, of course). */
168 "Grudnuk demand sustenance!", "Keep the Lasagna flying!",
169 "Umlaut Zebra über alles!", "You are what you see.",
170 "Or is it?", "This statement is false.",
171 #if defined(linux) || defined (__linux__) || defined (__linux)
172 "Hail Eris, Hack Linux!",
177 char default_fmt
[] = "%{%A, %B %d%}, %Y YOLD";
178 char *default_immediate_fmt
=
179 #ifdef OLD_IMMEDIATE_FMT
180 "Today is %{%A, the %e day of %B%} in the YOLD %Y%N%nCelebrate %H"
186 #define DY(y) (y+1166)
188 static inline char *ending(int i
) {
189 return (i
%10==1)?"st":(i
%10==2?"nd":(i
%10==3?"rd":"th"));
192 static inline int leapp(int i
) {
193 return (!(DY(i
)%4))&&((DY(i
)%100)||(!(DY(i
)%400)));
196 /* select a random string */
197 static inline char *sel(char **strings
, int num
) {
198 return(strings
[random()%num
]);
201 void print(struct disc_time
,char **); /* old */
202 void format(char *buf
, const char* fmt
, struct disc_time dt
);
203 /* read a fortune file */
204 int load_fortunes(char *fn
, char *delim
, char** result
);
206 struct disc_time
convert(int,int);
207 struct disc_time
makeday(int,int,int);
210 main (int argc
, char *argv
[]) {
214 struct disc_time hastur
;
215 char schwa
[23*17], *fnord
=0;
220 if ((p
= strrchr(progname
, '/')) != NULL
)
223 setlocale(LC_ALL
, "");
224 bindtextdomain(PACKAGE
, LOCALEDIR
);
229 for(pi
=1; pi
<argc
; pi
++) {
230 switch(argv
[pi
][0]) {
231 case '+': fnord
=argv
[pi
]+1; break;
233 switch(argv
[pi
][1]) {
235 printf(_("%s from %s version %s\n"), progname
, PACKAGE
, VERSION
);
244 int moe
=atoi(argv
[pi
]), larry
=atoi(argv
[pi
+1]), curly
=atoi(argv
[pi
+2]);
252 if (hastur
.season
== -1) {
253 printf("Invalid date -- out of range\n");
256 fnord
=fnord
?fnord
:default_fmt
;
257 } else if (argc
!=pi
) {
259 fprintf(stderr
,_("usage: %s [+format] [day month year]\n"), argv
[0]);
264 bob
=eris
->tm_yday
; /* days since Jan 1. */
265 raw
=eris
->tm_year
; /* years since 1980 */
266 hastur
=convert(bob
,raw
);
267 fnord
=fnord
?fnord
:default_immediate_fmt
;
269 format(schwa
, fnord
, hastur
);
270 printf("%s\n", schwa
);
275 void format(char *buf
, const char* fmt
, struct disc_time dt
)
277 int tib_start
=-1, tib_end
=0;
278 int i
, fmtlen
=strlen(fmt
);
281 /* fprintf(stderr, "format(%p, \"%s\", dt)\n", buf, fmt);*/
283 /* first, find extents of St. Tib's Day area, if defined */
284 for(i
=0; i
<fmtlen
; i
++) {
291 if(tib_start
>0) tib_end
=i
+1;
294 case '{': tib_start
=i
; break;
295 case '}': tib_end
=i
+1; break;
300 /* now do the formatting */
303 for(i
=0; i
<fmtlen
; i
++) {
304 if((i
==tib_start
) && (dt
.day
==-1)) {
305 /* handle St. Tib's Day */
306 strcpy(bufptr
, _("St. Tib's Day")); bufptr
+= 13;
310 char *wibble
=0, snarf
[23];
312 case 'A': wibble
=day_long
[dt
.yday
%5]; break;
313 case 'a': wibble
=day_short
[dt
.yday
%5]; break;
314 case 'B': wibble
=season_long
[dt
.season
]; break;
315 case 'b': wibble
=season_short
[dt
.season
]; break;
316 case 'd': sprintf(snarf
, "%d", dt
.day
+1); wibble
=snarf
; break;
317 case 'e': sprintf(snarf
, "%d%s", dt
.day
+1, ending(dt
.day
+1));
319 case 'H': if(dt
.day
==4||dt
.day
==49)
320 wibble
=holyday
[dt
.season
][dt
.day
==49]; break;
321 case 'N': if(dt
.day
!=4&&dt
.day
!=49) goto eschaton
; break;
322 case 'n': *(bufptr
++)='\n'; break;
323 case 't': *(bufptr
++)='\t'; break;
325 case 'Y': sprintf(snarf
, "%d", dt
.year
); wibble
=snarf
; break;
326 case '.': wibble
=sel(excl
, sizeof(excl
)/sizeof(excl
[0]));
329 case 'X': sprintf(snarf
, "%d",
330 xday_countdown(dt
.yday
, dt
.year
));
331 wibble
= snarf
; break;
332 #endif /* KILL_BOB */
335 /* fprintf(stderr, "wibble = (%s)\n", wibble);*/
336 strcpy(bufptr
, wibble
); bufptr
+=strlen(wibble
);
339 *(bufptr
++) = fmt
[i
];
347 struct disc_time
makeday(int imonth
,int iday
,int iyear
) /*i for input */
349 struct disc_time funkychickens
;
351 int cal
[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
354 /* basic range checks */
355 if (imonth
< 1 || imonth
> 12) {
356 funkychickens
.season
= -1;
357 return funkychickens
;
359 if (iday
< 1 || iday
> cal
[imonth
-1]) {
360 if (!(imonth
== 2 && iday
== 29 && iyear
%4 == 0 &&
361 (iyear
%100 != 0 || iyear
%400 == 0))) {
362 funkychickens
.season
= -1;
363 return funkychickens
;
368 funkychickens
.year
= iyear
+1166;
369 while(imonth
>0) { dayspast
+=cal
[--imonth
]; }
370 funkychickens
.day
=dayspast
+iday
-1;
371 funkychickens
.season
=0;
372 if((funkychickens
.year
%4)==2) {
373 if (funkychickens
.day
==59 && iday
==29) funkychickens
.day
=-1;
375 funkychickens
.yday
=funkychickens
.day
;
376 /* note: EQUAL SIGN...hopefully that fixes it */
377 while(funkychickens
.day
>=73) {
378 funkychickens
.season
++;
379 funkychickens
.day
-=73;
381 return funkychickens
;
384 struct disc_time
convert(int nday
, int nyear
)
385 { struct disc_time funkychickens
;
387 funkychickens
.year
= nyear
+3066;
388 funkychickens
.day
=nday
;
389 funkychickens
.season
=0;
390 if ((funkychickens
.year
%4)==2)
391 {if (funkychickens
.day
==59)
392 funkychickens
.day
=-1;
393 else if (funkychickens
.day
>59)
394 funkychickens
.day
-=1;
396 funkychickens
.yday
=funkychickens
.day
;
397 while (funkychickens
.day
>=73)
398 { funkychickens
.season
++;
399 funkychickens
.day
-=73;
401 return funkychickens
;
407 /* Code for counting down to X-Day, X-Day being Cfn 40, 3164
409 * After `X-Day' passed without incident, the CoSG declared that it had
410 * got the year upside down --- X-Day is actually in 8661 AD rather than
413 * Thus, the True X-Day is Cfn 40, 9827.
417 int xday_countdown(int yday
, int year
) {
418 int r
=(185-yday
)+(((yday
<59)&&(leapp(year
)))?1:0);
419 while(year
<9827) r
+=(leapp(++year
)?366:365);
420 while(year
>9827) r
-=(leapp(year
--)?366:365);
This page took 0.064452 seconds and 5 git commands to generate.