def iso8601_to_dt(iso8601): """Given an ISO8601 string as returned by the device cloud, convert to a datetime object""" # We could just use arrow.get() but that is more permissive than we actually want. # Internal (but still public) to arrow is the actual parser where we can be # a bit more specific parser = DateTimeParser() try: arrow_dt = arrow.Arrow.fromdatetime(parser.parse_iso(iso8601)) return arrow_dt.to('utc').datetime except ParserError as pe: raise ValueError("Provided was not a valid ISO8601 string: %r" % pe)
def iso8601_to_dt(iso8601): """Given an ISO8601 string as returned by Device Cloud, convert to a datetime object""" # We could just use arrow.get() but that is more permissive than we actually want. # Internal (but still public) to arrow is the actual parser where we can be # a bit more specific parser = DateTimeParser() try: arrow_dt = arrow.Arrow.fromdatetime(parser.parse_iso(iso8601)) return arrow_dt.to('utc').datetime except ParserError as pe: raise ValueError("Provided was not a valid ISO8601 string: %r" % pe)
def rfc3339TextAsDateTime(rfc3339: str) -> DateTime: """ Convert an RFC 3339 formatted string to a :class:`DateTime`. :param rfc3339: An RFC-3339 formatted string. :return: A :class:`DateTime` corresponding to :obj:`rfc3339`. """ return DateTimeParser().parse_iso(rfc3339)
def grok_datetime(dstring): more_formats = ['YYYYMMDDTHHmmss', 'YYYYMMDDTHHmmssZ', 'YYYYMMDD'] parser = DateTimeParser() parser.SEPARATORS += [''] # Try to parse datetime string in regular ISO 8601 format try: return parser.parse_iso(dstring) # Fall back to parse datetime string in additional convenience formats except arrow.parser.ParserError as ex: for format in more_formats: try: return parser.parse(dstring, format) except arrow.parser.ParserError as ex: pass # Fall back to attempt to parse as relative datetime expression, e.g. "now-10m" return get_timedelta(dstring)
def from_app_store_receipt(cls, receipt, get_expiry=True): parser = DateTimeParser() purchase = { 'transaction_id': receipt['transaction_id'], 'product_id': receipt['product_id'], 'quantity': receipt['quantity'], 'purchased_at': receipt['purchase_date'] } purchase["purchased_at_datetime"] = parser.parse( receipt["purchase_date"], "YYYY-MM-DD HH:mm:ss ZZZ") if 'expires_date' in receipt and get_expiry: try: purchase["expires_date"] = datetime.utcfromtimestamp( float(receipt["expires_date"]) / 1000) except ValueError: purchase["expires_date"] = parser.parse( receipt["expires_date"], "YYYY-MM-DD HH:mm:ss ZZZ") return cls(**purchase)
def setup_method(self): self.parser = DateTimeParser(locale='en') self.multi_parser = MultiDateParser(self.parser)
from datetime import datetime import logging import uuid import times import ssl from arrow.parser import DateTimeParser # Monkey patch parse function for times library (newer) parser = DateTimeParser() times.parse = parser.parse_iso def fix_ssl_verify(): logger = logging.getLogger('{0}.fix_ssl_verify'.format(__name__)) try: _create_unverified_https_context = ssl._create_unverified_context except AttributeError: # Legacy Python that doesn't verify HTTPS certificates by default pass else: logger.info('Patching SSL HTTPS Context') # Handle target environment that doesn't support HTTPS verification ssl._create_default_https_context = _create_unverified_https_context def remove_none(struct, context=None): if not context: context = {} if 'xmlrpc' in context: return struct converted = struct.copy()