Python iso8601.parse_date() Examples

The following are 30 code examples of iso8601.parse_date(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module iso8601 , or try the search function .
Example #1
Source File: models.py    From mirandum with Apache License 2.0 6 votes vote down vote up
def as_dict(self):
        details = json.loads(self.details)
        name = "Anonymous"
        amount = " ".join([str(details['amount']), details['currencyCode']])
        if 'user' in details:
            name = details['user']['displayName']
        elif 'username' in details:
            name = details['username']
        timestamp = iso8601.parse_date(details['date'])
        info = {
            'name': name,
            'amount': amount,
            'comment': details['note'],
            'donation_amount': float(details['amount']),
            'currency': details['currencyCode'],
            'timestamp': timestamp,
        }
        return info 
Example #2
Source File: query.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def clear_expired_members_from_cache():
        """ Remove any files from the cache if they've expired. """

        cache_location = CachedQuery.cache_location() 
        CachedQuery.logger.log( "Looking for expired cache members in " + cache_location )
        if not os.path.isdir(cache_location):
            CachedQuery.logger.log( "Cache directory not found.")
            return
        for cache_file in os.listdir(cache_location):
            cache_file = os.path.join(cache_location, cache_file )
            if cache_file.endswith('.query') and os.path.isfile( cache_file ):
                with open(cache_file, 'r') as f:
                    obj = json.loads( f.read() ) 
                    expires = iso8601.parse_date(obj['expires'])
                    if not pytz.UTC.localize(datetime.datetime.now()) < expires:
                        result_file = cache_file.replace('.query', '.result')
                        os.remove( cache_file ) 
                        os.remove( result_file )
                        CachedQuery.logger.log( "Deleting expired cache: " + cache_file + ", " + result_file ) 
Example #3
Source File: views.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def _offering_meeting_time_data(request, offering):
    """
    fullcalendar.js data for this offering's events
    """
    try:
        st = iso8601.parse_date(request.GET['start'])
        en = iso8601.parse_date(request.GET['end'])
    except (KeyError, ValueError, iso8601.ParseError):
        return NotFoundResponse(request, errormsg="Bad request")

    local_tz = pytz.timezone(settings.TIME_ZONE)
    start = st - datetime.timedelta(days=1)
    end = en + datetime.timedelta(days=1)

    response = HttpResponse(content_type='application/json')
    data = list(_offerings_calendar_data([offering], None, start, end, local_tz,
                                         dt_string=True, colour=True, browse_titles=True))
    json.dump(data, response, indent=1)
    return response 
Example #4
Source File: views.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def calendar_data(request):
    """
    AJAX JSON results for the calendar (rendered by dashboard.views.calendar)
    """
    try:
        st = iso8601.parse_date(request.GET['start'])
        en = iso8601.parse_date(request.GET['end'])
    except (KeyError, ValueError, iso8601.ParseError):
        return NotFoundResponse(request, errormsg="Bad request")

    user = get_object_or_404(Person, userid=request.user.username)
    local_tz = pytz.timezone(settings.TIME_ZONE)
    start = st - datetime.timedelta(days=1)
    end = en + datetime.timedelta(days=1)

    resp = HttpResponse(content_type="application/json")
    events = _calendar_event_data(user, start, end, local_tz, dt_string=True, colour=True,
            due_before=datetime.timedelta(minutes=1), due_after=datetime.timedelta(minutes=30))
    json.dump(list(events), resp, indent=1)
    return resp 
Example #5
Source File: sfm_ui_consumer.py    From sfm-ui with MIT License 6 votes vote down vote up
def _on_warc_created_message(self):
        try:
            log.debug("Warc with id %s", self.message["warc"]["id"])
            # Create warc model object
            warc = Warc.objects.create(
                harvest=Harvest.objects.get(harvest_id=self.message["harvest"]["id"]),
                warc_id=self.message["warc"]["id"],
                path=self.message["warc"]["path"],
                sha1=self.message["warc"]["sha1"],
                bytes=self.message["warc"]["bytes"],
                date_created=iso8601.parse_date(self.message["warc"]["date_created"])
            )
            warc.save()

        except ObjectDoesNotExist:
            log.error("Harvest model object not found for harvest status message: %s",
                      json.dumps(self.message, indent=4)) 
Example #6
Source File: utils.py    From openprocurement.auction with Apache License 2.0 6 votes vote down vote up
def get_time(item):
    """
    >>> date = get_time({"time": "2015-01-04T15:40:44Z"}) # doctest: +NORMALIZE_WHITESPACE
    >>> date.utctimetuple()  # doctest: +NORMALIZE_WHITESPACE
    time.struct_time(tm_year=2015, tm_mon=1, tm_mday=4, tm_hour=15, tm_min=40,
                     tm_sec=44, tm_wday=6, tm_yday=4, tm_isdst=0)

    >>> date = get_time({"date": "2015-01-04T15:40:44Z"})
    >>> date.utctimetuple()  # doctest: +NORMALIZE_WHITESPACE
    time.struct_time(tm_year=2015, tm_mon=1, tm_mday=4, tm_hour=15, tm_min=40,
                     tm_sec=44, tm_wday=6, tm_yday=4, tm_isdst=0)

    >>> date = get_time({})
    >>> date.utctimetuple()  # doctest: +NORMALIZE_WHITESPACE
    time.struct_time(tm_year=0, tm_mon=12, tm_mday=31, tm_hour=21, tm_min=58,
                     tm_sec=0, tm_wday=6, tm_yday=366, tm_isdst=0)
    """
    if item.get('time', ''):
        bid_time = iso8601.parse_date(item['time'])
    elif item.get('date', ''):
        bid_time = iso8601.parse_date(item['date'])
    else:
        bid_time = datetime(MINYEAR, 1, 1, tzinfo=timezone('Europe/Kiev'))
    return bid_time 
Example #7
Source File: idphandler.py    From flask-saml2 with MIT License 6 votes vote down vote up
def validate_response(self, response: ResponseParser):
        # Check it came from the right place
        if self.entity_id != response.issuer:
            raise CannotHandleAssertion(
                f'Entity ID mismatch {self.entity_id} != {response.issuer}')

        if response.conditions is not None:
            # Validate the NotBefore/NotOnOrAfter tags
            now = utcnow()
            not_before = response.conditions.get('NotBefore')
            not_on_or_after = response.conditions.get('NotOnOrAfter')
            try:
                if not_before is not None and now < iso8601.parse_date(not_before):
                    raise CannotHandleAssertion(f'NotBefore={not_before} check failed')
                if not_on_or_after is not None and now >= iso8601.parse_date(not_on_or_after):
                    raise CannotHandleAssertion(f'NotOnOrAfter={not_on_or_after} check failed')
            except ValueError as err:
                raise CannotHandleAssertion("Could not parse date") from err

            # Validate the AudienceRestriction elements, if they exist
            audiences = response._xpath(response.conditions, './saml:AudienceRestriction/saml:Audience')
            entity_id = self.sp.get_sp_entity_id()
            if len(audiences) and not any(el.text == entity_id for el in audiences):
                raise CannotHandleAssertion("No valid AudienceRestriction found") 
Example #8
Source File: models.py    From rdmo with Apache License 2.0 6 votes vote down vote up
def value(self):
        if self.option:
            value = self.option.text or ''
            if self.option.additional_input and self.text:
                value += ': ' + self.text
            return value

        elif self.text:
            if self.value_type == VALUE_TYPE_DATETIME:
                try:
                    return iso8601.parse_date(self.text).date()
                except iso8601.ParseError:
                    return self.text
            elif self.value_type == VALUE_TYPE_BOOLEAN:
                if self.text == '1':
                    return _('Yes')
                else:
                    return _('No')
            else:
                return self.text
        else:
            return None 
Example #9
Source File: test_datastore.py    From aw-core with Mozilla Public License 2.0 6 votes vote down vote up
def test_create_bucket(datastore):
    name = "A label/name for a test bucket"
    bid = "test-identifier"
    try:
        bucket = datastore.create_bucket(
            bucket_id=bid,
            type="testtype",
            client="testclient",
            hostname="testhost",
            name=name,
            created=now,
        )
        assert bid == bucket.metadata()["id"]
        assert name == bucket.metadata()["name"]
        assert "testtype" == bucket.metadata()["type"]
        assert "testclient" == bucket.metadata()["client"]
        assert "testhost" == bucket.metadata()["hostname"]
        assert now == iso8601.parse_date(bucket.metadata()["created"])
        assert bid in datastore.buckets()
    finally:
        datastore.delete_bucket(bid)
    assert bid not in datastore.buckets() 
Example #10
Source File: test_query2.py    From aw-core with Mozilla Public License 2.0 6 votes vote down vote up
def test_query2_return_value():
    qname = "asd"
    starttime = iso8601.parse_date("1970-01-01")
    endtime = iso8601.parse_date("1970-01-02")
    example_query = "RETURN = 1;"
    result = query(qname, example_query, starttime, endtime, None)
    assert result == 1

    example_query = "RETURN = 'testing 123'"
    result = query(qname, example_query, starttime, endtime, None)
    assert result == "testing 123"

    example_query = "RETURN = {'a': 1}"
    result = query(qname, example_query, starttime, endtime, None)
    assert result == {"a": 1}

    # Nothing to return
    with pytest.raises(QueryParseException):
        example_query = "a=1"
        result = query(qname, example_query, starttime, endtime, None) 
Example #11
Source File: test_query2.py    From aw-core with Mozilla Public License 2.0 6 votes vote down vote up
def test_query2_query_function_calling():
    qname = "asd"
    starttime = iso8601.parse_date("1970-01-01")
    endtime = iso8601.parse_date("1970-01-02")

    # Function which doesn't exist
    with pytest.raises(QueryInterpretException):
        example_query = "RETURN = asd();"
        query(qname, example_query, starttime, endtime, None)

    # Function which does exist with invalid arguments
    with pytest.raises(QueryInterpretException):
        example_query = "RETURN = nop(badarg);"
        query(qname, example_query, starttime, endtime, None)

    # Function which does exist with valid arguments
    example_query = "RETURN = nop();"
    query(qname, example_query, starttime, endtime, None) 
Example #12
Source File: sample_additional_info.py    From soweego with GNU General Public License v3.0 6 votes vote down vote up
def get_date_strings(timestamp, precision):
    """Given a timestamp and a wikidata date precision, returns a combination of strings"""
    if timestamp:
        timesplit = timestamp.split('^')[0]
        precisionsplit = precision.split('^')[0]
        date = iso8601.parse_date(timesplit).date()

        if precisionsplit == "11":
            return [
                str(date.year),
                '%s%s' % (date.year, date.month),
                '%s%s%s' % (date.year, date.month, date.day),
            ]
        elif precisionsplit == "10":
            return [str(date.year), '%s%s' % (date.year, date.month)]
        else:
            return [str(date.year)]
    else:
        return [] 
Example #13
Source File: models.py    From aw-core with Mozilla Public License 2.0 6 votes vote down vote up
def _timestamp_parse(ts_in: ConvertableTimestamp) -> datetime:
    """
    Takes something representing a timestamp and
    returns a timestamp in the representation we want.
    """
    ts = iso8601.parse_date(ts_in) if isinstance(ts_in, str) else ts_in
    # Set resolution to milliseconds instead of microseconds
    # (Fixes incompability with software based on unix time, for example mongodb)
    ts = ts.replace(microsecond=int(ts.microsecond / 1000) * 1000)
    # Add timezone if not set
    if not ts.tzinfo:
        # Needed? All timestamps should be iso8601 so ought to always contain timezone.
        # Yes, because it is optional in iso8601
        logger.warning("timestamp without timezone found, using UTC: {}".format(ts))
        ts = ts.replace(tzinfo=timezone.utc)
    return ts 
Example #14
Source File: process_forest.py    From process-forest with Apache License 2.0 6 votes vote down vote up
def deserialize(self, f):
        s = f.read()
        data = json.loads(s)

        def complexify_process(p):
            process = Process(p["pid"], p["ppid"], p["cmdline"], p["ppname"], p["hashes"], p["path"], p["user"], p["domain"], p["logonid"], p["computer"],
                    pid_formatter=self.pid_formatter)
            process.begin = iso8601.parse_date(p["begin"]).replace(tzinfo=None)
            process.end = iso8601.parse_date(p["end"]).replace(tzinfo=None)
            process.parent = p["parent"]
            process.children = p["children"]
            process.notes = p["notes"]
            process.id = p["id"]
            return process

        self._defs = {p["id"]:complexify_process(p) for p in data["definitions"].values()}
        self._roots = data["roots"] 
Example #15
Source File: jsonschema2db.py    From jsonschema2db with MIT License 6 votes vote down vote up
def _coerce_type(self, t, value):
        ''' Returns a two-tuple (is_valid, new_value) where new_value is properly coerced. '''
        try:
            if t == 'number':
                return type(value) != bool, float(value)
            elif t == 'integer':
                return type(value) != bool, int(value)
            elif t == 'boolean':
                return type(value) == bool, value
            elif t == 'timestamp':
                if type(value) == datetime.datetime:
                    return True, value
                return True, iso8601.parse_date(value)
            elif t == 'date':
                if type(value) == datetime.date:
                    return True, value
                return True, datetime.date(*(int(z) for z in value.split('-')))
            elif t == 'string':
                # Allow coercing ints/floats, but nothing else
                return type(value) in [str, int, float], str(value)
            elif t == 'enum':
                return type(value) == str, str(value)
        except:
            pass
        return False, None 
Example #16
Source File: models.py    From mirandum with Apache License 2.0 6 votes vote down vote up
def as_dict(self):
        details = json.loads(self.details)
        name = "Anonymous Donor"
        if 'supporterDetails' in details['snippet']:
            name = details['snippet']['supporterDetails']['displayName']
        datetime = iso8601.parse_date(details['snippet']['createdAt'])    
        info = {
            # general 
            'name': name,
            'comment': details['snippet'].get('commentText', ""),
            'donation_amount': float(details['snippet']['amountMicros']) / 1000000.,
            'currency': details['snippet']['currency'],
            
            # Display-friendly
            'amount': details['snippet']['displayString'],
            
            # Filtering friendly
            'amount_micros': int(details['snippet']['amountMicros']),

            'timestamp': datetime,
        }
        return info 
Example #17
Source File: models.py    From mirandum with Apache License 2.0 6 votes vote down vote up
def as_dict(self):
        details = json.loads(self.details)
        name = "Anonymous Donor"
        if 'donorName' in details and details['donorName']:
            name = details['donorName']
        datetime = iso8601.parse_date(details['createdOn'])    
        info = {
            # general 
            'name': name,
            'comment': details.get('message', "") or '',
            'donation_amount': float(details['donationAmount']),
            'currency': 'USD',
            # Display-friendly
            'amount': "$%.2f" % details['donationAmount'],
            'timestamp': datetime,
        }
        return info 
Example #18
Source File: test_sfm_ui_consumer.py    From sfm-ui with MIT License 5 votes vote down vote up
def test_warc_created_on_message(self):
        self.consumer.routing_key = "warc_created"
        self.consumer.message = {
            "warc": {
                "path": "/var/folders/_d/3zzlntjs45nbq1f4dnv48c499mgzyf/T/tmpKwq9NL/test_collection_set/2015/07/28/"
                        "11/" +
                        "test_collection_set-flickr-2015-07-28T11:17:36Z.warc.gz",
                "sha1": "7512e1c227c29332172118f0b79b2ca75cbe8979",
                "bytes": 26146,
                "id": "test_collection-flickr-2015-07-28T11:17:36Z",
                "date_created": "2015-07-28T11:17:36.640178"
            },
            "collection_set": {
                "path": "/var/folders/_d/3zzlntjs45nbq1f4dnv48c499mgzyf/T/tmpKwq9NL/test_collection_set",
                "id": "test_collection_set"
            },
            "harvest": {
                "id": "test:1",
            }
        }
        # Trigger on_message
        self.consumer.on_message()

        # Check created Warc model object
        warc = Warc.objects.get(warc_id="test_collection-flickr-2015-07-28T11:17:36Z")
        self.assertEqual(self.consumer.message["warc"]["path"], warc.path)
        self.assertEqual(self.consumer.message["warc"]["sha1"], warc.sha1)
        self.assertEqual(self.consumer.message["warc"]["bytes"], warc.bytes)
        self.assertEqual(iso8601.parse_date("2015-07-28T11:17:36.640178"), warc.date_created)
        self.assertEqual(self.harvest, warc.harvest) 
Example #19
Source File: test_query2.py    From aw-core with Mozilla Public License 2.0 5 votes vote down vote up
def test_query2_function_invalid_argument_count():
    qname = "asd"
    starttime = iso8601.parse_date("1970-01-01")
    endtime = iso8601.parse_date("1970-01-02")
    example_query = "RETURN=nop(nop())"
    with pytest.raises(QueryInterpretException):
        result = query(qname, example_query, starttime, endtime, None) 
Example #20
Source File: test_query2.py    From aw-core with Mozilla Public License 2.0 5 votes vote down vote up
def test_query2_multiline():
    qname = "asd"
    starttime = iso8601.parse_date("1970-01-01")
    endtime = iso8601.parse_date("1970-01-02")
    example_query = """
my_multiline_string = "a
b";
RETURN = my_multiline_string;
    """
    result = query(qname, example_query, starttime, endtime, None)
    assert result == "a\nb" 
Example #21
Source File: CAP.py    From RPiNWR with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, dom):
        #       self.status = dom.find('{urn:oasis:names:tc:emergency:cap:1.1}status').text
        #       self.id = dom.find('{http://www.w3.org/2005/Atom}id').text
        #       self.published, self.updated = [
        #           iso8601.parse_date(self.__dom.find('{http://www.w3.org/2005/Atom}' + x).text).timestamp() for x in
        #           ('published', 'updated')]

        k = None
        for x in dom.iter():
            if x.tag == '{http://www.w3.org/2005/Atom}valueName':
                k = x.text.strip()
            elif k is not None and x.tag == '{http://www.w3.org/2005/Atom}value':
                self.__dict__[k] = CAPMessage.__parse_date_or_text(x.text)
            elif x.tag not in ["{urn:oasis:names:tc:emergency:cap:1.1}geocode",
                               "{urn:oasis:names:tc:emergency:cap:1.1}parameter"]:
                self.__dict__[x.tag[x.tag.find('}') + 1:]] = CAPMessage.__parse_date_or_text(x.text)

        if self.FIPS6:
            self.FIPS6 = re.sub("[\n\t ] +", " ", self.FIPS6.strip()).split(" ")

        if self.polygon is not None and len(self.polygon.strip()) > 0:
            self.polygon = re.sub("[\n\t ] +", " ", self.polygon)
            self.polygon = Polygon([(float(x), float(y)) for x, y in [x.split(",") for x in self.polygon.split(" ")]])
        else:
            self.polygon = None

        try:
            if self.__dict__['VTEC'] and len(self.VTEC):
                self.vtec = VTEC.VTEC(self.VTEC, self)
            else:
                self.vtec = (NOVTEC(dom, self),)
            if len(self.vtec) == 0:  # True if there was an invalid vtec code
                self.vtec = (NOVTEC(dom, self),)
        except KeyError:
            self.vtec = (NOVTEC(dom, self),) 
Example #22
Source File: peering.py    From kopf with MIT License 5 votes vote down vote up
def __init__(
            self,
            id: str,
            *,
            name: str,
            priority: int = 0,
            lastseen: Optional[str] = None,
            lifetime: int = 60,
            namespace: Optional[str] = None,
            legacy: bool = False,
            **_: Any,  # for the forward-compatibility with the new fields
    ):
        super().__init__()
        self.id = id
        self.name = name
        self.namespace = namespace
        self.priority = priority
        self.lifetime = (lifetime if isinstance(lifetime, datetime.timedelta) else
                         datetime.timedelta(seconds=int(lifetime)))
        self.lastseen = (lastseen if isinstance(lastseen, datetime.datetime) else
                         iso8601.parse_date(lastseen) if lastseen is not None else
                         datetime.datetime.utcnow())
        self.lastseen = self.lastseen.replace(tzinfo=None)  # only the naive utc -- for comparison
        self.deadline = self.lastseen + self.lifetime
        self.is_dead = self.deadline <= datetime.datetime.utcnow()
        self.legacy = legacy 
Example #23
Source File: deviceTypes.py    From iot-python with Eclipse Public License 1.0 5 votes vote down vote up
def created(self):
        return iso8601.parse_date(self["created"]) 
Example #24
Source File: peewee.py    From aw-core with Mozilla Public License 2.0 5 votes vote down vote up
def json(self):
        return {
            "id": self.id,
            "created": iso8601.parse_date(self.created)
            .astimezone(timezone.utc)
            .isoformat(),
            "name": self.name,
            "type": self.type,
            "client": self.client,
            "hostname": self.hostname,
        } 
Example #25
Source File: deviceTypes.py    From iot-python with Eclipse Public License 1.0 5 votes vote down vote up
def updated(self):
        return iso8601.parse_date(self["updated"]) 
Example #26
Source File: thingTypes.py    From iot-python with Eclipse Public License 1.0 5 votes vote down vote up
def created(self):
        return iso8601.parse_date(self["created"]) 
Example #27
Source File: sn_fuse.py    From standardnotes-fs with GNU General Public License v3.0 5 votes vote down vote up
def note_attr(self, path):
        note, note_name, uuid = self._path_to_note(path)
        st = self.note_stat
        st['st_size'] = len(note['text'])
        st['st_ino'] = note['inode'] + INODE_OFFSET
        st['st_ctime'] = iso8601.parse_date(note['created']).timestamp()
        st['st_mtime'] = iso8601.parse_date(note['modified']).timestamp()
        return st 
Example #28
Source File: thingTypes.py    From iot-python with Eclipse Public License 1.0 5 votes vote down vote up
def updated(self):
        return iso8601.parse_date(self["updated"]) 
Example #29
Source File: test_rest.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def _strtime_to_httpdate(dt):
        return email_utils.formatdate(calendar.timegm(
            iso8601.parse_date(dt).timetuple()), usegmt=True) 
Example #30
Source File: test_query2.py    From aw-core with Mozilla Public License 2.0 5 votes vote down vote up
def test_query2_function_in_function(datastore):
    qname = "asd"
    bid = "test_bucket"
    starttime = iso8601.parse_date("1970-01-01")
    endtime = iso8601.parse_date("1970-01-02")
    example_query = """
    RETURN=limit_events(query_bucket("{bid}"), 1);
    """.format(
        bid=bid
    )
    try:
        # Setup buckets
        bucket1 = datastore.create_bucket(
            bucket_id=bid, type="test", client="test", hostname="test", name="test"
        )
        # Prepare buckets
        e1 = Event(data={}, timestamp=starttime, duration=timedelta(seconds=1))
        e2 = Event(
            data={},
            timestamp=starttime + timedelta(seconds=1),
            duration=timedelta(seconds=1),
        )
        bucket1.insert(e1)
        result = query(qname, example_query, starttime, endtime, datastore)
        assert 1 == len(result)
    finally:
        datastore.delete_bucket(bid)