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
22"""
23Swedish-specific classes for parsing and displaying dates.
24"""
25
26#-------------------------------------------------------------------------
27#
28# Python modules
29#
30#-------------------------------------------------------------------------
31import re
32
33#-------------------------------------------------------------------------
34#
35# Gramps modules
36#
37#-------------------------------------------------------------------------
38from ..lib.date import Date
39from ._dateparser import DateParser
40from ._datedisplay import DateDisplay
41from ._datehandler import register_datehandler
42
43#-------------------------------------------------------------------------
44#
45# Swedish parser class
46#
47#-------------------------------------------------------------------------
48class DateParserSv(DateParser):
49    """
50    Convert a text string into a Date object, expecting a date
51    notation in the swedish language. If the date cannot be converted,
52    the text string is assigned.
53    """
54
55    # modifiers before the date
56    modifier_to_int = {
57        'före'    : Date.MOD_BEFORE,
58        'innan'   : Date.MOD_BEFORE,
59        'efter'   : Date.MOD_AFTER,
60        'omkring' : Date.MOD_ABOUT,
61        'ca'      : Date.MOD_ABOUT,
62        'c:a'     : Date.MOD_ABOUT
63        }
64
65    bce = ["f Kr"]
66
67    calendar_to_int = {
68        'gregoriansk   '      : Date.CAL_GREGORIAN,
69        'g'                   : Date.CAL_GREGORIAN,
70        'juliansk'            : Date.CAL_JULIAN,
71        'j'                   : Date.CAL_JULIAN,
72        'hebreisk'            : Date.CAL_HEBREW,
73        'h'                   : Date.CAL_HEBREW,
74        'islamisk'            : Date.CAL_ISLAMIC,
75        'muslimsk'            : Date.CAL_ISLAMIC,
76        'i'                   : Date.CAL_ISLAMIC,
77        'fransk'              : Date.CAL_FRENCH,
78        'fransk republikansk' : Date.CAL_FRENCH,
79        'f'                   : Date.CAL_FRENCH,
80        'persisk'             : Date.CAL_PERSIAN,
81        'p'                   : Date.CAL_PERSIAN,
82        'svensk'              : Date.CAL_SWEDISH,
83        's'                   : Date.CAL_SWEDISH,
84        }
85
86    quality_to_int = {
87        'uppskattat' : Date.QUAL_ESTIMATED,
88        'uppskattad' : Date.QUAL_ESTIMATED,
89        'bedömt'     : Date.QUAL_ESTIMATED,
90        'bedömd'     : Date.QUAL_ESTIMATED,
91        'beräknat'   : Date.QUAL_CALCULATED,
92        'beräknad'   : Date.QUAL_CALCULATED,
93        }
94
95    def init_strings(self):
96        """ Define, in Swedish, span and range regular expressions"""
97        DateParser.init_strings(self)
98        self._numeric = re.compile(r"((\d+)/)?\s*((\d+)/)?\s*(\d+)[/ ]?$")
99        # this next RE has the (possibly-slashed) year at the string's start
100        self._text2 = re.compile(r'((\d+)(/\d+)?)?\s+?%s\s*(\d+)?\s*$'
101                                 % self._mon_str, re.IGNORECASE)
102        self._span = re.compile(
103            r"(från)?\s*(?P<start>.+)\s*(till|--|–)\s*(?P<stop>.+)",
104            re.IGNORECASE)
105        self._range = re.compile(
106            r"(mellan)\s+(?P<start>.+)\s+och\s+(?P<stop>.+)",
107            re.IGNORECASE)
108
109#-------------------------------------------------------------------------
110#
111# Swedish display class
112#
113#-------------------------------------------------------------------------
114class DateDisplaySv(DateDisplay):
115    """
116    Swedish language date display class.
117    """
118
119    _bce_str = "%s f Kr"
120
121    formats = (
122        "ÅÅÅÅ-MM-DD (ISO)",
123        "År/mån/dag",
124        "År månad dag",
125        "År mån dag",
126        )
127        # this definition must agree with its "_display_calendar" method
128
129    def _display_calendar(self, date_val, long_months, short_months = None,
130                          inflect=""):
131        # this must agree with its locale-specific "formats" definition
132
133        if short_months is None:
134            # Let the short formats work the same as long formats
135            short_months = long_months
136
137        if self.format == 0:
138            return self.display_iso(date_val)
139        elif self.format == 1:
140            # numerical: year/month/day (with slashes)
141            value = self.dd_dformat01(date_val)
142        elif self.format == 2:
143            # year month_name day
144            value = self.dd_dformat02_sv(date_val, long_months)
145        # elif self.format == 3:
146        else:
147            # year month_abbreviation day
148            value = self.dd_dformat03_sv(date_val, short_months)
149        if date_val[2] < 0:
150            # TODO fix BUG 7064: non-Gregorian calendars wrongly use BCE notation for negative dates
151            return self._bce_str % value
152        else:
153            return value
154
155    def dd_dformat02_sv(self, date_val, long_months):
156        # year month_name day
157        year = self._slash_year(date_val[2], date_val[3])
158        if date_val[0] == 0:
159            if date_val[1] == 0:
160                return year
161            else:
162                return "%s %s" % (year, long_months[date_val[1]])
163        elif date_val[1] == 0: # month is zero but day is not (see 8477)
164            return self.display_iso(date_val)
165        else:
166            return "%s %s %s" % (year, long_months[date_val[1]], date_val[0])
167
168    def dd_dformat03_sv(self, date_val, short_months):
169        # year month_abbreviation day
170        year = self._slash_year(date_val[2], date_val[3])
171        if date_val[0] == 0:
172            if date_val[1] == 0:
173                return year
174            else:
175                return "%s %s" % (year, short_months[date_val[1]])
176        elif date_val[1] == 0: # month is zero but day is not (see 8477)
177            return self.display_iso(date_val)
178        else:
179            return "%s %s %s" % (year, short_months[date_val[1]], date_val[0])
180
181    display = DateDisplay.display_formatted
182
183#-------------------------------------------------------------------------
184#
185# Register classes
186#
187#-------------------------------------------------------------------------
188register_datehandler(
189    ('sv_SE', 'sv_SE.UTF-8', 'sv', 'Swedish', ('%Y-%m-%d',)),
190    DateParserSv, DateDisplaySv)
191