'Parses mail and returns an INTERNALDATE string
It will use information in the following order, falling back as an
attempt fails:
- rtime parameter
- Date header of email
We return None, if we couldn\'t find a valid date. In this case
the IMAP server will use the server local time when appening
(per RFC).
Note, that imaplib\'s Time2Internaldate is inherently broken as
it returns localized date strings which are invalid for IMAP
servers. However, that function is called for *every* append()
internally. So we need to either pass in `None` or the correct
string (in which case Time2Internaldate() will do nothing) to
append(). The output of this function is designed to work as
input to the imapobj.append() function.
TODO: We should probably be returning a bytearray rather than a
string here, because the IMAP server will expect plain
ASCII. However, imaplib.Time2INternaldate currently returns a
string so we go with the same for now.
:param rtime: epoch timestamp to be used rather than analyzing
the email.
:returns: string in the form of "DD-Mmm-YYYY HH:MM:SS +HHMM"
(including double quotes) or `None` in case of failure
(which is fine as value for append).'

def __getmessageinternaldate(self, content, rtime=None):

    if (rtime is None):
        rtime = emailutil.get_message_date(content)
        if (rtime == None):
            return None
    datetuple = time.localtime(rtime)
    try:
        if (datetuple[0] < 1981):
            raise ValueError
        datetuple_check = time.localtime(time.mktime(datetuple))
        if (datetuple[:2] != datetuple_check[:2]):
            raise ValueError
    except (ValueError, OverflowError):
        self.ui.debug('imap', ('Message    with    invalid    date    %s.    Server    will    use    local    time.' % datetuple))
        return None
    num2mon = {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
    if (datetuple.tm_isdst == 1):
        zone = (- time.altzone)
    else:
        zone = (- time.timezone)
    (offset_h, offset_m) = divmod((zone // 60), 60)
    internaldate = ('"%02d-%s-%04d    %02d:%02d:%02d    %+03d%02d"' % (datetuple.tm_mday, num2mon[datetuple.tm_mon], datetuple.tm_year, datetuple.tm_hour, datetuple.tm_min, datetuple.tm_sec, offset_h, offset_m))
    return internaldate
