Get the month number

This patch gets the month number using datetime library.

I removed the function Mon2num. The list is not used now, it is removed
too.

I need change the regex in the compile to get the fields. Now is str,
not binary, so I remored the 'b'.
This commit is contained in:
Rodolfo García Peñas (kix) 2020-08-28 20:05:52 +02:00
parent e61e77cf40
commit 36bfbdf7d6
2 changed files with 17 additions and 13 deletions

View File

@ -16,7 +16,7 @@ Public functions: Internaldate2Time
__all__ = ("IMAP4", "IMAP4_SSL", "IMAP4_stream", __all__ = ("IMAP4", "IMAP4_SSL", "IMAP4_stream",
"Internaldate2Time", "ParseFlags", "Time2Internaldate", "Internaldate2Time", "ParseFlags", "Time2Internaldate",
"Mon2num", "MonthNames", "InternalDate") "MonthNames", "InternalDate")
__version__ = "3.05" __version__ = "3.05"
__release__ = "3" __release__ = "3"
@ -58,7 +58,7 @@ __URL__ = "http://imaplib2.sourceforge.net"
__license__ = "Python License" __license__ = "Python License"
import binascii, calendar, errno, os, queue, random, re, select, socket, sys, time, threading, zlib import binascii, calendar, errno, os, queue, random, re, select, socket, sys, time, threading, zlib
import datetime
select_module = select select_module = select
@ -2306,13 +2306,11 @@ class _IdleCont(object):
MonthNames = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', MonthNames = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Mon2num = {s.encode():n+1 for n, s in enumerate(MonthNames[1:])} InternalDate = re.compile(r'.*INTERNALDATE "'
r'(?P<day>[ 0123][0-9])-(?P<mon>[A-Z][a-z][a-z])-(?P<year>[0-9][0-9][0-9][0-9])'
InternalDate = re.compile(br'.*INTERNALDATE "' r' (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9])'
br'(?P<day>[ 0123][0-9])-(?P<mon>[A-Z][a-z][a-z])-(?P<year>[0-9][0-9][0-9][0-9])' r' (?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9])'
br' (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9])' r'"')
br' (?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9])'
br'"')
def Internaldate2Time(resp): def Internaldate2Time(resp):
@ -2328,7 +2326,10 @@ def Internaldate2Time(resp):
if not mo: if not mo:
return None return None
mon = Mon2num[mo.group('mon')] # Get the month number
datetime_object = datetime.datetime.strptime(mo.group('mon'), "%b")
mon = datetime_object.month
zonen = mo.group('zonen') zonen = mo.group('zonen')
day = int(mo.group('day')) day = int(mo.group('day'))

View File

@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import datetime
import os import os
import fcntl import fcntl
import time import time
@ -30,7 +30,7 @@ import six
from offlineimap import OfflineImapError from offlineimap import OfflineImapError
from offlineimap.ui import getglobalui from offlineimap.ui import getglobalui
from offlineimap.virtual_imaplib2 import IMAP4, IMAP4_SSL, InternalDate, Mon2num from offlineimap.virtual_imaplib2 import IMAP4, IMAP4_SSL, InternalDate
class UsefulIMAPMixIn(object): class UsefulIMAPMixIn(object):
@ -248,7 +248,10 @@ def Internaldate2epoch(resp):
if not mo: if not mo:
return None return None
mon = Mon2num[mo.group('mon')] # Get the month number
datetime_object = datetime.datetime.strptime(mo.group('mon'), "%b")
mon = datetime_object.month
zonen = mo.group('zonen') zonen = mo.group('zonen')
day = int(mo.group('day')) day = int(mo.group('day'))