1# -*- coding: utf-8 -*-
2#
3# Gramps - a GTK+/GNOME based genealogy program
4#
5# Copyright (C) 2004-2006  Donald N. Allingham
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21# Attempt to parse dates for Icelandic, Sveinn í Felli 2016
22
23"""
24Icelandic-specific classes for parsing and displaying dates.
25"""
26
27#-------------------------------------------------------------------------
28#
29# Python modules
30#
31#-------------------------------------------------------------------------
32import re
33import datetime
34
35#-------------------------------------------------------------------------
36#
37# Gramps modules
38#
39#-------------------------------------------------------------------------
40from ..lib.date import Date
41from ._dateparser import DateParser
42from ._datedisplay import DateDisplay
43from ._datehandler import register_datehandler
44
45#-------------------------------------------------------------------------
46#
47# Icelandic parser class
48#
49#-------------------------------------------------------------------------
50class DateParserIs(DateParser):
51    """
52    Convert a text string into a Date object, expecting a date
53    notation in the Icelandic language. If the date cannot be converted,
54    the text string is assigned.
55    """
56
57    # modifiers before the date
58    modifier_to_int = {
59        'fyrir'    : Date.MOD_BEFORE,
60        'á undan'  : Date.MOD_BEFORE,
61        'eftir'   : Date.MOD_AFTER,
62        'í kringum' : Date.MOD_ABOUT,
63        'uþb'      : Date.MOD_ABOUT
64        }
65
66    bce = ["f Kr"]
67
68    calendar_to_int = {
69        'gregoríanskt   '      : Date.CAL_GREGORIAN,
70        'g'                   : Date.CAL_GREGORIAN,
71        'júlíanskt'            : Date.CAL_JULIAN,
72        'j'                   : Date.CAL_JULIAN,
73        'hebreskt'            : Date.CAL_HEBREW,
74        'h'                   : Date.CAL_HEBREW,
75        'íslamskt'            : Date.CAL_ISLAMIC,
76        'múslimskt'            : Date.CAL_ISLAMIC,
77        'i'                   : Date.CAL_ISLAMIC,
78        'franskt'              : Date.CAL_FRENCH,
79        'franska lýðveldisins' : Date.CAL_FRENCH,
80        'f'                   : Date.CAL_FRENCH,
81        'persneskt'             : Date.CAL_PERSIAN,
82        'p'                   : Date.CAL_PERSIAN,
83        'sænskt'              : Date.CAL_SWEDISH,
84        's'                   : Date.CAL_SWEDISH,
85        }
86
87    quality_to_int = {
88        'áætlað' : Date.QUAL_ESTIMATED,
89        'reiknað'   : Date.QUAL_CALCULATED,
90        }
91
92    def dhformat_changed(self):
93        self._dhformat_parse = re.compile(r".*%(\S).*%(\S).*%(\S).*%(\S).*")
94
95    def init_strings(self):
96        DateParser.init_strings(self)
97
98        # match 'day. month year' format
99        self._text2 = re.compile(r'(\d+)?\.?\s*?%s\.?\s*((\d+)(/\d+)?)?\s*$'
100                                 % self._mon_str, re.IGNORECASE)
101        # match 'short-day day.month year' format
102        short_day_str = '(' + '|'.join(self._ds.short_days[1:]) + ')'
103        self._numeric = re.compile(r"%s\s*((\d+)[\.]\s*)?((\d+)\s*)?(\d+)\s*$"
104                                   % short_day_str, re.IGNORECASE)
105        self._span = re.compile(
106            r"(frá)?\s*(?P<start>.+)\s*(til|--|–)\s*(?P<stop>.+)",
107            re.IGNORECASE)
108        self._range = re.compile(
109            r"(milli)\s+(?P<start>.+)\s+og\s+(?P<stop>.+)", re.IGNORECASE)
110
111#-------------------------------------------------------------------------
112#
113# Icelandic display class
114#
115#-------------------------------------------------------------------------
116class DateDisplayIs(DateDisplay):
117    """
118    Icelandic language date display class.
119    """
120
121    long_months = ( "", "janúar", "febrúar", "mars", "apríl", "maí",
122                    "júní", "júlí", "ágúst", "september", "október",
123                    "nóvember", "desember" )
124
125    short_months = ( "", "jan", "feb", "mar", "apr", "maí", "jún",
126                     "júl", "ágú", "sep", "okt", "nóv", "des" )
127
128    formats = (
129        "ÁÁÁÁ-MM-DD (ISO)",
130        "Tölulegt",
131        "Mánuður dagur, ár",
132        "Mán Dag Ár",
133        "Dagur mánuður ár",
134        "Dag Mán Ár",
135        )
136        # this must agree with DateDisplayEn's "formats" definition
137        # (since no locale-specific _display_gregorian exists, here)
138
139    calendar = (
140        "",
141        "júlíanskt",
142        "hebreskt",
143        "franska lýðveldisins",
144        "persneskt",
145        "íslamskt",
146        "sænskt"
147        )
148
149    _mod_str = ("", "fyrir ", "eftir ", "uþb ", "", "", "")
150
151    _qual_str = ("", "reiknað ", "reiknað ")
152
153    _bce_str = "%s f. Kr"
154
155    def display(self, date):
156        """
157        Return a text string representing the date.
158        """
159        mod = date.get_modifier()
160        cal = date.get_calendar()
161        qual = date.get_quality()
162        start = date.get_start_date()
163        newyear = date.get_new_year()
164
165        qual_str = self._qual_str[qual]
166
167        if mod == Date.MOD_TEXTONLY:
168            return date.get_text()
169        elif start == Date.EMPTY:
170            return ""
171        elif mod == Date.MOD_SPAN:
172            d1 = self.display_cal[cal](start)
173            d2 = self.display_cal[cal](date.get_stop_date())
174            scal = self.format_extras(cal, newyear)
175            return "%sfrá %s til %s%s" % (qual_str, d1, d2, scal)
176        elif mod == Date.MOD_RANGE:
177            d1 = self.display_cal[cal](start)
178            d2 = self.display_cal[cal](date.get_stop_date())
179            scal = self.format_extras(cal, newyear)
180            return "%smilli %s og %s%s" % (qual_str, d1, d2,
181                                              scal)
182        else:
183            text = self.display_cal[date.get_calendar()](start)
184            scal = self.format_extras(cal, newyear)
185            return "%s%s%s%s" % (qual_str, self._mod_str[mod],
186                                 text, scal)
187
188    def _get_weekday(self, date_val):
189        if (date_val[0] == 0 or date_val[1] == 0 # no day or no month or both
190                or date_val[2] > datetime.MAXYEAR): # bug 10815
191            return ''
192        w_day = datetime.date(date_val[2], date_val[1], date_val[0]) # y, m, d
193        return self.short_days[((w_day.weekday() + 1) % 7) + 1]
194
195    def dd_dformat01(self, date_val):
196        """
197        numerical -- for Icelandic dates
198        """
199        if date_val[3]:
200            return self.display_iso(date_val)
201        else:
202            if date_val[0] == date_val[1] == 0:
203                return str(date_val[2])
204            else:
205                value = self.dhformat.replace('%m', str(date_val[1]))
206                # some locales have %b for the month, e.g. ar_EG, is_IS, nb_NO
207                value = value.replace('%b', str(date_val[1]))
208                # some locales have %a for the abbreviated day, e.g. is_IS
209                value = value.replace('%a', self._get_weekday(date_val))
210                if date_val[0] == 0: # ignore the zero day and its delimiter
211                    i_day = value.find('%e') # Icelandic uses %e and not %d
212                    value = value.replace(value[i_day:i_day+3], '')
213                value = value.replace('%e', str(date_val[0]))
214                value = value.replace('%Y', str(abs(date_val[2])))
215                return value.replace('-', '/')
216
217#-------------------------------------------------------------------------
218#
219# Register classes
220#
221#-------------------------------------------------------------------------
222register_datehandler(
223    ('is_IS', 'is', 'íslenskt', 'Icelandic', ('%a %e.%b %Y',)),
224    DateParserIs, DateDisplayIs)
225