1 /* Date manipulation routines.
2 Copyright (C) 2001,2002 The Genes Development Team
3 This file is part of the Gedcom parser library.
4 Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
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.
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.
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
24 #include "gedcom_internal.h"
29 struct date_value dv_s;
32 struct date_value def_date_val;
35 const char* curr_line_value;
37 void cleanup_date_buffer();
38 struct safe_buffer date_buffer = { NULL, 0, NULL, 0, cleanup_date_buffer };
40 void cleanup_date_buffer()
42 cleanup_buffer(&date_buffer);
45 int max_month[] = { 12, /* CAL_GREGORIAN */
48 13, /* CAL_FRENCH_REV */
52 typedef long int (*cal_func_type) (int, int, int);
54 cal_func_type cal_func[] = { &GregorianToSdn, /* CAL_GREGORIAN */
55 &JulianToSdn, /* CAL_JULIAN */
56 &JewishToSdn, /* CAL_JEWISH */
57 &FrenchToSdn /* CAL_FRENCH_REV */
60 void copy_date(struct date *to, struct date from)
62 memcpy(to, &from, sizeof(struct date));
65 void init_date(struct date *d)
69 d->month_str[0] = '\0';
70 d->year_str[0] = '\0';
74 d->year_type = YEAR_SINGLE;
75 d->type = DATE_UNRECOGNIZED;
80 struct date_value make_date_value(Date_value_type t, struct date d1,
81 struct date d2, const char* p)
84 copy_date(&dv_s.date1, d1);
85 copy_date(&dv_s.date2, d2);
86 strncpy(dv_s.phrase, p, MAX_PHRASE_LEN + 1);
90 void make_date_complete(struct date *d)
92 if (d->cal == CAL_UNKNOWN)
93 d->type = DATE_UNRECOGNIZED;
97 if (d->day == -1 || d->month == -1 || d->year == -1) {
98 d->type = DATE_BOUNDED;
99 copy_date(&end_date, *d);
100 if (d->month == -1) {
101 d->month = 1; end_date.month = 1;
102 d->day = 1; end_date.day = 1;
105 else if (d->day == -1) {
106 d->day = 1; end_date.day = 1;
108 if (end_date.month > max_month[d->cal]) {
109 end_date.month -= max_month[d->cal];
115 d->type = DATE_EXACT;
118 to_sdn = cal_func[d->cal];
119 d->sdn1 = (*to_sdn)(d->year, d->month, d->day);
120 if (d->type == DATE_BOUNDED) {
121 d->sdn2 = (*to_sdn)(end_date.year, end_date.month, end_date.day);
127 struct date_value gedcom_parse_date(const char* line_value)
130 init_date(&def_date);
131 curr_line_value = line_value;
132 init_gedcom_date_lex(line_value);
134 close_gedcom_date_lex();
135 make_date_complete(&dv_s.date1);
136 make_date_complete(&dv_s.date2);
140 void add_date(struct date* d)
143 case CAL_GREGORIAN: break;
145 safe_buf_append(&date_buffer, "@#DJULIAN@ "); break;
147 safe_buf_append(&date_buffer, "@#DHEBREW@ "); break;
149 safe_buf_append(&date_buffer, "@#DFRENCH R@ "); break;
151 safe_buf_append(&date_buffer, "@#DUNKNOWN@ "); break;
156 safe_buf_append(&date_buffer, "%s ", d->day_str);
158 safe_buf_append(&date_buffer, "%s ", d->month_str);
159 safe_buf_append(&date_buffer, "%s", d->year_str);
162 char* gedcom_date_to_string(struct date_value* val)
164 reset_buffer(&date_buffer);
168 add_date(&val->date1); break;
170 safe_buf_append(&date_buffer, "BEF ");
171 add_date(&val->date1); break;
173 safe_buf_append(&date_buffer, "AFT ");
174 add_date(&val->date1); break;
176 safe_buf_append(&date_buffer, "BET ");
177 add_date(&val->date1);
178 safe_buf_append(&date_buffer, " AND ");
179 add_date(&val->date2); break;
181 safe_buf_append(&date_buffer, "FROM ");
182 add_date(&val->date1); break;
184 safe_buf_append(&date_buffer, "TO ");
185 add_date(&val->date1); break;
187 safe_buf_append(&date_buffer, "FROM ");
188 add_date(&val->date1);
189 safe_buf_append(&date_buffer, " TO ");
190 add_date(&val->date2); break;
192 safe_buf_append(&date_buffer, "ABT ");
193 add_date(&val->date1); break;
195 safe_buf_append(&date_buffer, "CAL ");
196 add_date(&val->date1); break;
198 safe_buf_append(&date_buffer, "EST ");
199 add_date(&val->date1); break;
201 safe_buf_append(&date_buffer, "INT ");
202 add_date(&val->date1);
203 safe_buf_append(&date_buffer, " (%s)", val->phrase); break;
205 safe_buf_append(&date_buffer, "(%s)", val->phrase); break;
210 return get_buf_string(&date_buffer);