Python django.utils.dateparse.parse_datetime() Examples
The following are 30
code examples of django.utils.dateparse.parse_datetime().
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
django.utils.dateparse
, or try the search function
.
Example #1
Source File: validator.py From rssant with BSD 3-Clause "New" or "Revised" License | 7 votes |
def datetime_validator(compiler, format='%Y-%m-%dT%H:%M:%S.%fZ', output_object=False): def validate(value): try: if not isinstance(value, datetime.datetime): value = parse_datetime(value) if value is None: raise Invalid('not well formatted datetime') if not timezone.is_aware(value): value = timezone.make_aware(value, timezone=timezone.utc) # https://bugs.python.org/issue13305 if value.year < 1000: raise Invalid('not support datetime before year 1000') if value.year > 2999: raise Invalid('not support datetime after year 2999') if output_object: return value else: return value.strftime(format) except Invalid: raise except Exception as ex: raise Invalid('invalid datetime') from ex return validate
Example #2
Source File: models.py From waliki with BSD 3-Clause "New" or "Revised" License | 6 votes |
def history(self, page): GIT_COMMIT_FIELDS = ['commit', 'author', 'date', 'date_relative', 'date_parsed', 'message'] GIT_LOG_FORMAT = '%x1f'.join(['%h', '%an', '%ad', '%ar', '%ai', '%s']) + '%x1e' output = git.log('--format=%s' % GIT_LOG_FORMAT, '--follow', '-z', '--shortstat', page.abspath) output = output.split('\n') history = [] for line in output: if '\x1f' in line: log = line.strip('\x1e\x00').split('\x1f') history.append(dict(zip(GIT_COMMIT_FIELDS, log))) else: insertion = re.match(r'.* (\d+) insertion', line) deletion = re.match(r'.* (\d+) deletion', line) history[-1]['insertion'] = int(insertion.group(1)) if insertion else 0 history[-1]['deletion'] = int(deletion.group(1)) if deletion else 0 max_changes = float(max([(v['insertion'] + v['deletion']) for v in history])) or 1.0 for v in history: v.update({'insertion_relative': str((v['insertion'] / max_changes) * 100), 'deletion_relative': str((v['deletion'] / max_changes) * 100), 'date_parsed': parse_datetime('%s %s%s' % tuple(v['date_parsed'].split()))}) return history
Example #3
Source File: resources.py From chain-api with MIT License | 6 votes |
def sanitize_field_value(cls, field_name, value): if field_name == 'value': return float(value) if field_name == 'timestamp': from django.db import models if value == None: return timezone.now() timestamp = parse_datetime(value) if timezone.is_aware(timestamp): return timestamp return timezone.make_aware(timestamp, timezone.get_current_timezone()) # we store the metric as a tag in Influx for convenience of querying # for clients that are using influx directly. It's not a real field that's # handled by Chain
Example #4
Source File: views.py From scale with Apache License 2.0 | 6 votes |
def get_v6(self, request): """The v6 version to get high level status information :param request: the HTTP GET request :type request: :class:`rest_framework.request.Request` :rtype: :class:`rest_framework.response.Response` :returns: the HTTP response to send back to the user """ status_dict = Scheduler.objects.get_master().status if not status_dict: # Empty dict from model initialization raise ServiceUnavailable(unicode('Status is missing. Scheduler may be down.')) # If status dict has not been updated recently, assume scheduler is down or slow status_timestamp = parse_datetime(status_dict['timestamp']) if (now() - status_timestamp).total_seconds() > StatusView.STATUS_FRESHNESS_THRESHOLD: raise ServiceUnavailable(unicode('Status is over %d seconds old' % StatusView.STATUS_FRESHNESS_THRESHOLD)) return Response(status_dict)
Example #5
Source File: views.py From monasca-ui with Apache License 2.0 | 6 votes |
def transform_alarm_history(results, name, ts_mode, ts_offset): new_list = [] def get_ts_val(val): if ts_mode == 'bl': offset = int((ts_offset or '0').replace('+', '')) dt_val = parse_datetime(val) + timedelta(hours=offset) dt_val_formatter = dt_val.strftime('%Y-%m-%dT%H:%M:%S.%fZ') return dt_val_formatter.replace('000Z', '') elif ts_mode != 'utc': raise ValueError('%s is not supported timestamp format' % ts_mode) else: return val # utc case for item in results: new_list.append({'alarm_id': item['alarm_id'], 'name': name, 'old_state': item['old_state'], 'new_state': item['new_state'], 'timestamp': get_ts_val(item['timestamp']), 'reason': item['reason'], 'metrics': item['metrics'], 'reason_data': item['reason_data']}) return new_list
Example #6
Source File: imagestore.py From esdc-ce with Apache License 2.0 | 6 votes |
def __init__(self, manifest, manifest_url): dict.__init__(self) self['manifest'] = manifest self['manifest_url'] = manifest_url self['uuid'] = manifest['uuid'] self['name'] = manifest['name'] self['version'] = manifest.get('version', '') self['created'] = parse_datetime(manifest.get('published_at', '')) or '' self['ostype'] = Image.os_to_ostype(manifest) self['desc'] = manifest.get('description', '') self['homepage'] = manifest.get('homepage') self['size'] = manifest.get('image_size', Image.DEFAULT_SIZE) self['state'] = manifest.get('state', '') try: self['download_size'] = manifest['files'][0]['size'] except (KeyError, IndexError): self['download_size'] = 0
Example #7
Source File: sync_deleted_instances_fix.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def handle(self, *args, **kwargs): # Reset all sql deletes to None Instance.objects.exclude( deleted_at=None, xform__downloadable=True).update(deleted_at=None) # Get all mongo deletes query = '{"$and": [{"_deleted_at": {"$exists": true}}, ' \ '{"_deleted_at": {"$ne": null}}]}' query = json.loads(query) xform_instances = settings.MONGO_DB.instances cursor = xform_instances.find(query) for record in cursor: # update sql instance with deleted_at datetime from mongo try: i = Instance.objects.get( uuid=record["_uuid"], xform__downloadable=True) except Instance.DoesNotExist: continue else: deleted_at = parse_datetime(record["_deleted_at"]) if not timezone.is_aware(deleted_at): deleted_at = timezone.make_aware( deleted_at, timezone.utc) i.set_deleted(deleted_at)
Example #8
Source File: task_results.py From scale with Apache License 2.0 | 6 votes |
def get_task_run_length(self, task_type): """Returns the run time length for the given task type, possibly None :param task_type: The task type :type task_type: string :returns: The task run time length :rtype: :class:`datetime.timedelta`: """ for task_dict in self._task_results['tasks']: if task_dict['type'] == task_type: if 'started' in task_dict and 'ended' in task_dict: started = dateparse.parse_datetime(task_dict['started']) ended = dateparse.parse_datetime(task_dict['ended']) return ended - started return None
Example #9
Source File: test_config_settings.py From product-database with MIT License | 5 votes |
def test_write_datetime_to_settings_file(self): settings = AppSettings() # get a configuration value (default in the global section) now = datetime.now() value = now.isoformat() settings.set_cisco_eox_api_auto_sync_last_execution_time(value) read_value = settings.get_cisco_eox_api_auto_sync_last_execution_time() assert value == read_value assert now == parse_datetime(read_value)
Example #10
Source File: test_email.py From kpi with GNU Affero General Public License v3.0 | 5 votes |
def test_notifications(self): self._create_periodic_task() first_log_response = self._send_and_fail() failures_reports.delay() self.assertEqual(len(mail.outbox), 1) expected_record = { "username": self.asset.owner.username, "email": self.asset.owner.email, "language": "en", "assets": { self.asset.uid: { "name": self.asset.name, "max_length": len(self.hook.name), "logs": [{ "hook_name": self.hook.name, "status_code": first_log_response.get("status_code"), "message": first_log_response.get("message"), "uid": first_log_response.get("uid"), "date_modified": dateparse.parse_datetime(first_log_response.get("date_modified")) }] } } } plain_text_template = get_template("reports/failures_email_body.txt") variables = { "username": expected_record.get("username"), "assets": expected_record.get("assets"), 'kpi_base_url': settings.KPI_URL } # Localize templates translation.activate(expected_record.get("language")) text_content = plain_text_template.render(variables) self.assertEqual(mail.outbox[0].body, text_content)
Example #11
Source File: credential.py From aries-vcr with Apache License 2.0 | 5 votes |
def process_config_date(cls, config, credential, field_name): date_value = cls.process_mapping(config.get(field_name), credential) date_result = None if date_value: try: # could be seconds since epoch date_result = datetime.utcfromtimestamp(int(date_value)) except ValueError: # Django method to parse a date string. Must be in ISO8601 format try: date_result = parse_datetime(date_value) if not date_result: date_result = parse_date(date_value) if not date_result: raise ValueError() date_result = datetime.combine(date_result, datetime.min.time()) date_result = timezone.make_aware(date_result) except re.error: raise CredentialException( "Error parsing {}: {}".format(field_name, date_value) ) except ValueError: raise CredentialException( "Credential {} is invalid: {}".format(field_name, date_value) ) if not date_result.tzinfo: # interpret as UTC date_result = date_result.replace(tzinfo=timezone.utc) else: # convert to UTC date_result = date_result.astimezone(timezone.utc) return date_result
Example #12
Source File: models.py From digihel with MIT License | 5 votes |
def events(self, future=False): try: events = self._event_methods[self.data_source](self) except (TimeoutError, ConnectionError, LookupError): # if the event source is unreachable or down or data is invalid events = [] if not future: return json.dumps(events) # the methods are assumed to return events latest first, reverse the order tz = pytz.timezone(settings.TIME_ZONE) for event in events: # for future filtering, make sure all events have end times not null try: end = event['end_time'] if not end: event['end_time'] = event['start_time'] except LookupError: event['end_time'] = event['start_time'] # check the datetimes first start = dateparse.parse_datetime(event['start_time']) end = dateparse.parse_datetime(event['end_time']) # linkedevents may not have exact times, parse_datetime may fail # we have to append time, assume server time zone and convert to utc for filtering if not start: start = tz.localize(datetime.combine(dateparse.parse_date(event['start_time']), time())) event['start_time'] = start.astimezone(pytz.utc).strftime('%Y-%m-%dT%H:%M:%SZ') if not end: end = tz.localize(datetime.combine(dateparse.parse_date(event['end_time']), time(23,59,59))) event['end_time'] = end.astimezone(pytz.utc).strftime('%Y-%m-%dT%H:%M:%SZ') # we want the next event first return json.dumps(list(reversed([event for event in events if dateparse.parse_datetime(event['end_time']) > datetime.now(tz)])))
Example #13
Source File: utils.py From connect with MIT License | 5 votes |
def create_open(data, headers): """Create and save the new EmailOpen object""" # We must import EmailOpen here to avoid nasty import problems from open_connect.mailer.models import EmailOpen open_object = EmailOpen() open_object.email = data['e'] open_object.timestamp = parse_datetime(data['t']) open_object.key = data['k'] # If there is a notification ID, it needs to be an integer if 'n' in data: open_object.notification = int(data['n']) ip_addresses = headers.get( 'HTTP_X_FORWARDED_FOR', headers.get('REMOTE_ADDR')).split(',') if ip_addresses: open_object.ip_address = ip_addresses[0] user_agent = headers.get('HTTP_USER_AGENT', '') open_object.user_agent = user_agent[0:1000] raw_referrer = headers.get('HTTP_REFERER', None) if raw_referrer: referrer = force_text(raw_referrer, errors='replace') parsed_referrer = urlparse.urlparse(referrer) open_object.referrer_netloc = parsed_referrer.netloc[0:1000] open_object.referrer = referrer[0:1000] if user_agent: operating_system, browser, device = process_useragent(user_agent) open_object.operating_system = operating_system open_object.browser = browser open_object.device_family = device # Create the object open_object.save()
Example #14
Source File: fields.py From esdc-ce with Apache License 2.0 | 5 votes |
def from_native(self, value): if value in validators.EMPTY_VALUES: return None if isinstance(value, datetime.datetime): return value if isinstance(value, datetime.date): value = datetime.datetime(value.year, value.month, value.day) if settings.USE_TZ: # For backwards compatibility, interpret naive datetimes in # local time. This won't work during DST change, but we can't # do much about it, so we let the exceptions percolate up the # call stack. warnings.warn("DateTimeField received a naive datetime (%s)" " while time zone support is active." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) return value for fmt in self.input_formats: if fmt.lower() == ISO_8601: try: parsed = parse_datetime(value) except (ValueError, TypeError): pass else: if parsed is not None: return parsed else: try: parsed = datetime.datetime.strptime(value, fmt) except (ValueError, TypeError): pass else: return parsed msg = self.error_messages['invalid'] % readable_datetime_formats(self.input_formats) raise ValidationError(msg)
Example #15
Source File: gui_utils.py From esdc-ce with Apache License 2.0 | 5 votes |
def dtparse(s): return parse_datetime(s)
Example #16
Source File: views.py From eoj3 with MIT License | 5 votes |
def post(self, request, *args, **kwargs): start_time = parse_datetime(request.POST["time"]) redirect_link = reverse('contest:dashboard', kwargs={'cid': self.contest.pk}) if start_time <= datetime.now(): messages.error(request, "输入时间应晚于当前时间。") return redirect(redirect_link) if start_time - datetime.now() > timedelta(days=1): messages.error(request, "不支持预约 24 小时以后的比赛。") return redirect(redirect_link) self.contest.contestparticipant_set.create(user=self.request.user, join_time=start_time) return redirect(redirect_link)
Example #17
Source File: practivity.py From gm_pr with Apache License 2.0 | 5 votes |
def __init__(self, date="", user="", event=""): if isinstance(date, datetime): self.date = date else: self.date = dateparse.parse_datetime(date) self.user = user self.event = event
Example #18
Source File: models.py From rocket-league-replays with GNU General Public License v3.0 | 5 votes |
def steam_info(self): steam = self.user.social_auth.get(provider='steam') # Have we updated this profile recently? if 'last_updated' in steam.extra_data: # Parse the last updated date. last_date = parse_datetime(steam.extra_data['last_updated']) seconds_ago = (now() - last_date).seconds # 3600 seconds = 1 hour if seconds_ago < 3600: return steam.extra_data['player'] try: player = requests.get(USER_INFO, params={ 'key': settings.SOCIAL_AUTH_STEAM_API_KEY, 'steamids': steam.uid }).json() if len(player['response']['players']) > 0: steam.extra_data = { 'player': player['response']['players'][0], 'last_updated': now().isoformat(), } steam.save() except: pass return steam.extra_data['player']
Example #19
Source File: operations.py From python2017 with MIT License | 5 votes |
def convert_datetimefield_value(self, value, expression, connection, context): if value is not None: if not isinstance(value, datetime.datetime): value = parse_datetime(value) if settings.USE_TZ and not timezone.is_aware(value): value = timezone.make_aware(value, self.connection.timezone) return value
Example #20
Source File: filters.py From product-definition-center with MIT License | 5 votes |
def _date_time_with_timezone(value): date_time = parse_datetime(value) if date_time is None: return value if timezone.is_aware(date_time): return date_time return timezone.make_aware(date_time)
Example #21
Source File: datetime_utils.py From django-htk with MIT License | 5 votes |
def parse_datetime(dt_str): return django_parse_datetime(dt_str)
Example #22
Source File: operations.py From openhgsenti with Apache License 2.0 | 5 votes |
def convert_datetimefield_value(self, value, expression, connection, context): if value is not None: if not isinstance(value, datetime.datetime): value = parse_datetime(value) if settings.USE_TZ: value = timezone.make_aware(value, self.connection.timezone) return value
Example #23
Source File: __init__.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def to_python(self, value): if value is None: return value if isinstance(value, datetime.datetime): return value if isinstance(value, datetime.date): value = datetime.datetime(value.year, value.month, value.day) if settings.USE_TZ: # For backwards compatibility, interpret naive datetimes in # local time. This won't work during DST change, but we can't # do much about it, so we let the exceptions percolate up the # call stack. warnings.warn("DateTimeField received a naive datetime (%s)" " while time zone support is active." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) return value try: parsed = parse_datetime(value) if parsed is not None: return parsed except ValueError: msg = self.error_messages['invalid_datetime'] % value raise exceptions.ValidationError(msg) try: parsed = parse_date(value) if parsed is not None: return datetime.datetime(parsed.year, parsed.month, parsed.day) except ValueError: msg = self.error_messages['invalid_date'] % value raise exceptions.ValidationError(msg) msg = self.error_messages['invalid'] % value raise exceptions.ValidationError(msg)
Example #24
Source File: base.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def parse_datetime_with_timezone_support(value): dt = parse_datetime(value) # Confirm that dt is naive before overwriting its tzinfo. if dt is not None and settings.USE_TZ and timezone.is_naive(dt): dt = dt.replace(tzinfo=timezone.utc) return dt
Example #25
Source File: basic.py From ontask_b with MIT License | 5 votes |
def __init__(self, form_data, *args, **kwargs): """Set item_column values.""" self.workflow = kwargs.pop('workflow', None) super().__init__(form_data, *args, **kwargs) self.set_fields_from_dict([ 'name', 'description_text', 'execute', 'frequency', 'execute_until']) self.fields['execute'].initial = parse_datetime( self.get_payload_field('execute', '')) self.fields['execute_until'].initial = parse_datetime( self.get_payload_field('execute_until', '')) self.fields['frequency'].label = '' self.fields['multiple_executions'].initial = bool( self.instance.frequency) self.order_fields([ 'name', 'description_text', 'execute', 'multiple_executions', 'frequency', 'execute_until'])
Example #26
Source File: plugin.py From ontask_b with MIT License | 5 votes |
def _create_param_fields(self): """Create the fields to capture the parameters.""" for idx, (lbl, p_type, p_allow, p_init, p_help) in enumerate( self.plugin_instance.parameters, ): if p_allow: new_field = forms.ChoiceField( choices=[(cval, cval) for cval in p_allow], required=False, label=lbl, help_text=p_help) else: new_field = self._create_datatype_field(p_type, p_help, lbl) # Set the initial value of each field if p_allow: new_field.initial = (p_init, p_init) else: if p_type == 'datetime': new_field.initial = parse_datetime(p_init) else: new_field.initial = p_init # Insert the new_field in the form self.fields[self.param_field_pattern % idx] = new_field
Example #27
Source File: column.py From ontask_b with MIT License | 5 votes |
def validate_column_value(cls, data_type, col_value) -> Any: """Check if a value is correct for a column. Test that a value is suitable to be stored in this column. It is done simply by casting the type and throwing the corresponding exception. :param data_type: string specifying the data type :param col_value: Value to store in the column :return: The new value to be stored """ # Remove spaces col_value = col_value.strip() distrib = { 'string': lambda txt_val: str(txt_val), 'double': lambda txt_val: float(txt_val), 'boolean': lambda txt_val: ( txt_val.lower() == 'true' or txt_val == 1), 'datetime': lambda txt_val: parse_datetime(txt_val), 'integer': None, } if data_type not in distrib.keys(): raise ValueError( _('Unsupported type %(type)s') % {'type': str(data_type)}) if data_type == 'integer': # In this case, although the column has been declared as an # integer, it could mutate to a float, so we allow this value. try: newval = int(col_value) except ValueError: newval = float(col_value) else: newval = distrib[data_type](col_value) return newval
Example #28
Source File: column.py From ontask_b with MIT License | 5 votes |
def get_categories(self): """Return the categories and parse datetime if needed. :return: List of values """ if self.data_type == 'datetime': return [parse_datetime(cat) for cat in self.categories] return self.categories
Example #29
Source File: test_timezone.py From django-peeringdb with Apache License 2.0 | 5 votes |
def test_conversion(self): input = "2020-02-04T20:34:14Z" parsed = parse_datetime(input) assert parsed.year == 2020 assert parsed.month == 2 assert parsed.day == 4 assert parsed.hour == 20 assert parsed.minute == 34 assert parsed.utcoffset().seconds == 0 stripped = parsed.replace(tzinfo=None) assert stripped.year == 2020 assert stripped.month == 2 assert stripped.day == 4 assert stripped.hour == 20 assert stripped.minute == 34 assert stripped.utcoffset() is None aware = make_aware(stripped) assert aware.year == 2020 assert aware.month == 2 assert aware.day == 4 assert aware.hour == 20 assert aware.minute == 34 assert aware.utcoffset().seconds == 0 naive = make_naive(aware, timezone=utc) assert naive.year == 2020 assert naive.month == 2 assert naive.day == 4 assert naive.hour == 20 assert naive.minute == 34 assert naive.utcoffset() is None
Example #30
Source File: test_models.py From django-konfera with MIT License | 5 votes |
def test_clean_dates(self): """ Cleaning models dates follow different type of rules, also depending on event """ event = Event.objects.get(title='PyCon SK 2016') # PyCon SK 2016 is in the past tt = TicketType(title='Test Ticket', price=12, event=event) # Past event and no dates raises error self.assertRaises(ValidationError, tt.clean) # Past event and dates after it raises error tt.date_from = now tt.date_to = now + 3 * day self.assertRaises(ValidationError, tt.clean) tt.date_from = parse_datetime('2016-01-11T09:00:00Z') self.assertRaises(ValidationError, tt.clean) # End date can not be before start date tt.date_to = tt.date_from - 3 * day self.assertRaises(ValidationError, tt.clean) # Correct data in the past before event, SAVE. tt.date_to = tt.date_from + 9 * day tt.clean() tt.save() # PyCon SK 2054 is in the future future_event = Event.objects.create( title='PyCon SK 2054', description='test', event_type=Event.MEETUP, status=Event.PUBLISHED, location=event.location, cfp_end=distant_future - 7 * day, date_from=distant_future, date_to=distant_future + 7 * day) ftt = TicketType(title='Test Future Ticket', price=120, event=future_event) # Future event pre-populate the dates ftt.clean() self.assertEquals(abs(ftt.date_from - timezone.now()).seconds, 0) self.assertEquals(abs(ftt.date_to - ftt.event.date_to).seconds, 0) ftt.save()