Python datetime.datetime.fromisoformat() Examples
The following are 30
code examples of datetime.datetime.fromisoformat().
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
datetime.datetime
, or try the search function
.
Example #1
Source File: transform_probes.py From probe-scraper with Mozilla Public License 2.0 | 6 votes |
def update_or_add_item(repo_items, commit_hash, item, definition, commit_timestamps, equal_fn, type_ctor): # If we've seen this item before, check previous definitions if item in repo_items: prev_defns = repo_items[item][HISTORY_KEY] max_defn_i = max(range(len(prev_defns)), key=lambda i: datetime.fromisoformat(prev_defns[i][DATES_KEY]["last"])) max_defn = prev_defns[max_defn_i] # If equal to previous commit, update date and commit on existing definition if equal_fn(definition, max_defn): new_defn = make_item_defn(max_defn, commit_hash, commit_timestamps) repo_items[item][HISTORY_KEY][max_defn_i] = new_defn # Otherwise, prepend changed definition for existing item else: new_defn = make_item_defn(definition, commit_hash, commit_timestamps) repo_items[item][HISTORY_KEY] = prev_defns + [new_defn] # We haven't seen this item before, add it else: defn = make_item_defn(definition, commit_hash, commit_timestamps) repo_items[item] = type_ctor(defn, item) return repo_items
Example #2
Source File: innerutils.py From pyecore with BSD 3-Clause "New" or "Revised" License | 6 votes |
def parse_date(str_date): try: return datetime.fromisoformat(str_date) except Exception: formats = ('%Y-%m-%dT%H:%M:%S.%f%z', '%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%dT%H:%M', '%Y-%m-%d %H:%M:%S.%f%z', '%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', '%Y-%m-%d',) for format in formats: with ignored(ValueError): return datetime.strptime(str_date, format) raise ValueError('Date format is unknown')
Example #3
Source File: util.py From avwx-engine with MIT License | 6 votes |
def datetime_parser(data: dict) -> dict: """ Convert ISO strings into datetime objects """ for key, val in data.items(): if isinstance(val, str): if "+00:00" in val: try: data[key] = datetime.fromisoformat(val) except ValueError: pass try: data[key] = datetime.strptime(val, r"%Y-%m-%d") except ValueError: pass return data
Example #4
Source File: main.py From deck-chores with ISC License | 6 votes |
def inspect_running_containers() -> datetime: log.info("Inspecting running containers.") last_event_time = datetime.utcnow() containers = cfg.client.containers.list(ignore_removed=True, sparse=True) for container in containers: container_id = container.id data = cfg.client.api.inspect_container(container_id) last_event_time = max( last_event_time, # not sure why mypy doesn't know about this method: datetime.fromisoformat(data['State']['StartedAt'][:26]), # type: ignore ) process_started_container_labels( container_id, paused=container.status == 'paused' ) log.debug('Finished inspection of running containers.') return last_event_time
Example #5
Source File: plano_recorrente_automatico.py From pygseguro with GNU Affero General Public License v3.0 | 6 votes |
def criar_no_pagseguro(self) -> 'PlanoAutomaticoRecorrente': """ Cria um plano automático na conta do pagseguro :return: código do plano criado """ headers = { 'Content-Type': 'application/json;charset=UTF-8', 'Accept': 'application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1' } response = requests.post(self._config.construir_url('/pre-approvals/request'), json=self._main_data, headers=headers) codigo_data = response.json() if codigo_data.get('error', False): raise PagseguroException(codigo_data, response.status_code) dt = datetime.fromisoformat(codigo_data['date']).astimezone(pytz.UTC) return PlanoAutomaticoRecorrente(codigo_data['code'], dt)
Example #6
Source File: fitbit.py From personal-influxdb with Apache License 2.0 | 6 votes |
def process_levels(levels): for level in levels: type = level['level'] if type == "asleep": type = "light" if type == "restless": type = "rem" if type == "awake": type = "wake" time = datetime.fromisoformat(level['dateTime']) utc_time = LOCAL_TIMEZONE.localize(time).astimezone(pytz.utc).isoformat() points.append({ "measurement": "sleep_levels", "time": utc_time, "fields": { "seconds": int(level['seconds']) } })
Example #7
Source File: fitbit.py From personal-influxdb with Apache License 2.0 | 6 votes |
def fetch_data(category, type): try: response = requests.get('https://api.fitbit.com/1/user/-/' + category + '/' + type + '/date/today/1d.json', headers={'Authorization': 'Bearer ' + FITBIT_ACCESS_TOKEN, 'Accept-Language': FITBIT_LANGUAGE}) response.raise_for_status() except requests.exceptions.HTTPError as err: print("HTTP request failed: %s" % (err)) sys.exit() data = response.json() print("Got " + type + " from Fitbit") for day in data[category.replace('/', '-') + '-' + type]: points.append({ "measurement": type, "time": LOCAL_TIMEZONE.localize(datetime.fromisoformat(day['dateTime'])).astimezone(pytz.utc).isoformat(), "fields": { "value": float(day['value']) } })
Example #8
Source File: test_types.py From cjworkbench with GNU Affero General Public License v3.0 | 6 votes |
def test_arrow_datetime_column(self): dataframe, columns = arrow_table_to_dataframe( arrow_table( { "A": pyarrow.array( [dt.fromisoformat("2019-09-17T21:21:00.123456"), None], type=pyarrow.timestamp(unit="ns", tz=None), ) }, [atypes.Column("A", atypes.ColumnType.Datetime())], ) ) assert_frame_equal( dataframe, pd.DataFrame( {"A": ["2019-09-17T21:21:00.123456Z", None]}, dtype="datetime64[ns]" ), ) self.assertEqual(columns, [Column("A", ColumnType.DATETIME())])
Example #9
Source File: test_types.py From cjworkbench with GNU Affero General Public License v3.0 | 6 votes |
def test_dataframe_datetime_column(self): assert_arrow_table_equals( dataframe_to_arrow_table( pd.DataFrame( {"A": ["2019-09-17T21:21:00.123456Z", None]}, dtype="datetime64[ns]" ), [Column("A", ColumnType.DATETIME())], self.path, ), arrow_table( { "A": pyarrow.array( [dt.fromisoformat("2019-09-17T21:21:00.123456"), None], type=pyarrow.timestamp(unit="ns", tz=None), ) }, [atypes.Column("A", atypes.ColumnType.Datetime())], ), )
Example #10
Source File: mm.py From dataclasses-json with MIT License | 5 votes |
def _deserialize(self, value, attr, data, **kwargs): if value is not None: return datetime.fromisoformat(value) else: if not self.required: return None else: raise ValidationError(self.default_error_messages["required"])
Example #11
Source File: metadata.py From pikepdf with Mozilla Public License 2.0 | 5 votes |
def fromisoformat(datestr): # strptime %z can't parse a timezone with punctuation if re.search(r'[+-]\d{2}[-:]\d{2}$', datestr): datestr = datestr[:-3] + datestr[-2:] try: return datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%S%z") except ValueError: return datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%S")
Example #12
Source File: metadata.py From pikepdf with Mozilla Public License 2.0 | 5 votes |
def docinfo_from_xmp(xmp_val): if xmp_val.endswith('Z'): xmp_val = xmp_val[:-1] + '+00:00' dateobj = fromisoformat(xmp_val) return encode_pdf_date(dateobj)
Example #13
Source File: helpers.py From aws-sso with Apache License 2.0 | 5 votes |
def credentials(self, credentials): if isinstance(credentials['Expiration'], str): credentials['Expiration'] = datetime.fromisoformat(credentials['Expiration']) self._credentials = credentials
Example #14
Source File: courses.py From richie with MIT License | 5 votes |
def format_es_object_for_api(es_course, language=None): """ Format a course stored in ES into a consistent and easy-to-consume record for API consumers """ language = language or translation.get_language() source = es_course["_source"] # Prepare the state state = es_course["fields"]["state"][0] try: state["date_time"] = datetime.fromisoformat(state["date_time"]) except KeyError: state["date_time"] = None return { **{ field: get_best_field_language(source[field], language) for field in [ "absolute_url", "cover_image", "duration", "effort", "icon", "title", ] }, "id": es_course["_id"], "categories": source["categories"], "organization_highlighted": get_best_field_language( source["organization_highlighted"], language ) if source.get("organization_highlighted", None) else None, "organizations": source["organizations"], "state": CourseState(**state), }
Example #15
Source File: deletedmessage_filters.py From site with MIT License | 5 votes |
def footer_datetime(timestamp: str) -> datetime: """Takes an embed timestamp and returns a timezone-aware datetime object.""" return datetime.fromisoformat(timestamp)
Example #16
Source File: builder.py From alpine-ec2-ami with MIT License | 5 votes |
def force_iso_date(input): return datetime.fromisoformat(input).isoformat(timespec="seconds")
Example #17
Source File: utils.py From dask-gateway with BSD 3-Clause "New" or "Revised" License | 5 votes |
def parse_k8s_timestamp(ts): t = datetime.fromisoformat(ts[:-1]) return int(t.replace(tzinfo=timezone.utc).timestamp() * 1000)
Example #18
Source File: tasks.py From ipygee with MIT License | 5 votes |
def fromisoformat(iso): """ for python versions < 3.7 get datetime from isoformat """ d, t = iso.split('T') year, month, day = d.split('-') hours, minutes, seconds = t.split(':') seconds = float(seconds[0:-1]) sec = int(seconds) microseconds = int((seconds-sec)*1e6) return datetime(int(year), int(month), int(day), int(hours), int(minutes), sec, microseconds)
Example #19
Source File: edsm.py From personal-influxdb with Apache License 2.0 | 5 votes |
def add_jump(src, dst): global points system = fetch_system(dst['system']) if 'type' in system['primaryStar']: points.append({ "measurement": "jump", "time": datetime.fromisoformat(dst['date']).isoformat(), "tags": { "commander": EDSM_COMMANDER_NAME, "system": dst['system'], "firstDiscover": dst['firstDiscover'], "primaryStarType": system['primaryStar']['type'] }, "fields": { "distance": distance(src['system'], dst['system']), "x": float(system['coords']['x']), "y": float(system['coords']['y']), "z": float(system['coords']['z']) } }) else: points.append({ "measurement": "jump", "time": datetime.fromisoformat(dst['date']).isoformat(), "tags": { "commander": EDSM_COMMANDER_NAME, "system": dst['system'], "firstDiscover": dst['firstDiscover'] }, "fields": { "distance": distance(src['system'], dst['system']), "x": float(system['coords']['x']), "y": float(system['coords']['y']), "z": float(system['coords']['z']) } })
Example #20
Source File: git.py From TagBot with MIT License | 5 votes |
def time_of_commit(self, sha: str) -> datetime: """Get the time that a commit was made.""" # The format %cI is "committer date, strict ISO 8601 format". date = self.command("show", "-s", "--format=%cI", sha) dt = datetime.fromisoformat(date) # Convert to UTC and remove time zone information. offset = dt.utcoffset() if offset: dt -= offset return dt.replace(tzinfo=None)
Example #21
Source File: github_data_loader.py From LDG with Educational Community License v2.0 | 5 votes |
def iso_parse(dt): # return datetime.fromisoformat(dt) # python >= 3.7 return dateutil.parser.isoparse(dt)
Example #22
Source File: user_update_eligibility.py From TWLight with MIT License | 5 votes |
def handle(self, *args, **options): wp_editcount_updated = now() if options["datetime"]: wp_editcount_updated = datetime.fromisoformat(options["datetime"]) editors = Editor.objects.filter(wp_bundle_eligible=True) for editor in editors: if options["global_userinfo"]: global_userinfo = options["global_userinfo"] else: global_userinfo = editor_global_userinfo( editor.wp_username, editor.wp_sub, True ) if global_userinfo: editor.wp_editcount_prev_updated, editor.wp_editcount_prev, editor.wp_editcount_recent, editor.wp_enough_recent_edits = editor_recent_edits( global_userinfo["editcount"], editor.wp_editcount_updated, editor.wp_editcount_prev_updated, editor.wp_editcount_prev, editor.wp_editcount_recent, editor.wp_enough_recent_edits, ) editor.wp_editcount = global_userinfo["editcount"] editor.wp_editcount_updated = wp_editcount_updated editor.wp_enough_edits = editor_enough_edits(editor.wp_editcount) editor.wp_not_blocked = editor_not_blocked(global_userinfo["merged"]) editor.wp_valid = editor_valid( editor.wp_enough_edits, # We could recalculate this, but we would only need to do that if upped the minimum required account age. editor.wp_account_old_enough, # editor.wp_not_blocked can only be rechecked on login, so we're going with the existing value. editor.wp_not_blocked, editor.ignore_wp_blocks, ) editor.wp_bundle_eligible = editor_bundle_eligible( editor.wp_valid, editor.wp_enough_recent_edits ) editor.save() editor.update_bundle_authorization()
Example #23
Source File: database.py From trusat-backend with Apache License 2.0 | 5 votes |
def datetime_from_sqldatetime(sql_date_string): """ The 4 digit sub-seconds are not the standard 3 or 6, which creates problems with datetime.fromisoformat """ date_format = '%Y-%m-%d %H:%M:%S.%f' return datetime.strptime(sql_date_string, date_format)
Example #24
Source File: nginx.py From integrations-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
def fromisoformat(ts): return datetime.strptime(ts, "%Y-%m-%dT%H:%M:%S")
Example #25
Source File: event.py From betterreads with MIT License | 5 votes |
def updated_at(self): """Event updated at""" return datetime.fromisoformat(self._event_dict["updated_at"]["#text"])
Example #26
Source File: date_accessor.py From koku with GNU Affero General Public License v3.0 | 5 votes |
def __init__(self): """Initializer.""" if Config.MASU_DATE_OVERRIDE and Config.DEBUG and DateAccessor.mock_date_time is None: # python-dateutil is needed in Python <=3.6.x; # in Python 3.7 there is datetime.fromisoformat() DateAccessor.mock_date_time = parser.parse(Config.MASU_DATE_OVERRIDE) if DateAccessor.mock_date_time.tzinfo is None: DateAccessor.mock_date_time = DateAccessor.mock_date_time.replace(tzinfo=pytz.UTC) LOG.info("Initializing masu date/time to %s", str(DateAccessor.mock_date_time))
Example #27
Source File: git_scraper.py From probe-scraper with Mozilla Public License 2.0 | 5 votes |
def retrieve_files(repo_info, cache_dir): results = defaultdict(list) timestamps = dict() base_path = os.path.join(cache_dir, repo_info.name) min_date = None if repo_info.name in MIN_DATES: min_date = utc_timestamp(datetime.fromisoformat(MIN_DATES[repo_info.name])) if os.path.exists(repo_info.name): shutil.rmtree(repo_info.name) repo = Repo.clone_from(repo_info.url, repo_info.name) repo.git.checkout(repo_info.branch) try: for rel_path in repo_info.get_change_files(): hashes = get_commits(repo, rel_path) for _hash, (ts, index) in hashes.items(): if (min_date and ts < min_date): continue disk_path = os.path.join(base_path, _hash, rel_path) if not os.path.exists(disk_path): contents = get_file_at_hash(repo, _hash, rel_path) dir = os.path.split(disk_path)[0] if not os.path.exists(dir): os.makedirs(dir) with open(disk_path, 'wb') as f: f.write(contents.encode("UTF-8")) results[_hash].append(disk_path) timestamps[_hash] = (ts, index) except Exception: # without this, the error will be silently discarded raise finally: shutil.rmtree(repo_info.name) return timestamps, results
Example #28
Source File: test_rw_model.py From fastapi-realworld-example-app with MIT License | 5 votes |
def test_api_datetime_is_in_realworld_format() -> None: dt = datetime.fromisoformat("2019-10-27T02:21:42.844640") assert convert_datetime_to_realworld(dt) == "2019-10-27T02:21:42.844640Z"
Example #29
Source File: defcon.py From bot with MIT License | 5 votes |
def _defcon_action(self, ctx: Context, days: int, action: Action) -> None: """Providing a structured way to do an defcon action.""" try: response = await self.bot.api_client.get('bot/bot-settings/defcon') data = response['data'] if "enable_date" in data and action is Action.DISABLED: enabled = datetime.fromisoformat(data["enable_date"]) delta = datetime.now() - enabled self.bot.stats.timing("defcon.enabled", delta) except Exception: pass error = None try: await self.bot.api_client.put( 'bot/bot-settings/defcon', json={ 'name': 'defcon', 'data': { # TODO: retrieve old days count 'days': days, 'enabled': action is not Action.DISABLED, 'enable_date': datetime.now().isoformat() } } ) except Exception as err: log.exception("Unable to update DEFCON settings.") error = err finally: await ctx.send(self.build_defcon_msg(action, error)) await self.send_defcon_log(action, ctx.author, error) self.bot.stats.gauge("defcon.threshold", days)
Example #30
Source File: math.py From cccatalog with MIT License | 5 votes |
def _validate_percentiles(percentiles, popularity_fields): if not percentiles: return False valid = True for popfield in popularity_fields: if popfield not in percentiles['percentiles']: log.info('Percentile cache missing expected field. Recomputing.') valid = False now = datetime.utcnow() if datetime.fromisoformat(percentiles['expires']) < now: log.info('Percentile cache expired and needs to be recomputed.') valid = False return valid