Python datetime.datetime.strptime() Examples

The following are 30 code examples of datetime.datetime.strptime(). 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: getmessages_file_replay.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #2
Source File: mondrian_test.py    From Mondrian with MIT License 6 votes vote down vote up
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 #3
Source File: importer.py    From pydfs-lineup-optimizer with MIT License 6 votes vote down vote up
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 #4
Source File: attime.py    From worker with GNU General Public License v3.0 6 votes vote down vote up
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 #5
Source File: getlogs_hadoop-mapreduce.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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
Source File: getmessages_prometheus.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #7
Source File: insightagent-boilerplate.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #8
Source File: __main__.py    From gw2pvo with MIT License 6 votes vote down vote up
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 #9
Source File: client.py    From clashroyale with MIT License 6 votes vote down vote up
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 #10
Source File: nba_scraper.py    From nba_scraper with GNU General Public License v3.0 6 votes vote down vote up
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 #11
Source File: reportMetrics.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #12
Source File: backuper.py    From MySQL-AutoXtraBackup with MIT License 6 votes vote down vote up
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 #13
Source File: getmetrics_cadvisor.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #14
Source File: api.py    From razzy-spinner with GNU General Public License v3.0 6 votes vote down vote up
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 #15
Source File: getmessages_elasticsearch2.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #16
Source File: getlogs_spark.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #17
Source File: PseudoDailyScheduleController.py    From pseudo-channel with GNU General Public License v3.0 6 votes vote down vote up
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 #18
Source File: getlogs_tcpdump.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #19
Source File: getlogs_servicenow.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #20
Source File: getlogs_evtx.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #21
Source File: getlogs_k8s.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #22
Source File: getlogs_k8s.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #23
Source File: getmetrics_cadvisor.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #24
Source File: getmetrics_sar.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
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 #25
Source File: remote.py    From fitbit-googlefit with GNU General Public License v3.0 5 votes vote down vote up
def SyncFitbitSleepToGoogleFit(self, date_stamp):
		"""
		Sync sleep data for a given day from Fitbit to Google fit.

		date_stamp -- timestamp in yyyy-mm-dd format of the start day
		"""
		dataSourceId = self.convertor.GetDataSourceId('sleep')
		date_obj = self.convertor.parseHumanReadableDate(date_stamp)

		# Get sleep data for a given date
		fitbitSleeps = self.ReadFromFitbit(self.fitbitClient.get_sleep,date_obj)['sleep']

		# Iterate over each sleep log for that day
		sleep_count = 0
		for fit_sleep in fitbitSleeps:
			minute_points = fit_sleep['minuteData']
			sleep_count += 1

			# save first time stamp for comparison
			start_time = minute_points[0]['dateTime']
			# convert to date, add 1 day, convert back to string
			next_date_stamp = (datetime.strptime(date_stamp, DATE_FORMAT) + timedelta(1)).strftime(DATE_FORMAT)

			# convert all fitbit data points to google fit data points
			googlePoints = [self.convertor.ConvertFibitPoint((date_stamp if start_time <= point['dateTime'] else \
				next_date_stamp),point,'sleep') for point in minute_points]

			# 1. Write a fit session about sleep
			google_session = self.convertor.ConvertGFitSleepSession(googlePoints, fit_sleep['logId'])
			self.WriteSessionToGoogleFit(google_session)

			# 2. create activity segment data points for the activity
			self.WriteToGoogleFit(dataSourceId, googlePoints)

		print("synced sleep - {} logs".format(sleep_count)) 
Example #26
Source File: Utils.py    From gphotos-sync with MIT License 5 votes vote down vote up
def date_string_normalize(
    date_in: str, pattern_in: PatType, pattern_out: str
) -> datetime:
    result = None
    matches = pattern_in.match(date_in)
    if matches:
        normalized = pattern_out.format(*matches.groups())
        result = datetime.strptime(normalized, DATE_FORMAT)
    return result 
Example #27
Source File: misc.py    From pymoo with Apache License 2.0 5 votes vote down vote up
def time_to_int(t):
    td = datetime.strptime(t, '%H:%M:%S') - datetime(1900, 1, 1)
    return td.total_seconds() 
Example #28
Source File: backuper.py    From MySQL-AutoXtraBackup with MIT License 5 votes vote down vote up
def clean_old_archives(self):
        logger.info("Starting cleaning of old archives")
        for archive in self.sorted_ls(self.archive_dir):
            if '_archive' in archive:
                archive_date = datetime.strptime(
                    archive, "%Y-%m-%d_%H-%M-%S_archive")
            else:
                archive_date = datetime.strptime(
                    archive, "%Y-%m-%d_%H-%M-%S.tar.gz")

            now = datetime.now()

            # Finding if last full backup older than the interval or more from
            # now!
            cleanup_msg = "Removing archive {}/{} due to {}"
            if hasattr(self, 'archive_max_duration') and (now - archive_date).total_seconds() >= self.archive_max_duration:
                logger.info(cleanup_msg.format(self.archive_dir, archive, 'archive_max_duration exceeded.'))
                if os.path.isdir(self.archive_dir + "/" + archive):
                    shutil.rmtree(self.archive_dir + "/" + archive)
                else:
                    os.remove(self.archive_dir + "/" + archive)
            elif hasattr(self, 'archive_max_size') and self.get_directory_size(self.archive_dir) > self.archive_max_size:
                logger.info(cleanup_msg.format(self.archive_dir, archive, 'archive_max_size exceeded.'))
                if os.path.isdir(self.archive_dir + "/" + archive):
                    shutil.rmtree(self.archive_dir + "/" + archive)
                else:
                    os.remove(self.archive_dir + "/" + archive) 
Example #29
Source File: utils.py    From python-wp with MIT License 5 votes vote down vote up
def parse_iso8601(string):
    return datetime.strptime(string, '%Y-%m-%dT%H:%M:%S') 
Example #30
Source File: logdir_helpers.py    From imgcomp-cvpr with GNU General Public License v3.0 5 votes vote down vote up
def is_log_date(possible_log_date):
    try:
        datetime.strptime(possible_log_date, _LOG_DATE_FORMAT)
        return True
    except ValueError:
        return False