Skip to content

Instantly share code, notes, and snippets.

@saeed-abdullah
Created November 26, 2019 00:22
Show Gist options
  • Save saeed-abdullah/7b45dcb4de61fa55411399ba542134ce to your computer and use it in GitHub Desktop.
Save saeed-abdullah/7b45dcb4de61fa55411399ba542134ce to your computer and use it in GitHub Desktop.
MailParser timezone parsing patch
diff --git mailparser/utils.py mailparser/utils.py
index f44139c..797f218 100644
--- mailparser/utils.py
+++ mailparser/utils.py
@@ -345,7 +345,7 @@ def convert_mail_date(date):
log.debug("Date parsed in timestamp: {!r}".format(t))
date_utc = datetime.datetime.utcfromtimestamp(t)
timezone = d[9] / 3600 if d[9] else 0
- timezone = "{:+.0f}".format(timezone)
+ timezone = "{:+.2f}".format(timezone)
return date_utc, timezone
diff --git tests/test_mail_parser.py tests/test_mail_parser.py
index a0abd5a..dd17c9f 100644
--- tests/test_mail_parser.py
+++ tests/test_mail_parser.py
@@ -495,7 +495,7 @@ class TestMailParser(unittest.TestCase):
self.assertIsInstance(result, list)
result = mail.timezone
- self.assertEqual(result, "+1")
+ self.assertEqual(result, "+1.00")
def test_get_to_domains(self):
m = mailparser.parse_from_file(mail_test_6)
@@ -514,11 +514,15 @@ class TestMailParser(unittest.TestCase):
def test_convert_mail_date(self):
s = "Mon, 20 Mar 2017 05:12:54 +0600"
d, t = convert_mail_date(s)
- self.assertEqual(t, "+6")
+ self.assertEqual(t, "+6.00")
self.assertEqual(str(d), "2017-03-19 23:12:54")
s = "Mon, 20 Mar 2017 05:12:54 -0600"
d, t = convert_mail_date(s)
- self.assertEqual(t, "-6")
+ self.assertEqual(t, "-6.00")
+ s = "Mon, 11 Dec 2017 15:27:44 +0530"
+ d, t = convert_mail_date(s)
+ self.assertEqual(str(d), "2017-12-11 09:57:44")
+ self.assertEqual(t, "+5.50")
def test_ported_string(self):
raw_data = ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment