Add simple parse_delay() method
The XEP is pretty strict on Delay tags so we can make a simple method.
This commit is contained in:
		
							parent
							
								
									06d890eea7
								
							
						
					
					
						commit
						89367a83ec
					
				
					 1 changed files with 34 additions and 4 deletions
				
			
		| 
						 | 
					@ -43,11 +43,13 @@ import shlex
 | 
				
			||||||
from common import caps_cache
 | 
					from common import caps_cache
 | 
				
			||||||
import socket
 | 
					import socket
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
import datetime
 | 
					from datetime import datetime, timedelta
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from encodings.punycode import punycode_encode
 | 
					from encodings.punycode import punycode_encode
 | 
				
			||||||
from string import Template
 | 
					from string import Template
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import nbxmpp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from common.i18n import Q_
 | 
					from common.i18n import Q_
 | 
				
			||||||
from common.i18n import ngettext
 | 
					from common.i18n import ngettext
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -589,16 +591,45 @@ def datetime_tuple(timestamp):
 | 
				
			||||||
    tim = time.strptime(date + 'T' + tim, '%Y%m%dT%H:%M:%S')
 | 
					    tim = time.strptime(date + 'T' + tim, '%Y%m%dT%H:%M:%S')
 | 
				
			||||||
    if zone:
 | 
					    if zone:
 | 
				
			||||||
        zone = zone.replace(':', '')
 | 
					        zone = zone.replace(':', '')
 | 
				
			||||||
        tim = datetime.datetime.fromtimestamp(time.mktime(tim))
 | 
					        tim = datetime.fromtimestamp(time.mktime(tim))
 | 
				
			||||||
        if len(zone) > 2:
 | 
					        if len(zone) > 2:
 | 
				
			||||||
            zone = time.strptime(zone, '%H%M')
 | 
					            zone = time.strptime(zone, '%H%M')
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            zone = time.strptime(zone, '%H')
 | 
					            zone = time.strptime(zone, '%H')
 | 
				
			||||||
        zone = datetime.timedelta(hours=zone.tm_hour, minutes=zone.tm_min)
 | 
					        zone = timedelta(hours=zone.tm_hour, minutes=zone.tm_min)
 | 
				
			||||||
        tim += zone * sign
 | 
					        tim += zone * sign
 | 
				
			||||||
        tim = tim.timetuple()
 | 
					        tim = tim.timetuple()
 | 
				
			||||||
    return tim
 | 
					    return tim
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def parse_delay(timestamp):
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
 | 
					    Parse a timestamp
 | 
				
			||||||
 | 
					    https://xmpp.org/extensions/xep-0203.html
 | 
				
			||||||
 | 
					    Note: Not all delay tags should be parsed with this method
 | 
				
			||||||
 | 
					    see https://xmpp.org/extensions/xep-0082.html for more information
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    :param timestamp: a XEP-0203 fomated timestring string or a delay Node
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    Examples:
 | 
				
			||||||
 | 
					    '2017-11-05T01:41:20Z'
 | 
				
			||||||
 | 
					    '2017-11-05T01:41:20.123Z'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return epoch UTC timestamp
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
 | 
					    if isinstance(timestamp, nbxmpp.protocol.Node):
 | 
				
			||||||
 | 
					        timestamp = timestamp.getAttr('stamp')
 | 
				
			||||||
 | 
					    timestamp += '+0000'
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        datetime_ = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ%z')
 | 
				
			||||||
 | 
					    except ValueError:
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            datetime_ = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ%z')
 | 
				
			||||||
 | 
					        except ValueError:
 | 
				
			||||||
 | 
					            log.error('Could not parse delay timestamp: %s', timestamp)
 | 
				
			||||||
 | 
					            raise
 | 
				
			||||||
 | 
					    return datetime_.timestamp()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from common import gajim
 | 
					from common import gajim
 | 
				
			||||||
if gajim.HAVE_PYCURL:
 | 
					if gajim.HAVE_PYCURL:
 | 
				
			||||||
    import pycurl
 | 
					    import pycurl
 | 
				
			||||||
| 
						 | 
					@ -1280,7 +1311,6 @@ def prepare_and_validate_gpg_keyID(account, jid, keyID):
 | 
				
			||||||
    return keyID
 | 
					    return keyID
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_optional_features(account = None):
 | 
					def update_optional_features(account = None):
 | 
				
			||||||
    import nbxmpp
 | 
					 | 
				
			||||||
    if account:
 | 
					    if account:
 | 
				
			||||||
        accounts = [account]
 | 
					        accounts = [account]
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue