Added some initial documentation.
[gedcom-parse.git] / gedcom / date.c
1 /* Date manipulation routines.
2    Copyright (C) 2001 The Genes Development Team
3    This file is part of the Gedcom parser library.
4    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
5
6    The Gedcom parser library is free software; you can redistribute it
7    and/or modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The Gedcom parser library is distributed in the hope that it will be
12    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the Gedcom parser library; if not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* $Id$ */
22 /* $Name$ */
23
24 #include <stdio.h>
25 #include "gedcom_internal.h"
26 #include "sdncal.h"
27 #include "date.h"
28
29 struct date_value dv_s;
30 struct date date_s;
31
32 struct date_value def_date_val;
33 struct date def_date;
34
35 char* curr_line_value;
36
37 int max_month[] = { 12,  /* CAL_GREGORIAN */
38                     12,  /* CAL_JULIAN */
39                     13,  /* CAL_HEBREW */
40                     13,  /* CAL_FRENCH_REV */
41                     0    /* CAL_UNKNOWN */
42                   };
43
44 typedef long int (*cal_func_type) (int, int, int);
45
46 cal_func_type cal_func[] = { &GregorianToSdn,   /* CAL_GREGORIAN */
47                              &JulianToSdn,      /* CAL_JULIAN */
48                              &JewishToSdn,      /* CAL_JEWISH */
49                              &FrenchToSdn       /* CAL_FRENCH_REV */
50                            };
51
52 void copy_date(struct date *to, struct date from)
53 {
54   memcpy(to, &from, sizeof(struct date));
55 }
56
57 void init_date(struct date *d)
58 {
59   d->cal = CAL_UNKNOWN;
60   d->day_str[0] = '\0';
61   d->month_str[0] = '\0';
62   d->year_str[0] = '\0';
63   d->day = -1;
64   d->month = -1;
65   d->year = -1;
66   d->year_type = YEAR_SINGLE;
67   d->type = DATE_UNRECOGNIZED;
68   d->sdn1 = -1;
69   d->sdn2 = -1;
70 }
71
72 struct date_value make_date_value(Date_value_type t, struct date d1,
73                                   struct date d2, char* p)
74 {
75   dv_s.type = t;
76   copy_date(&dv_s.date1, d1);
77   copy_date(&dv_s.date2, d2);
78   strncpy(dv_s.phrase, p, MAX_PHRASE_LEN + 1);
79   return dv_s;
80 }
81
82 void make_date_complete(struct date *d)
83 {
84   if (d->cal == CAL_UNKNOWN)
85     d->type = DATE_UNRECOGNIZED;
86   else {
87     struct date end_date;
88     cal_func_type to_sdn;
89     if (d->day == -1 || d->month == -1 || d->year == -1) {
90       d->type = DATE_BOUNDED;
91       copy_date(&end_date, *d);
92       if (d->month == -1) {
93         d->month = 1; end_date.month = 1;
94         d->day   = 1; end_date.day   = 1;
95         end_date.year += 1;
96       }
97       else if (d->day == -1) {
98         d->day   = 1; end_date.day   = 1;
99         end_date.month += 1;
100         if (end_date.month > max_month[d->cal]) {
101           end_date.month -= max_month[d->cal];
102           end_date.year  += 1;
103         }
104       }
105     }
106     else {
107       d->type = DATE_EXACT;
108     }
109
110     to_sdn = cal_func[d->cal];
111     d->sdn1 = (*to_sdn)(d->year, d->month, d->day);
112     if (d->type == DATE_BOUNDED) {
113       d->sdn2 = (*to_sdn)(end_date.year, end_date.month, end_date.day);
114       d->sdn2 -= 1;
115     }
116   }
117 }
118
119 struct date_value gedcom_parse_date(char* line_value)
120 {
121   init_date(&date_s);
122   init_date(&def_date);
123   curr_line_value = line_value;
124   init_gedcom_date_lex(line_value);
125   gedcom_date_parse();
126   close_gedcom_date_lex();
127   make_date_complete(&dv_s.date1);
128   make_date_complete(&dv_s.date2);
129   return dv_s;
130 }
131