c145f213d3fe9a6c6911e1980c9b891025b5bc93
[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 int max_month[] = { 12,  /* CAL_GREGORIAN */
36                     12,  /* CAL_JULIAN */
37                     13,  /* CAL_HEBREW */
38                     13,  /* CAL_FRENCH_REV */
39                     0    /* CAL_UNKNOWN */
40                   };
41
42 void copy_date(struct date *to, struct date from)
43 {
44   memcpy(to, &from, sizeof(struct date));
45 }
46
47 void init_date(struct date *d)
48 {
49   d->cal = CAL_UNKNOWN;
50   d->day_str[0] = '\0';
51   d->month_str[0] = '\0';
52   d->year_str[0] = '\0';
53   d->day = -1;
54   d->month = -1;
55   d->year = -1;
56   d->year_type = YEAR_SINGLE;
57   d->type = DATE_UNRECOGNIZED;
58   d->sdn1 = -1;
59   d->sdn2 = -1;
60 }
61
62 struct date_value make_date_value(Date_value_type t, struct date d1,
63                                   struct date d2, char* p)
64 {
65   dv_s.type = t;
66   copy_date(&dv_s.date1, d1);
67   copy_date(&dv_s.date2, d2);
68   strncpy(dv_s.phrase, p, MAX_PHRASE_LEN + 1);
69   return dv_s;
70 }
71
72 void make_date_complete(struct date *d)
73 {
74   if (d->cal == CAL_UNKNOWN)
75     d->type = DATE_UNRECOGNIZED;
76   else {
77     struct date end_date;
78     if (d->day == -1 || d->month == -1 || d->year == -1) {
79       d->type = DATE_BOUNDED;
80       copy_date(&end_date, *d);
81       if (d->month == -1) {
82         d->month = 1; end_date.month = 1;
83         d->day   = 1; end_date.day   = 1;
84         end_date.year += 1;
85       }
86       else if (d->day == -1) {
87         d->day   = 1; end_date.day   = 1;
88         end_date.month += 1;
89         if (end_date.month > max_month[d->cal]) {
90           end_date.month -= max_month[d->cal];
91           end_date.year  += 1;
92         }
93       }
94     }
95     else {
96       d->type = DATE_EXACT;
97     }
98     
99     if (d->cal == CAL_GREGORIAN) {
100       d->sdn1 = GregorianToSdn(d->year, d->month, d->day);
101       if (d->type == DATE_BOUNDED) {
102         d->sdn2 = GregorianToSdn(end_date.year, end_date.month, end_date.day);
103         d->sdn2 -= 1;
104       }
105     }
106     else if (d->cal == CAL_JULIAN) {
107       d->sdn1 = JulianToSdn(d->year, d->month, d->day);
108       if (d->type == DATE_BOUNDED) {
109         d->sdn2 = JulianToSdn(end_date.year, end_date.month, end_date.day);
110         d->sdn2 -= 1;
111       }
112     }
113     else if (d->cal == CAL_HEBREW) {
114       d->sdn1 = JewishToSdn(d->year, d->month, d->day);
115       if (d->type == DATE_BOUNDED) {
116         d->sdn2 = JewishToSdn(end_date.year, end_date.month, end_date.day);
117         d->sdn2 -= 1;
118       }
119     }
120     else if (d->cal == CAL_FRENCH_REV) {
121       d->sdn1 = FrenchToSdn(d->year, d->month, d->day);
122       if (d->type == DATE_BOUNDED) {
123         d->sdn2 = FrenchToSdn(end_date.year, end_date.month, end_date.day);
124         d->sdn2 -= 1;
125       }
126     }
127   }
128 }
129
130 struct date_value gedcom_parse_date(char* line_value)
131 {
132   init_date(&date_s);
133   init_date(&def_date);
134   init_gedcom_date_lex(line_value);
135   gedcom_date_parse();
136   close_gedcom_date_lex();
137   make_date_complete(&dv_s.date1);
138   make_date_complete(&dv_s.date2);
139   return dv_s;
140 }
141