def _parse_datetime_header(value): match = re.match(r'^(?P<datetime>.*?)(?P<tzoffset>[+-]\d{4})?$', value) tt = time.strptime(match.group('datetime'), '%Y-%m-%d %H:%M') ts = time.mktime(tt) dt = datetime.fromtimestamp(ts) # Separate the offset into a sign component, hours, and # minutes tzoffset = match.group('tzoffset') if tzoffset is not None: plus_minus_s, rest = tzoffset[0], tzoffset[1:] hours_offset_s, mins_offset_s = rest[:2], rest[2:] # Make them all integers plus_minus = int(plus_minus_s + '1') hours_offset = int(hours_offset_s) mins_offset = int(mins_offset_s) # Calculate net offset net_mins_offset = hours_offset * 60 net_mins_offset += mins_offset net_mins_offset *= plus_minus # Create an offset object tzoffset = FixedOffsetTimezone(net_mins_offset) # Store the offset in a datetime object dt = dt.replace(tzinfo=tzoffset) return dt
def test_mime_headers_contain_same_information_as_attributes(self): cat = catalog.Catalog() cat[''] = catalog.Message( '', "Last-Translator: Foo Bar <*****@*****.**>\n" + "Language-Team: de <*****@*****.**>\n" + "POT-Creation-Date: 2009-03-01 11:20+0200\n" + "PO-Revision-Date: 2009-03-09 15:47-0700\n") self.assertEqual(None, cat.locale) mime_headers = dict(cat.mime_headers) self.assertEqual('Foo Bar <*****@*****.**>', cat.last_translator) self.assertEqual('Foo Bar <*****@*****.**>', mime_headers['Last-Translator']) self.assertEqual('de <*****@*****.**>', cat.language_team) self.assertEqual('de <*****@*****.**>', mime_headers['Language-Team']) dt = datetime.datetime(2009, 3, 9, 15, 47, tzinfo=FixedOffsetTimezone(-7 * 60)) self.assertEqual(dt, cat.revision_date) formatted_dt = format_datetime(dt, 'yyyy-MM-dd HH:mmZ', locale='en') self.assertEqual(formatted_dt, mime_headers['PO-Revision-Date'])
def test_header_entry(self): buf = StringIO(r'''\ # SOME DESCRIPTIVE TITLE. # Copyright (C) 2007 THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: 3.15\n" "Report-Msgid-Bugs-To: Fliegender Zirkus <*****@*****.**>\n" "POT-Creation-Date: 2007-09-27 11:19+0700\n" "PO-Revision-Date: 2007-09-27 21:42-0700\n" "Last-Translator: John <*****@*****.**>\n" "Language-Team: German Lang <*****@*****.**>\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=iso-8859-2\n" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 1.0dev-r313\n" ''') catalog = pofile.read_po(buf) self.assertEqual(1, len(list(catalog))) self.assertEqual(u'3.15', catalog.version) self.assertEqual(u'Fliegender Zirkus <*****@*****.**>', catalog.msgid_bugs_address) self.assertEqual( datetime(2007, 9, 27, 11, 19, tzinfo=FixedOffsetTimezone(7 * 60)), catalog.creation_date) self.assertEqual(u'John <*****@*****.**>', catalog.last_translator) self.assertEqual(u'German Lang <*****@*****.**>', catalog.language_team) self.assertEqual(u'iso-8859-2', catalog.charset) self.assertEqual(True, list(catalog)[0].fuzzy)
def test_can_store_datetime_with_timezone(self): dt = DateTime(2013, 5, 25, 9, 53, 24, tzinfo=FixedOffsetTimezone(-90)) inserted_id = self._insert_data(self.table, [{'timestamp': dt}]) dt_from_db = self._fetch_value(self.table, id=inserted_id) assert_equals(dt, dt_from_db) assert_equals(UTC, dt_from_db.tzinfo)
def _set_mime_headers(self, headers): for name, value in headers: if name.lower() == 'content-type': mimetype, params = parse_header(value) if 'charset' in params: self.charset = params['charset'].lower() break for name, value in headers: name = name.lower().decode(self.charset) value = value.decode(self.charset) if name == 'project-id-version': parts = value.split(' ') self.project = u' '.join(parts[:-1]) self.version = parts[-1] elif name == 'report-msgid-bugs-to': self.msgid_bugs_address = value elif name == 'last-translator': self.last_translator = value elif name == 'language-team': self.language_team = value elif name == 'plural-forms': _, params = parse_header(' ;' + value) self._num_plurals = int(params.get('nplurals', 2)) self._plural_expr = params.get('plural', '(n != 1)') elif name == 'pot-creation-date': # FIXME: this should use dates.parse_datetime as soon as that # is ready value, tzoffset, _ = re.split('[+-](\d{4})$', value, 1) tt = time.strptime(value, '%Y-%m-%d %H:%M') ts = time.mktime(tt) tzoffset = FixedOffsetTimezone(int(tzoffset[:2]) * 60 + int(tzoffset[2:])) dt = datetime.fromtimestamp(ts) self.creation_date = dt.replace(tzinfo=tzoffset)
def _utc(self): UTC = FixedOffsetTimezone(0, 'UTC') def fake_localize(self, dt, is_dst=False): raise NotImplementedError() UTC.localize = new.instancemethod(fake_localize, UTC, UTC.__class__) # This is important to trigger the actual bug (#257) self.assertEqual(False, hasattr(UTC, 'normalize')) return UTC
def _set_mime_headers(self, headers): for name, value in headers: name = name.lower() if name == 'project-id-version': parts = value.split(' ') self.project = u(' ').join(parts[:-1]) self.version = parts[-1] elif name == 'report-msgid-bugs-to': self.msgid_bugs_address = value elif name == 'last-translator': self.last_translator = value elif name == 'language-team': self.language_team = value elif name == 'content-type': mimetype, params = parse_header(value) if 'charset' in params: self.charset = params['charset'].lower() elif name == 'plural-forms': _, params = parse_header(' ;' + value) self._num_plurals = int(params.get('nplurals', 2)) self._plural_expr = params.get('plural', '(n != 1)') elif name == 'pot-creation-date': # FIXME: this should use dates.parse_datetime as soon as that # is ready value, tzoffset, _ = re.split('([+-]\d{4})$', value, 1) tt = time.strptime(value, '%Y-%m-%d %H:%M') ts = time.mktime(tt) # Separate the offset into a sign component, hours, and minutes plus_minus_s, rest = tzoffset[0], tzoffset[1:] hours_offset_s, mins_offset_s = rest[:2], rest[2:] # Make them all integers plus_minus = int(plus_minus_s + '1') hours_offset = int(hours_offset_s) mins_offset = int(mins_offset_s) # Calculate net offset net_mins_offset = hours_offset * 60 net_mins_offset += mins_offset net_mins_offset *= plus_minus # Create an offset object tzoffset = FixedOffsetTimezone(net_mins_offset) # Store the offset in a datetime object dt = datetime.fromtimestamp(ts) self.creation_date = dt.replace(tzinfo=tzoffset) elif name == 'po-revision-date': # Keep the value if it's not the default one if 'YEAR' not in value: # FIXME: this should use dates.parse_datetime as soon as # that is ready value, tzoffset, _ = re.split('([+-]\d{4})$', value, 1) tt = time.strptime(value, '%Y-%m-%d %H:%M') ts = time.mktime(tt) # Separate the offset into a sign component, hours, and # minutes plus_minus_s, rest = tzoffset[0], tzoffset[1:] hours_offset_s, mins_offset_s = rest[:2], rest[2:] # Make them all integers plus_minus = int(plus_minus_s + '1') hours_offset = int(hours_offset_s) mins_offset = int(mins_offset_s) # Calculate net offset net_mins_offset = hours_offset * 60 net_mins_offset += mins_offset net_mins_offset *= plus_minus # Create an offset object tzoffset = FixedOffsetTimezone(net_mins_offset) # Store the offset in a datetime object dt = datetime.fromtimestamp(ts) self.revision_date = dt.replace(tzinfo=tzoffset)