1 from .base import MetaValue
4 class DateValue(MetaValue):
6 def from_text(cls, text):
8 Dates for digitization of pictures. It seems we need the following:
11 half centuries/decades: '2 poł. XVIII w.', 'XVII w., l. 20'
13 circa 'ok. 1813-1814', 'ok.1876-ok.1886
16 For now we will translate this to some single date
17 losing information of course.
20 # check out the "N. poł X w." syntax
21 if isinstance(text, six.binary_type):
22 text = text.decode("utf-8")
25 u"(?:([12]) *poł[.]? +)?([MCDXVI]+) *w[.,]*(?: *l[.]? *([0-9]+))?"
27 vague_format = u"(?:po *|ok. *)?([0-9]{4})(-[0-9]{2}-[0-9]{2})?"
29 m = re.match(century_format, text)
30 m2 = re.match(vague_format, text)
34 century = roman_to_int(m.group(2))
36 if decade is not None:
39 "Cannot specify both half and decade of century."
42 t = ((century*100 + (half-1)*50), 1, 1)
44 decade = int(decade or 0)
45 t = ((century*100 + decade), 1, 1)
50 t = time.strptime(year + mon_day, "%Y-%m-%d")
52 t = time.strptime(year, '%Y')
56 return cls(date(t[0], t[1], t[2]))
58 raise ValueError("Unrecognized date format. Try YYYY-MM-DD or YYYY.")