Python datetime.datetime.strptime() Examples
The following are 30 code examples for showing how to use datetime.datetime.strptime(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
datetime.datetime
, or try the search function
.
Example 1
Project: nba_scraper Author: mcbarlowe File: nba_scraper.py License: GNU General Public License v3.0 | 6 votes |
def check_valid_dates(from_date, to_date): """ Check if it's a valid date range. If not raise ValueError Inputs: date_from - Date to scrape form date_to - Date to scrape to Outputs: """ try: if datetime.strptime(to_date, "%Y-%m-%d") < datetime.strptime( from_date, "%Y-%m-%d" ): raise ValueError( "Error: The second date input is earlier than the first one" ) except ValueError: raise ValueError( "Error: Incorrect format given for dates. They must be given like 'yyyy-mm-dd' (ex: '2016-10-01')." )
Example 2
Project: InsightAgent Author: insightfinder File: getmetrics_cadvisor.py License: Apache License 2.0 | 6 votes |
def strip_tz_info(timestamp_format): # strptime() doesn't allow timezone info if '%Z' in timestamp_format: position = timestamp_format.index('%Z') strip_tz_fmt = PCT_Z_FMT if '%z' in timestamp_format: position = timestamp_format.index('%z') strip_tz_fmt = PCT_z_FMT if len(timestamp_format) > (position + 2): timestamp_format = timestamp_format[:position] + timestamp_format[position+2:] else: timestamp_format = timestamp_format[:position] if cli_config_vars['time_zone'] == pytz.timezone('UTC'): logger.warning('Time zone info will be stripped from timestamps, but no time zone info was supplied in the config. Assuming UTC') return {'strip_tz': True, 'strip_tz_fmt': strip_tz_fmt, 'timestamp_format': timestamp_format}
Example 3
Project: InsightAgent Author: insightfinder File: getmetrics_cadvisor.py License: Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(PCT_z_FMT.split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except e: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example 4
Project: InsightAgent Author: insightfinder File: getlogs_k8s.py License: Apache License 2.0 | 6 votes |
def strip_tz_info(timestamp_format): # strptime() doesn't allow timezone info if '%Z' in timestamp_format: position = timestamp_format.index('%Z') strip_tz_fmt = PCT_Z_FMT if '%z' in timestamp_format: position = timestamp_format.index('%z') strip_tz_fmt = PCT_z_FMT if len(timestamp_format) > (position + 2): timestamp_format = timestamp_format[:position] + timestamp_format[position+2:] else: timestamp_format = timestamp_format[:position] if cli_config_vars['time_zone'] == pytz.timezone('UTC'): logger.warning('Time zone info will be stripped from timestamps, but no time zone info was supplied in the config. Assuming UTC') return {'strip_tz': True, 'strip_tz_fmt': strip_tz_fmt, 'timestamp_format': timestamp_format}
Example 5
Project: InsightAgent Author: insightfinder File: getlogs_k8s.py License: Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example 6
Project: InsightAgent Author: insightfinder File: getlogs_evtx.py License: Apache License 2.0 | 6 votes |
def strip_tz_info(timestamp_format): # strptime() doesn't allow timezone info if '%Z' in timestamp_format: position = timestamp_format.index('%Z') strip_tz_fmt = PCT_Z_FMT if '%z' in timestamp_format: position = timestamp_format.index('%z') strip_tz_fmt = PCT_z_FMT if len(timestamp_format) > (position + 2): timestamp_format = timestamp_format[:position] + timestamp_format[position+2:] else: timestamp_format = timestamp_format[:position] if cli_config_vars['time_zone'] == pytz.timezone('UTC'): logger.warning('Time zone info will be stripped from timestamps, but no time zone info was supplied in the config. Assuming UTC') return {'strip_tz': True, 'strip_tz_fmt': strip_tz_fmt, 'timestamp_format': timestamp_format}
Example 7
Project: InsightAgent Author: insightfinder File: getlogs_servicenow.py License: Apache License 2.0 | 6 votes |
def strip_tz_info(timestamp_format): # strptime() doesn't allow timezone info if '%Z' in timestamp_format: position = timestamp_format.index('%Z') strip_tz_fmt = PCT_Z_FMT if '%z' in timestamp_format: position = timestamp_format.index('%z') strip_tz_fmt = PCT_z_FMT if len(timestamp_format) > (position + 2): timestamp_format = timestamp_format[:position] + timestamp_format[position+2:] else: timestamp_format = timestamp_format[:position] return {'strip_tz': True, 'strip_tz_fmt': strip_tz_fmt, 'timestamp_format': [timestamp_format]}
Example 8
Project: InsightAgent Author: insightfinder File: getlogs_spark.py License: Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except e: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example 9
Project: InsightAgent Author: insightfinder File: getmessages_elasticsearch2.py License: Apache License 2.0 | 6 votes |
def get_datetime_from_date_string(date_string): if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: for timestamp_format in agent_config_vars['timestamp_format']: try: if timestamp_format == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, timestamp_format) break except Exception as e: logger.info('timestamp {} does not match {}'.format( date_string, timestamp_format)) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = ['epoch'] return timestamp_datetime
Example 10
Project: InsightAgent Author: insightfinder File: getmessages_file_replay.py License: Apache License 2.0 | 6 votes |
def strip_tz_info(timestamp_format): # strptime() doesn't allow timezone info if '%Z' in timestamp_format: position = timestamp_format.index('%Z') strip_tz_fmt = PCT_Z_FMT if '%z' in timestamp_format: position = timestamp_format.index('%z') strip_tz_fmt = PCT_z_FMT if len(timestamp_format) > (position + 2): timestamp_format = timestamp_format[:position] + timestamp_format[position+2:] else: timestamp_format = timestamp_format[:position] return {'strip_tz': True, 'strip_tz_fmt': strip_tz_fmt, 'timestamp_format': [timestamp_format]}
Example 11
Project: InsightAgent Author: insightfinder File: getlogs_tcpdump.py License: Apache License 2.0 | 6 votes |
def strip_tz_info(timestamp_format): # strptime() doesn't allow timezone info if '%Z' in timestamp_format: position = timestamp_format.index('%Z') strip_tz_fmt = PCT_Z_FMT if '%z' in timestamp_format: position = timestamp_format.index('%z') strip_tz_fmt = PCT_z_FMT if len(timestamp_format) > (position + 2): timestamp_format = timestamp_format[:position] + timestamp_format[position+2:] else: timestamp_format = timestamp_format[:position] if cli_config_vars['time_zone'] == pytz.timezone('UTC'): logger.warning('Time zone info will be stripped from timestamps, but no time zone info was supplied in the config. Assuming UTC') return {'strip_tz': True, 'strip_tz_fmt': strip_tz_fmt, 'timestamp_format': [timestamp_format]}
Example 12
Project: InsightAgent Author: insightfinder File: getmetrics_sar.py License: Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example 13
Project: InsightAgent Author: insightfinder File: reportMetrics.py License: Apache License 2.0 | 6 votes |
def update_data_start_time(): if "FileReplay" in parameters['mode'] and reporting_config_vars['prev_endtime'] != "0" and len( reporting_config_vars['prev_endtime']) >= 8: start_time = reporting_config_vars['prev_endtime'] # pad a second after prev_endtime start_time_epoch = 1000 + long(1000 * time.mktime(time.strptime(start_time, "%Y%m%d%H%M%S"))); end_time_epoch = start_time_epoch + 1000 * 60 * reporting_config_vars['reporting_interval'] elif reporting_config_vars['prev_endtime'] != "0": start_time = reporting_config_vars['prev_endtime'] # pad a second after prev_endtime start_time_epoch = 1000 + long(1000 * time.mktime(time.strptime(start_time, "%Y%m%d%H%M%S"))); end_time_epoch = start_time_epoch + 1000 * 60 * reporting_config_vars['reporting_interval'] else: # prev_endtime == 0 end_time_epoch = int(time.time()) * 1000 start_time_epoch = end_time_epoch - 1000 * 60 * reporting_config_vars['reporting_interval'] return start_time_epoch # update prev_endtime in config file
Example 14
Project: InsightAgent Author: insightfinder File: insightagent-boilerplate.py License: Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except e: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example 15
Project: InsightAgent Author: insightfinder File: getmessages_prometheus.py License: Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example 16
Project: InsightAgent Author: insightfinder File: getlogs_hadoop-mapreduce.py License: Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example 17
Project: gw2pvo Author: markruys File: __main__.py License: MIT License | 6 votes |
def copy(settings): # Fetch readings from GoodWe date = datetime.strptime(settings.date, "%Y-%m-%d") gw = gw_api.GoodWeApi(settings.gw_station_id, settings.gw_account, settings.gw_password) data = gw.getDayReadings(date) if settings.pvo_system_id and settings.pvo_api_key: if settings.darksky_api_key: ds = ds_api.DarkSkyApi(settings.darksky_api_key) temperatures = ds.get_temperature_for_day(data['latitude'], data['longitude'], date) else: temperatures = None # Submit readings to PVOutput pvo = pvo_api.PVOutputApi(settings.pvo_system_id, settings.pvo_api_key) pvo.add_day(data['entries'], temperatures) else: for entry in data['entries']: logging.info("{}: {:6.0f} W {:6.2f} kWh".format( entry['dt'], entry['pgrid_w'], entry['eday_kwh'], )) logging.warning("Missing PVO id and/or key")
Example 18
Project: Mondrian Author: qiyuangong File: mondrian_test.py License: MIT License | 6 votes |
def test_mondrian_datetime(self): d1 = datetime.strptime("2007-03-04 21:08:12", "%Y-%m-%d %H:%M:%S") d2 = datetime.strptime("2008-03-04 21:08:12", "%Y-%m-%d %H:%M:%S") d3 = datetime.strptime("2009-03-04 21:08:12", "%Y-%m-%d %H:%M:%S") d4 = datetime.strptime("2007-03-05 21:08:12", "%Y-%m-%d %H:%M:%S") data = [[6, d1, 'haha'], [8, d1, 'haha'], [8, d1, 'test'], [8, d1, 'haha'], [8, d1, 'test'], [4, d1, 'hha'], [4, d2, 'hha'], [4, d3, 'hha'], [4, d4, 'hha']] result, eval_r = mondrian(data, 2, False) print(eval_r)
Example 19
Project: clashroyale Author: cgrok File: client.py License: MIT License | 6 votes |
def get_datetime(self, timestamp: str, unix=True): """Converts a %Y%m%dT%H%M%S.%fZ to a UNIX timestamp or a datetime.datetime object Parameters --------- timestamp: str A timstamp in the %Y%m%dT%H%M%S.%fZ format, usually returned by the API in the ``created_time`` field for example (eg. 20180718T145906.000Z) unix: Optional[bool] = True Whether to return a POSIX timestamp (seconds since epoch) or not Returns int or datetime.datetime """ time = datetime.strptime(timestamp, '%Y%m%dT%H%M%S.%fZ') if unix: return int(time.timestamp()) else: return time
Example 20
Project: MySQL-AutoXtraBackup Author: ShahriyarR File: backuper.py License: MIT License | 6 votes |
def last_full_backup_date(self): """ Check if last full backup date retired or not. :return: 1 if last full backup date older than given interval, 0 if it is newer. """ # Finding last full backup date from dir/folder name max_dir = self.recent_full_backup_file() dir_date = datetime.strptime(max_dir, "%Y-%m-%d_%H-%M-%S") now = datetime.now() # Finding if last full backup older than the interval or more from now! if (now - dir_date).total_seconds() >= self.full_backup_interval: return 1 else: return 0
Example 21
Project: pseudo-channel Author: justinemter File: PseudoDailyScheduleController.py License: GNU General Public License v3.0 | 6 votes |
def check_for_end_time(self, datalist): currentTime = datetime.now() """c.execute("SELECT * FROM daily_schedule") datalist = list(c.fetchall()) """ for row in datalist: try: endTime = datetime.strptime(row[9], '%Y-%m-%d %H:%M:%S.%f') except ValueError: endTime = datetime.strptime(row[9], '%Y-%m-%d %H:%M:%S') if currentTime.hour == endTime.hour: if currentTime.minute == endTime.minute: if currentTime.second == endTime.second: if self.DEBUG: print("Ok end time found") self.write_schedule_to_file(self.get_html_from_daily_schedule(None, None, datalist)) self.write_xml_to_file(self.get_xml_from_daily_schedule(None, None, datalist)) self.write_refresh_bool_to_file() break
Example 22
Project: razzy-spinner Author: rafasashi File: api.py License: GNU General Public License v3.0 | 6 votes |
def check_date_limit(self, data, verbose=False): """ Validate date limits. """ if self.upper_date_limit or self.lower_date_limit: date_fmt = '%a %b %d %H:%M:%S +0000 %Y' tweet_date = \ datetime.strptime(data['created_at'], date_fmt).replace(tzinfo=UTC) if (self.upper_date_limit and tweet_date > self.upper_date_limit) or \ (self.lower_date_limit and tweet_date < self.lower_date_limit): if self.upper_date_limit: message = "earlier" date_limit = self.upper_date_limit else: message = "later" date_limit = self.lower_date_limit if verbose: print("Date limit {0} is {1} than date of current tweet {2}".\ format(date_limit, message, tweet_date)) self.do_stop = True
Example 23
Project: worker Author: moira-alert File: attime.py License: GNU General Public License v3.0 | 6 votes |
def parseATTime(s, tzinfo=None): if tzinfo is None: tzinfo = pytz.utc s = s.strip().lower().replace('_', '').replace(',', '').replace(' ', '') if s.isdigit(): if len(s) == 8 and int(s[:4]) > 1900 and int( s[4:6]) < 13 and int(s[6:]) < 32: pass # Fall back because its not a timestamp, its YYYYMMDD form else: return datetime.fromtimestamp(int(s), tzinfo) elif ':' in s and len(s) == 13: return tzinfo.localize(datetime.strptime(s, '%H:%M%Y%m%d'), daylight) if '+' in s: ref, offset = s.split('+', 1) offset = '+' + offset elif '-' in s: ref, offset = s.split('-', 1) offset = '-' + offset else: ref, offset = s, '' return ( parseTimeReference(ref) + parseTimeOffset(offset)).astimezone(tzinfo)
Example 24
Project: pydfs-lineup-optimizer Author: DimaKudosh File: importer.py License: MIT License | 6 votes |
def _parse_game_info(self, row: Dict) -> Optional[GameInfo]: game_info = row.get('Game Info') if not game_info: return None if game_info in ('In Progress', 'Final'): return GameInfo( # No game info provided, just mark game as started home_team='', away_team='', starts_at='', game_started=True) try: teams, date, time, tz = game_info.rsplit(' ', 3) away_team, home_team = teams.split('@') starts_at = datetime.strptime(date + time, '%m/%d/%Y%I:%M%p').\ replace(tzinfo=timezone(get_timezone())) return GameInfo( home_team=home_team, away_team=away_team, starts_at=starts_at, game_started=False ) except ValueError: return None
Example 25
Project: incubator-spot Author: apache File: common.py License: Apache License 2.0 | 5 votes |
def coerce_date(value): if isinstance(value, date): return value elif isinstance(value, datetime): return value.date() elif isinstance(value, int): return date.utcfromtimestamp(value) else: return datetime.strptime(str(value), '%Y-%m-%d').date()
Example 26
Project: incubator-spot Author: apache File: common.py License: Apache License 2.0 | 5 votes |
def serialize_date(value): return datetime.strptime(value, '%Y-%m-%d').strftime('%Y-%m-%d')
Example 27
Project: incubator-spot Author: apache File: common.py License: Apache License 2.0 | 5 votes |
def parse_date_literal(ast): return datetime.strptime(ast.value, '%Y-%m-%d')
Example 28
Project: incubator-spot Author: apache File: common.py License: Apache License 2.0 | 5 votes |
def serialize_datetime(value): if not isinstance(value, datetime): value = datetime.strptime(str(value), '%Y-%m-%d %H:%M:%S') return value.strftime('%Y-%m-%d %H:%M:%S')
Example 29
Project: incubator-spot Author: apache File: common.py License: Apache License 2.0 | 5 votes |
def parse_datetime_literal(ast): return datetime.strptime(ast.value, '%Y-%m-%d %H:%M:%S')
Example 30
Project: aegea Author: kislyuk File: models.py License: Apache License 2.0 | 5 votes |
def _strptime(self, time_str): """Convert an ISO 8601 formatted string in UTC into a timezone-aware datetime object.""" if time_str: # Parse UTC string into naive datetime, then add timezone dt = datetime.strptime(time_str, __timeformat__) return dt.replace(tzinfo=UTC()) return None