libkal-0.9.0/ 40755 764 144 0 6552475210 11736 5ustar tomasekuserslibkal-0.9.0/gtk-example/ 40755 764 144 0 6552474563 14166 5ustar tomasekuserslibkal-0.9.0/gtk-example/Makefile100644 764 144 423 6552371756 15702 0ustar tomasekusers LIBS=-L/usr/X11/lib -L../ -lgtk -lgdk -lglib -lXt -lXext -lX11 -lkal INCL=-I../ #-lICE CC=gcc #OPT=-O6 OPT=-g ################## # OO=$(OPT) $(LIBS) example: example.c utilz.c ../libkal.a $(CC) $(OPT) example.c $(INCL) $(LIBS) -o $@ clean: rm -f *~ *.o core example libkal-0.9.0/gtk-example/example.c100644 764 144 14315 6552475105 16077 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* * An X-windows example for the 'libkal' library. * (Uses the gtk library.) * */ #include #include #include "utilz.c" long jda; /* The jd of (some) day of the displayed month */ int sys; /* Calendary system */ GtkWidget *kalendar; GtkWidget *labl [7][6]; GtkWidget *frame; static GtkWidget * radio[KAL_XXX]; char mes[50]; void destr (void) { gtk_exit(0); } void k_upd () { GtkWidget * kal; mesyc(mes,jda,sys); gtk_frame_set_label(GTK_FRAME(frame),mes); kalendar=tblka(kalendar,labl,jda,sys); } void nxt (void) { /* next month */ int d,m,y; int ms=12; if (sys==KAL_JEW) ms=13; /* jewish calendar has (sometimes) 13 months */ kal_conv_jd_xxx(jda,&d,&m,&y,sys); /* JEW: skip the leap month if not leap year */ if ((m==6) && (sys==KAL_JEW) && (!kal_jew_leap(y))) m++; if (m==ms) { y++; m=1; } else m++; jda=kal_conv_xxx_jd(d,m,y,sys); k_upd (); } void prv (void) { /* previous month */ int d,m,y; int ms=12; if (sys==KAL_JEW) ms=13; /* jewish calendar has (sometimes) 13 months */ kal_conv_jd_xxx(jda,&d,&m,&y,sys); /* JEW: skip the leap month if not leap year */ if ((m==8) && (sys==KAL_JEW) && (!kal_jew_leap(y))) m--; if (m==1) { y--; m=ms; } else m--; jda=kal_conv_xxx_jd(d,m,y,sys); k_upd (); } void nxty (void) { /* next year */ int d,m,r; kal_conv_jd_xxx(jda,&d,&m,&r,sys); r++; jda=kal_conv_xxx_jd(d,m,r,sys); k_upd (); } void prvy (void) { /* previous year */ int d,m,r; kal_conv_jd_xxx(jda,&d,&m,&r,sys); r--; jda=kal_conv_xxx_jd(d,m,r,sys); k_upd (); } void change_sys (void) { /* switch to another calendary system */ int i,j=0; for(i=0; iactive) j=i; if (j!=sys) { sys=j; k_upd (); } } GtkWidget * radios() { /* draw radio-buttons */ GtkWidget * table; GtkWidget * label; GSList * group; int i; // --------- label=gtk_label_new("Calendar :"); table=gtk_table_new(KAL_XXX+1,1,TRUE); gtk_table_attach_defaults(GTK_TABLE(table),label,0,1,0,1); gtk_widget_show(label); for(i=0; i table=gtk_table_new(4,1,TRUE); //----button button=gtk_button_new_with_label(" > "); gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,0,1); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(nxt),NULL); gtk_widget_show (button); //----button button=gtk_button_new_with_label(" < "); gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,1,2); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(prv),NULL); gtk_widget_show (button); //----button button=gtk_button_new_with_label(" >> "); gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,2,3); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(nxty),NULL); gtk_widget_show (button); //----button button=gtk_button_new_with_label(" << "); gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,3,4); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(prvy),NULL); gtk_widget_show (button); gtk_box_pack_start_defaults(GTK_BOX(box),table); gtk_widget_show (table); //----button button=gtk_button_new_with_label("O.K."); gtk_box_pack_start_defaults(GTK_BOX(box),button); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC (destr),NULL); gtk_widget_show (button); gtk_widget_show (box); gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC (destr), NULL); gtk_widget_show(window); gtk_main(); return 0; } libkal-0.9.0/gtk-example/utilz.c100644 764 144 6213 6552475114 15571 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* * An X-windows example for the 'libkal' library. * (Uses the gtk library.) * */ #include #include "kal.h" #include const char * days[]= { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" }; const char * mons[]= { "January","February","March","April","May", "June","July","August","September","October","November","December" }; char * month_name(int m, int sys) { if (kal_proc_valid(KAL_PROCS_MONTHS,sys)) return mons[m-1]; /* 'sys' does not have the 'kal_xxx_months' function */ else return kal_xxx_months(m,sys); } GtkWidget * dni () { GtkWidget * table; GtkWidget * label; int i; table=gtk_table_new(7,1,TRUE); for (i=1;i<=7;i++) { label=gtk_label_new(days[i-1]); gtk_table_attach_defaults(GTK_TABLE(table),label,0,1,i-1,i); gtk_widget_show(label); } return table; } void mesyc (char mes[50], long jd, int sys) { int d,m,y; int before; char * bf=""; kal_conv_jd_xxx(jd,&d,&m,&y,sys); y=kal_year_decode(y,&before); if (before) bf=" before..."; printf ("<%s>>%s %d%s\n", kal_proc_get_name(sys), month_name(m,sys),y,bf); fflush(stdout); sprintf (mes,"%s %d%s", month_name(m,sys),y,bf); } GtkWidget * tblka (GtkWidget *tbla, GtkWidget *label[7][6],long jd,int sys) { long jd1,jd2,jdd; int i,j; char num[10]; GtkWidget * table; int day,month,year,m,y; int cols; kal_conv_jd_xxx(jd,&day,&month,&year,sys); jd2=kal_conv_xxx_jd(1,month+1,year,sys); jdd=kal_conv_xxx_jd(1,month,year,sys); jd1=kal_prev_dw(kal_conv_xxx_jd(1,month,year,sys),0); /* nearest (previous) Sunday to the beginning of the month */ if (tbla==NULL) { table=gtk_table_new(7,6,TRUE); for(i=1; i<=7; i++) for (j=1; j<=6; j++) { label[i-1][j-1]=gtk_label_new(" "); gtk_table_attach_defaults(GTK_TABLE(table),label[i-1][j-1],j-1,j,i-1,i); gtk_widget_show(label[i-1][j-1]); } } else { table=tbla; for(i=1; i<=7; i++) for (j=1; j<=6; j++) { gtk_widget_hide (label[i-1][j-1]); gtk_label_set (GTK_LABEL(label[i-1][j-1])," "); gtk_widget_show(label[i-1][j-1]); } } for (i=1; i<=7; i++) { j=0; for(jdd=jd1+i-1;jdd Copyright (C) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA. Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. , 1 April 1990 Ty Coon, President of Vice That's all there is to it! libkal-0.9.0/Makefile100644 764 144 1366 6552473632 13507 0ustar tomasekusers CC=gcc AR=ar RANLIB=ranlib #OPT=-O2 OPT=-g -Wall AROPT=rc LIBS=-L./ -lkal ######################################################################### all:tests libkal.a tests: check test test1 test-dw test-dw: test-dw.c libkal.a $(CC) $(OPT) test-dw.c libkal.a -o $@ $(LIBS) test1: test1.c libkal.a $(CC) $(OPT) test1.c libkal.a -o $@ $(LIBS) test: test.c libkal.a $(CC) $(OPT) test.c libkal.a -o $@ $(LIBS) check: check.c libkal.a $(CC) $(OPT) check.c libkal.a -o $@ $(LIBS) libkal.a: kal_main.o kal_getdate.o kal_names.o kal_check.o kal_procs.o rm -f $@ $(AR) $(AROPT) $@ kal_main.o kal_getdate.o kal_names.o kal_check.o kal_procs.o $(RANLIB) $@ clean: rm -f *.o *~ core check test test1 libkal.a test-dw libkal-0.9.0/README100644 764 144 17316 6552473632 12751 0ustar tomasekusers[Sorry for my english, it's not very good. I hope, this will be understandable enough, thought.] INTRODUCTION The 'libkal' library provides support for converting dates between various calendary systems (currently only julianic, gregorianic, arabic and jewish ones) and some more related stuff. The basic idea of this library is, that any date in any calendar system can be converted to a single number, which express the number of days since some fixed date in the past. I use here so-called 'julianic date'(jd), that is used in Astronomy (not exactly). Therefore it's easy to compute for example 'how many days are there between x and y' - just compute the jd's of x and y, and substract them: difference=kal_conv_gre_jd(y_day,y_month,y_year)- kal_conv_gre_jd(x_day,x_month,x_year); Also evaluating the day of week of given date is quite easy - just divide the jd by 7 and the reminder is what you seek. (not exactly) LIBRARY OVERVIEW 1. Basic conversions -------------------- Each calendary system has two basic functions, which convert the date to and from the jd: long kal_conv_*_jd(int d, int m, int y); void kal_conv_jd_*(long jd, int *d, int *m, int *y); ,where * can be one of: jul,gre,ar,jew for julianic, gregorianic, arabic and jewish calendary system respectively. (e.g.: "kal_conv_gre_jd(day,month,year)" computes the jd of day/month/year according to the gregorian calendar.) (See the test.c example.) 2. Checking if date valid ------------------------- When converting a date into the jd, we would like to know, whether the date is valid. This is done in the folowing function: int kal_*_check(long *jd, int d, int m, int y); (simillar to the previous). It returns 0 on succes and 1 when the date is invalid. The check is done in two steps: first checking for 'big nonsenses' such as the month number bigger than 13 (jewish calendar has sometimes 13 months) or lower than 1; second, we convert the date into jd, and then back into d/m/y and check, whether the d/m/y are equal. Thus we have here as a parameter the pointer on the jd, as we need not to do the same job (converting d/m/y to jd) later again. (See also the test1.c example.) 3. Computing the day of easter sunday ------------------------------------- This functions compute the jd of the easter sunday for given year: long kal_jul_east(int y); respectivelly: long kal_gre_east(int y); (Dates of many christian feasts are evaluated from this date). 4. Days of week --------------- A day of week can be simply obtained from jd by calling: int kal_day_of_week(long jd); ,where 0 stands for sunday, 1==monday, 2==tuesday, etc. Another functions give you the closest (next or previous) 'day of week': long kal_next_dw(long jd, int dw); long kal_prev_dw(long jd, int dw); For exapmle: "jd1=kal_next_dw(jd,0);" will make the jd1 to be the nearest sunday(==0) _after_ the jd, or to be equal to jd, if jd is a sunday. There exist also two simillar functions, that differs in that the result is never equal to the inputed jd. long kal_next_after_dw(long jd, int dw); long kal_prev_before_dw(long jd, int dw); (See also the 'test-dw.c' example.) 5. Names of months ------------------ I suppose that the names of the months are language dependand, so I don't cover them in this library. The only exeptions are the "exotic" calendars. You can get that names by calling: char * kal_jew_months(int m); char * kal_ar_months(int m); 6. Some notes on the Jewish calendar ------------------------------------ Jewish calendar is probably the most complicated calendary system ever existed. It's so-called 'luni-solar' calendar, which means, that it's 'lunar' calendar, i.e. that the 'calendar month' equals to the 'lunar month', but this is adjusted so, that every month remains in it's season. In order to do that, jews have 7 times in 19 years 13 months per year. But for historical reasons, this 'leap-month' isn't added at the end of the year, but after the sixth month. (Which was in the past the end of the year.) This makes it ugly for computing- either would the relation between month number and month name be not stable (it would be dependand on whether the year is leap or not), or would the number of the month be not trully it's number. I choose the second possibility, so that the month no. 7 may be used in leap years only. Therefore it might be usefull to know, whether the year is leap, or not. You may use 'int kal_jew_leap(int y)' function, which returns 1, if the year is leap. (See functions 'prv' and 'nxt' in gtk-example/example.c) 7. Year adjustments ------------------- It could happen, that we will do something with the years 'before [Christ | creation | Hegira ]'. In such a case it's not nice to output to user something like "Year -2", because it can be very confusing ( year "-2" is the "year 3 before" !). For that case ther are in this library two functions, which convert to and from "user friendly" form, having beside the year number a boolean variable "a", which tells, whether the year is "before" or not: int kal_year_code(int y, int a); /* from 'user friendly' form */ int kal_year_decode(int y, int *a); /* to 'user friendly' form */ (See also the 'gtk-example'.) 8. Getting current date ----------------------- The funcion kal_getdate() gets the jd of today's date. (Needs to be rewriten!) 9. More general function interface. ----------------------------------- It can happen, that you will need general functions, to be independant on what calendar is beeing used. I Implemented here a simple table, where basic routines for each calendar are stored and general functions, that can call them. These functions are: long kal_conv_xxx_jd(int d, int m, int y, int sys); void kal_conv_jd_xxx(long &jd, int d, int y, int sys); long kal_xxx_east(int y, int sys); char * kal_xxx_months(int m, int sys); ,where sys can be one of the following: KAL_JUL, KAL_GRE, KAL_AR, KAL_JEW for the julianic, gregorianic .... calendary system. Note, that not all of the functions must be defined for all calendars. (It is, for example, nonsense to compute the date od Easter Sunday for jewish, or arabic calendar - but not for gregorian or julian ones - and for example also not for the coptic one [which is not implemented yet;-)].) In order to test if the function is available, call: int kal_proc_valid(int proc, int sys); ,where 'proc' can be: KAL_PROCS_XXX_JD, KAL_PROCS_JD_XXX, KAL_PROCS_MONTHS or KAL_PROCS_EAST. It results 0 on succes, 1 if the function doesn't exist. You can also get the abbreviation for the calendar by calling: char * kal_proc_get_name(int sys); (For more see the 'gtk-example'.) TESTING The library is not very well tested, althought something was done: - I checked all the calendars with the 'check' utility for things that can be checked without having to have any chronological tables or calendars. (For more see check.c) - I have (randomly) checked the library against some known-to-work programs. (for example the 'xjewcal'). - I have (randomly) checked the 'jul', 'gre' and 'jew' calendars agains chronological tables and calendars I've found. I've been using this routines for about 2 years (since 1996), so there is a chance, that they might be correct. Expecially the dates between 1990- 2000 should be o.k. Please, if You could test this library in any way, do so. Thanks. ADDING NEW CALENDARS If someone has any other calendar to be added to this library, should: * Add the kal_conv_*_jd and kal_conv_jd_* functions to the (possible) kal_main.c . * Add other functions there. * Update the kal_proc.h. * Test the library with 'check'. * Test it more using chronological tables etc. AUTHOR Petr Tomasek July 1998 libkal-0.9.0/TODO100644 764 144 1632 6552473633 12534 0ustar tomasekusers What needs to be done: * more extensive testing. (Does anybody have some chronological tables ?) * adding new calendars: Mayan - could be quite easy. Egyptian, Coptic, Armenian - very nice calendars; have 12 months - each 30 days + 5 days extra at the end/ beginning of the year. Roman - the same as the julianic, but 754 years 'older'. The Calendar of the french revolution - seems to be not soo much easy. Indian, Japanese, Chinese... - I have no idea :-( * debugging the turkish calendar. * some implementation for feasts. (but this is VERY complicated if we want to be as much general as possible). * rewriting of kal_getdate() so that it doesn't need to exec the "date" command. * adding 'kal_xxx_year_info', that would compute the characteristic for given year (such as 'the golden number', 'epact', number of days in the year etc.) * making it into a shared library. libkal-0.9.0/check.c100644 764 144 5703 6552474271 13267 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* * Basic checks for the libkal library. * */ #include "kal.h" #include long jd,jd1; int d,m,y; int dd,mm,yy; long errs; void help(void) { int i; printf( "Tester of the libkal library. (c) 1998 Petr Tomasek.\n" "Usage: check \n\n" ",where can be one of:"); for (i=0;i error ! */ else { if ((m!=mm+1) || (y!=yy)) /* simmillar for months */ { if (m!=1) { /* workaround for the jewish calendar */ if ((sys!=KAL_JEW) || (m!=8) || (mm!=6) || (kal_jew_leap(y))) test2er(); /* error ! */ } else if (y!=yy+1) test2er(); } } } dd=d;mm=m;yy=y; } printf("\n\n %ld errors found.\n",errs); } libkal-0.9.0/kal.h100644 764 144 1770 6552473634 12770 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #ifndef _KAL_H__ #define _KAL_H__ #include "kal_main.h" #include "kal_procs.h" #include "kal_getdate.h" #include "kal_names.h" #include "kal_check.h" #endif libkal-0.9.0/kal_check.c100644 764 144 4110 6552473634 14107 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* * This provides checks, whether given date is valid. * Note, that this already converts the date to the jd, so that * you need not to convert it again. */ #include "kal_main.h" #include "kal_procs.h" int kal_general_check(int d,int m,int y) { if ((d<1) || (d>32)) return 1; /* does anybody know about calendar system, that can have more than 31 days per month ? */ if ((m<0) || (m>13)) return 1; /* does anybody know about calendar system, that can have more then 13 months per year ? */ return 0; } /* * kal_xxx_check */ int kal_xxx_check(long *jd, int d, int m, int y, int sys) { int dd,mm,yy; if (kal_general_check(d,m,y)) return 1; *jd=kal_conv_xxx_jd(d,m,y,sys); kal_conv_jd_xxx(*jd,&dd,&mm,&yy,sys); if ((d!=dd) || (m!=mm) || (y!=yy)) return 1; return 0; } /* julianic */ int kal_jul_check(long *jd, int d, int m, int y) { return kal_xxx_check (jd, d, m, y, KAL_JUL); } /* gregorianic */ int kal_gre_check(long *jd, int d, int m, int y) { return kal_xxx_check (jd, d, m, y, KAL_GRE); } /* arabic */ int kal_ar_check(long *jd, int d, int m, int y) { return kal_xxx_check (jd, d, m, y, KAL_AR); } /* jewish */ int kal_jew_check(long *jd, int d, int m, int y) { return kal_xxx_check (jd, d, m, y, KAL_JEW); } libkal-0.9.0/kal_check.h100644 764 144 2274 6552473635 14126 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #ifndef _KAL_CHECK_H__ #define _KAL_CHECK_H__ int kal_general_check(int d,int m,int y); int kal_xxx_check(long *jd, int d, int m, int y, int sys); int kal_jul_check(long *jd, int d, int m, int y); int kal_gre_check(long *jd, int d, int m, int y); int kal_ar_check(long *jd, int d, int m, int y); int kal_jew_check(long *jd, int d, int m, int y); #endif libkal-0.9.0/kal_getdate.c100644 764 144 2426 6552473635 14460 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include /* * Get current date (jd) * * FIXME : Is it possible to do this without having to exec 'date' ? * */ long kal_getdate () { FILE * fil; char str[100]; int day,month,year; fil=popen("date +%d%n%m%n%Y%n%H%n%M%n%S%n%w%n%j%n%Z%n","r"); fscanf(fil,"%s",str);day=atoi(str); fscanf(fil,"%s",str);month=atoi(str); fscanf(fil,"%s",str);year=atoi(str); pclose(fil); return kal_conv_gre_jd(day,month,year); } libkal-0.9.0/kal_getdate.h100644 764 144 1755 6552473635 14471 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #ifndef _KAL_GETDATE_H__ #define _KAL_GETDATE_H__ #include /* Gets today's date and converts it into jd. */ long kal_getdate (); #endif libkal-0.9.0/kal_main.c100644 764 144 15375 6552473636 14017 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* * Divide/modulo. */ long kal_dv(long a, long b) { long c; c=a/b; if (c<=0) if (b*c>a) c--; return c; } long kal_md(long a, long b) { return a-b*kal_dv(a,b); } /* Just not to have to type a lot. Internal. */ #define dv(a,b) kal_dv(a,b) #define md(a,b) kal_md(a,b) /* * Internal. */ int s(int m, int p) { const int t[]={0,31,59,90,120,151,181,212,243,273,304,334}; int b; if (m==13) return 365; else { m=md(m-1,12)+1; /* for stability reasons */ b=t[m-1]; if (m<3) b=b-p; return b; } } /* * Julianic calendar. */ long kal_conv_jul_jd(int d,int m,int y) { long p1,p2,q; p1=dv(y,4); p2=md(y,4); q=0;if (p2==0) q=1; return d+s(m,q)+1461*p1+365*p2+1721058; } void kal_conv_jd_jul(long jd,int *d,int *m,int *y) { long p1,p2,q1,q2,q; p1=dv(jd-1721058,1461); q1=md(jd-1721058,1461); if (q1==0) {*d=1; *m=1; *y=4*p1; } else { p2=dv(q1-1,365); q2=md(q1-1,365); *y=4*p1+p2; q=0; if (md(*y,4)==0) q=1; *m=1; while ((*m<12) && ((q2+1)>s(*m+1,q))) *m=*m+1;; *d=q2+1-s(*m,q); } } long kal_jul_east(int y) { /* Computes jd of easter sunday for given year. */ long p1,p2,q,ep,d; ep=md(11*md(y,19)+14,30); p1=dv(y,4); p2=md(y,4); q=0; if (p2==0) q=1; d=50-ep+s(3,q)+1461*p1+365*p2+1721058; return d+7-md(d+1,7); } /* * Gregorianic calendar. */ long kal_conv_gre_jd(int d,int m,int y) { long m1,m2,n1,n2,q; m1=dv(y,400); m2=dv(y,100); n1=dv(y,4); n2=md(y,4); q=0; if (n2==0) q=1; if (md(y,100)==0) q=0; if (md(y,400)==0) q=1; return d+s(m,q)+m1-m2+1461*n1+365*n2+1721060; } void kal_conv_jd_gre(long jd,int *d,int *m,int *y) { long qq,a1,a2,b1,b2,c1,c2,d1,d2,q; qq=jd-1721060; a1=dv(qq,146097); a2=md(qq,146097); b1=dv(a2,36524); b2=md(a2,36524); c1=dv(b2,1461); c2=md(b2,1461); if ((c2==0) && ((b2>0) || (a2==0)) ) { *d=1; *m=1; *y=400*a1+100*b1+4*c1; } else { d1=dv(c2-1,365); d2=md(c2-1,365); *y=400*a1+100*b1+4*c1+d1; q=0; if (md(*y,4)==0) q=1; if (md(*y,100)==0) q=0; if (md(*y,400)==0) q=1; *m=1; while ((*m<12) && ((d2+1)>s(*m+1,q))) *m=*m+1; *d=d2+1-s(*m,q); } } long kal_gre_east(int y) { /* Computes jd of easter sunday for given year. */ long p1,p2,q,ep,a,g,d,m1,m2; a=md(y,19); m1=dv(y,400); m2=dv(y,100); g=md(dv(y,300)+m1-m2+14,30); ep=md(11*a+g,30); if (ep==0) ep=1; if ((ep==1) && (a>10)) ep=2; p1=dv(y,4); p2=md(y,4); q=0; if (p2==0) q=1; d=50-ep+s(3,q)+m1-m2+1461*p1+365*p2+1721060; return d+7-md(d+1,7); } /* * Arabic calendar. */ long kal_conv_ar_jd(int d,int m,int h) { long h1,h2; h1=dv(h-1,30); h2=md(h-1,30); return d+29*(m-1)+dv(m,2)+10631*h1+354*h2+dv(7*h2+8,19)+1948439; } void kal_conv_jd_ar(long jd,int *d,int *m,int *h) { long h1,h2,q1,q2; h1=dv(jd-1948440,10631); q1=md(jd-1948440,10631); h2=-1; do h2++; while (354*h2+dv(7*h2+8,19)<=q1); h2--; *h=30*h1+h2+1; q2=q1-(354*h2+dv(7*h2+8,19)); *m=0; do *m=*m+1; while (29*(*m-1)+dv(*m,2)<=q2); *m=*m-1; *d=q2-(29*(*m-1)+dv(*m,2))+1; } /* * Turkish calendar */ /* * THIS IS (afaik) BUGGY !!!!! * long kal_conv_tur_jd(int d,int m,int h) { long h1,h2; h1=dv(h-1,8); h2=md(h-1,8); return d+29*(m-1)+dv(m,2)+2835*h1+354*h2+dv(3*h2+1,8)+1948440; } void kal_conv_jd_tur(long jd,int *d,int *m,int *h) { long h1,h2,q1,q2; h1=dv(jd-1948441,2835); q1=md(jd-1948441,2835); h2=-1; do h2++; while (354*h2+dv(3*h2+1,8)<=q1); h2--; *h=8*h1+h2+1; q2=q1-(354*h2+dv(3*h2+1,8)); *m=0; do *m=*m+1; while (29*(*m-1)+dv(*m,2)<=q2); *m=*m-1; *d=q2-(29*(*m-1)+dv(*m,2))+1; } * * */ /* * Jewish calendar. */ int prest(int w2) { int q=0; if (w2>0) q=dv(7*w2+1,19)-dv(7*w2-6,19); return q; } int uuw(int u2,long u3,int w2,int y) { int uu=0; switch (u2) { case 2: case 4: case 6: uu=1; break; case 1: if ((u3>=16404) && (prest(w2)==0)) uu=2; break; case 0: if ((u3>=23269) && (prest(md(y-2,19))==1)) uu=1; break; } return uu; } long kal_jew_rosh_hashana(int y) { long w1,w2,u2,w,uu,u1,u3; w1=dv(y-1,19); w2=md(y-1,19); w=235*w1+12*w2+dv(7*w2+1,19); u3=md(13753*w+12084,25920); uu=29*w+dv(13753*w+12084,25920); u2=md(uu,7); u1=dv(uu,7); return 347998+7*u1+u2+uuw(u2,u3,w2,y); } int z_s(int m,int p,int q) { int ss; ss=(m-1)*29; if ((p==0) && (m>7)) ss=ss-29; if (m>6) ss=ss+3+dv(m-7,2); else ss=ss+dv(m,2); if ((p==1) && (m>6)) ss++; if ((q==0) && (m>3)) ss--; if ((q==2) && (m>2)) ss++; return ss; } long kal_conv_jew_jd(int d, int m, int y) { long j1,j2; int j,p,q; j1=kal_jew_rosh_hashana(y); j2=kal_jew_rosh_hashana(y+1); j=j2-j1; if (j>380) { p=1; q=j-383; } else { p=0; q=j-353; } return j1+z_s(m,p,q)+d-1; } void kal_conv_jd_jew(long jd,int *d, int *m, int *y) { long w1,w2,ww,jd1,jd2; int j,p,q; /* ww=(long)((float)((jd-347998)/29.5305941)); */ ww=(((jd-347998)/29.5305941)); if (ww<0) ww--; w1=dv(ww,235); w2=dv(md(ww,235),12); *y=19*w1+w2-1; jd2=kal_jew_rosh_hashana(*y); do {jd1=jd2; *y=*y+1; jd2=kal_jew_rosh_hashana(*y); } while (jd2<=jd); *y=*y-1; j=jd2-jd1; if (j>380) {p=1; q=j-383;} else {p=0; q=j-353;} j=jd-jd1;*m=0; do {*m=*m+1; } while (j>=z_s(*m,p,q)); *m=*m-1; *d=j-z_s(*m,p,q)+1; } /* is the year leap? */ int kal_jew_leap( int y) { return ((kal_jew_rosh_hashana(y+1)- /* Is the number of days */ kal_jew_rosh_hashana(y))>360); /* in the year > 360 ? */ } /* * Days of week */ int kal_day_of_week(long jd) { return md(jd+1,7); } long kal_prev_dw (long jd, int dw) { return (7*dv(jd+1-dw,7)+dw-1); } long kal_prev_before_dw (long jd, int dw) { return (7*dv(jd-dw,7)+dw-1); } long kal_next_dw (long jd, int dw) { return (dw-1-7*dv(dw-1-jd,7)); } long kal_next_after_dw (long jd, int dw) { return (dw-1-7*dv(dw-2-jd,7)); } /* * Year adjustments. * */ int kal_year_code(int y,int a) { if (a) return 1-y; else return y; } int kal_year_decode(int y, int *a) { if (y<1) { *a=1; return 1-y;} else { *a=0; return y;} } libkal-0.9.0/kal_main.h100644 764 144 4440 6552473636 13773 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #ifndef _KAL_MAIN_H__ #define _KAL_MAIN_H__ /* * divide/modulo */ long kal_dv(long a, long b); long kal_md(long a, long b); /* * Julianic calendar */ long kal_conv_jul_jd(int d,int m, int y); void kal_conv_jd_jul(long jd,int *d,int *m,int *y); long kal_jul_east(int y); /* * Gregorianic calendar */ long kal_conv_gre_jd(int d,int m,int y); void kal_conv_jd_gre(long jd,int *d,int *m,int *y); long kal_gre_east(int y); /* * Arabic calendar * ("h" means the arabic year, "The Year of Hegira".) */ long kal_conv_ar_jd(int d,int m,int h); void kal_conv_jd_ar(long jd,int *d,int *m,int *h); /* * Turkish calendar */ /* ** Currently buggy and unusable ** long kal_conv_tur_jd (int d, int m, int h); void kal_conv_jd_tur(long jd,int *d,int *m,int *h); */ /* * Jewish calendar */ long kal_jew_rosh_hashana(int y); /* Get's the jd of the first day of given year (so-called "Rosh Hashana"). Used by other routines. */ long kal_conv_jew_jd(int d, int m, int y); void kal_conv_jd_jew(long jd,int *d,int *m, int *y); int kal_jew_leap(int y); /* is the year leap? */ /* * Get the day of week of given jd, etc. [0=sun,1=mon,2=tue..] */ int kal_day_of_week(long jd); long kal_next_dw (long jd, int dw); long kal_next_after_dw (long jd, int dw); long kal_prev_dw (long jd, int dw); long kal_prev_before_dw (long jd, int dw); /* * Year adjustments. */ int kal_year_code(int y,int a); int kal_year_decode(int y, int *a); #endif libkal-0.9.0/kal_names.c100644 764 144 2501 6552473637 14142 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* * NOTE : The transscription may vary !! * */ char * kal_jew_months(int m) { const char *mm[]= {"Tishri","Marcheshwan","Kislew","Tebet","Shebat","Adar", "Adar sheni","Nisan","Ijjar","Sivan","Tamuz","Ab","Elul" }; return (char *)mm[m-1]; } char * kal_ar_months(int m) { const char *mm[]= {"Muharram","Safar","Rabi' al-awwal","Rabi' ath-thani", "Jumada-l-ula","Jumada-l-ahira","Rajab","Sha'ban","Ramadan","Shauwal", "Dhu-l-qada", "Dhu-l-hijja" }; return (char *)mm[m-1]; } libkal-0.9.0/kal_names.h100644 764 144 1712 6552473637 14152 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #ifndef _KAL_NAMES_H__ #define _KAL_NAMES_H__ char * kal_jew_months(int m); char * kal_ar_months(int m); #endif libkal-0.9.0/kal_procs.c100644 764 144 3254 6552473637 14173 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include "kal_procs.h" #include "kal_main.h" /* * Checks whether proc valid. Returns 0 on succes. */ int kal_proc_valid(int proc, int sys) { return (!kal_procs[sys][proc]); } /* * Get name of the calendar. */ char * kal_proc_get_name(int sys) { return (char *)kal_procs[sys][KAL_PROCS_NAME]; } /* * Basic conversion routines. */ long kal_conv_xxx_jd(int d, int m, int y, int sys) { long (* fce)(); fce=kal_procs[sys][KAL_PROCS_XXX_JD]; return (* fce)(d,m,y); } void kal_conv_jd_xxx(long jd, int *d, int *m, int *y, int sys) { void (* fce)(); fce=kal_procs[sys][KAL_PROCS_JD_XXX]; (* fce)(jd,d,m,y); } long kal_xxx_east(int y, int sys) { long (* fce)(); fce=kal_procs[sys][KAL_PROCS_EAST]; return (* fce)(y); } char * kal_xxx_months(int m, int sys) { char * (* fce)(); fce=kal_procs[sys][KAL_PROCS_MONTHS]; return (* fce)(m); } libkal-0.9.0/kal_procs.h100644 764 144 4760 6552473640 14175 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #ifndef _KAL_PROCS_H__ #define _KAL_PROCS_H__ #include "kal_main.h" #include "kal_names.h" #ifndef NULL #define NULL ((void *) 0) #endif /* * Numbers of calendary systems. */ #define KAL_JUL 0 #define KAL_GRE 1 #define KAL_AR 2 /* #define KAL_TUR 3 */ #define KAL_JEW 3 #define KAL_XXX 4 /* How many calednars do we know ? */ /* * Procedure table. When adding a new calendary system, add a new * entry here. */ #define KAL_PROCS_NAME 0 #define KAL_PROCS_XXX_JD 1 #define KAL_PROCS_JD_XXX 2 #define KAL_PROCS_YEAR_INFO 3 #define KAL_PROCS_MONTHS 4 #define KAL_PROCS_EAST 5 static const void * kal_procs[][6] = { { (void *) "jul", /* Julianic */ kal_conv_jul_jd, /* kal_conv_xxx_jd */ kal_conv_jd_jul, /* kal_conv_jd_xxx */ NULL, /* kal_xxx_year_info (NOT IMPLEMENTED YET) */ NULL, /* kal_xxx_months */ &kal_jul_east }, /* kal_xxx_east */ { (void *) "gre", /* Gregorianic */ kal_conv_gre_jd, kal_conv_jd_gre, NULL, NULL, kal_jul_east }, { (void *) "ar", /* Arabic */ kal_conv_ar_jd, kal_conv_jd_ar, NULL, kal_ar_months, NULL }, /* { (void *) "tur", * Turkish * (NOT IMPLEMENTED) kal_conv_tur_jd, kal_conv_jd_tur, NULL, kal_ar_months, NULL }, */ { (void *) "jew", /* Jewish */ kal_conv_jew_jd, kal_conv_jd_jew, NULL, kal_jew_months, NULL, } }; /* * Procedures in general form. * */ int kal_proc_valid(int proc, int sys); char * kal_proc_get_name(int sys); long kal_conv_xxx_jd(int d, int m, int y, int sys); void kal_conv_jd_xxx(long jd, int *d, int *m, int *y, int sys); long kal_xxx_east(int y, int sys); char * kal_xxx_months(int m, int sys); #endif libkal-0.9.0/test-dw.c100644 764 144 3512 6552473640 13574 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* * An example for day-of-week manipulation. */ #include "kal.h" void main() { const char * dw[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; long jd,jd1; int i,j; int d,m,y; jd=kal_getdate(); /* get current date */ for (j=0; j<=6; j++) for (i=0; i<=19; i++) { jd1=jd+i; if (!i) printf ("\n --->%s<--\n \t |\t prev\t\t prev_before\t next\t\t next_after\n" "============+===============================================================\n",dw[j]); kal_conv_jd_gre(jd1,&d,&m,&y); printf(" %s %d/%d |",dw[kal_day_of_week(jd1)],d,m); jd1=kal_prev_dw(jd1,j); kal_conv_jd_gre(jd1,&d,&m,&y); printf("\t %s %d/%d",dw[kal_day_of_week(jd1)],d,m); jd1=kal_prev_before_dw(jd+i,j); kal_conv_jd_gre(jd1,&d,&m,&y); printf("\t %s %d/%d",dw[kal_day_of_week(jd1)],d,m); jd1=kal_next_dw(jd+i,j); kal_conv_jd_gre(jd1,&d,&m,&y); printf("\t %s %d/%d",dw[kal_day_of_week(jd1)],d,m); jd1=kal_next_after_dw(jd+i,j); kal_conv_jd_gre(jd1,&d,&m,&y); printf("\t %s %d/%d\n",dw[kal_day_of_week(jd1)],d,m); } } libkal-0.9.0/test.c100644 764 144 6301 6552473640 13163 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* * A (very) simple example. */ #include "kal.h" const char * dw[]= {"Sunday","Monday","Tuestday","Wendsday","Thurstday","Friday","Saturday"}; void get_date(int *d, int *m, int *y) { printf("Day:"); scanf("%d",d); printf("Month:"); scanf("%d",m); printf("Year:"); scanf("%d",y); } int whatdate() { char in[100]; while (1) { printf("\n\nSelect what kind of date You want to enter " "[jul,gre,ar,jew,today]:\n"); scanf("%s",in); if (!strcmp(in,"jul")) return 0; if (!strcmp(in,"gre")) return 1; if (!strcmp(in,"ar")) return 2; if (!strcmp(in,"jew")) return 3; if (!strcmp(in,"today")) return 4; if (!strcmp(in,"quit")) exit(0); if (!strcmp(in,"exit")) exit(0); if (!strcmp(in,"x")) exit(0); if (!strcmp(in,"q")) exit(0); printf("Invalid choice !\n"); } } void main() { int d,m,y; long jd,jd1; int choice; /* * Input: */ if ((choice=whatdate())<4) { printf("\nInput a date in the "); switch (choice) { case 0: printf("julianic system:\n"); break; case 1: printf("gregorianic system:\n"); break; case 2: printf("arabic system:\n"); break; case 3: printf("jewish system:\n"); printf(" (NOTE that the 8th month is always 'Nisan' here,\n" " even if the year has only 12 months!)\n"); break; } get_date(&d,&m,&y); switch (choice) { case 0: jd=kal_conv_jul_jd(d,m,y); break; case 1: jd=kal_conv_gre_jd(d,m,y); break; case 2: jd=kal_conv_ar_jd(d,m,y); break; case 3: jd=kal_conv_jew_jd(d,m,y); break; } } else { jd=kal_getdate(); /* get today's date */ } /* * Output : */ printf("\n===============================================================================\n"); printf("Julianic date jd=%ld\t\tDay of week: %s\n",jd,dw[kal_day_of_week(jd)]); printf("===============================================================================\n\n"); kal_conv_jd_jul(jd,&d,&m,&y); printf("Date in Julianic system: %d.%d.%d\n",d,m,y); jd1=kal_jul_east(y); kal_conv_jd_jul(jd1,&d,&m,&y); printf("(Easter sunday this year: %d.%d.%d)\n\n",d,m,y); kal_conv_jd_gre(jd,&d,&m,&y); printf("Date in Gregorianic system: %d.%d.%d\n",d,m,y); jd1=kal_gre_east(y); kal_conv_jd_gre(jd1,&d,&m,&y); printf("(Easter sunday this year: %d.%d.%d)\n\n",d,m,y); kal_conv_jd_ar(jd,&d,&m,&y); printf("Date in Arabic system: %d. %s %d\n\n",d,kal_ar_months(m),y); kal_conv_jd_jew(jd,&d,&m,&y); printf("Date in Jewish system: %d. %s %d\n\n",d,kal_jew_months(m),y); } libkal-0.9.0/test1.c100644 764 144 6430 6552473641 13250 0ustar tomasekusers/* The 'libkal' library for date conversion. * Copyright (C) 1996-1998 Petr Tomasek * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* * A (very) simple example II. */ #include "kal.h" const char * dw[]= {"Sunday","Monday","Tuestday","Wendsday","Thurstday","Friday","Saturday"}; void get_date(int *d, int *m, int *y) { printf("Day:"); scanf("%d",d); printf("Month:"); scanf("%d",m); printf("Year:"); scanf("%d",y); } int whatdate() { char in[100]; while (1) { printf("\n\nSelect what kind of date You want to enter " "[jul, gre, ar, jew, today]:\n"); scanf("%s",in); if (!strcmp(in,"jul")) return 0; if (!strcmp(in,"gre")) return 1; if (!strcmp(in,"ar")) return 2; if (!strcmp(in,"jew")) return 3; if (!strcmp(in,"today")) return 4; if (!strcmp(in,"quit")) exit(0); if (!strcmp(in,"exit")) exit(0); if (!strcmp(in,"x")) exit(0); if (!strcmp(in,"q")) exit(0); printf("Invalid choice !\n"); } } void main() { int d,m,y; long jd,jd1; int choice; /* * Input: */ if ((choice=whatdate())<4) { printf("\nInput a date in the "); switch (choice) { case 0: printf("julianic system:\n"); break; case 1: printf("gregorianic system:\n"); break; case 2: printf("arabic system:\n"); break; case 3: printf("jewish system:\n"); printf(" (NOTE that the 8th month is always 'Nisan' here,\n" " even if the year has only 12 months!)\n"); break; } get_date(&d,&m,&y); } if (choice>3) { jd=kal_getdate(); /* get today's date */ } else /* Now , how easy: The "kal_xxx_check" function : 1) checks if the date is valid. 2) converts it from the 'choice' calendar format [see kal_procs.h] to jd. */ if (kal_xxx_check(&jd, d, m, y,choice)) { printf("\n Invalid date !\n"); exit(-1); } /* * Output : */ printf("\n===============================================================================\n"); printf("Julianic date jd=%ld\t\tDay of week: %s\n",jd,dw[kal_day_of_week(jd)]); printf("===============================================================================\n\n"); kal_conv_jd_jul(jd,&d,&m,&y); printf("Date in Julianic system: %d.%d.%d\n",d,m,y); jd1=kal_jul_east(y); kal_conv_jd_jul(jd1,&d,&m,&y); printf("(Easter sunday this year: %d.%d.%d)\n\n",d,m,y); kal_conv_jd_gre(jd,&d,&m,&y); printf("Date in Gregorianic system: %d.%d.%d\n",d,m,y); jd1=kal_gre_east(y); kal_conv_jd_gre(jd1,&d,&m,&y); printf("(Easter sunday this year: %d.%d.%d)\n\n",d,m,y); kal_conv_jd_ar(jd,&d,&m,&y); printf("Date in Arabic system: %d. %s %d\n\n",d,kal_ar_months(m),y); kal_conv_jd_jew(jd,&d,&m,&y); printf("Date in Jewish system: %d. %s %d\n\n",d,kal_jew_months(m),y); }