Python datetime.strptime() Examples

The following are code examples for showing how to use datetime.strptime(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: bigquerylayers   Author: smandaric   File: _helpers.py    GNU General Public License v3.0 6 votes vote down vote up
def _time_from_iso8601_time_naive(value):
    """Convert a zoneless ISO8601 time string to naive datetime time

    :type value: str
    :param value: The time string to convert

    :rtype: :class:`datetime.time`
    :returns: A datetime time object created from the string
    :raises ValueError: if the value does not match a known format.
    """
    if len(value) == 8:  # HH:MM:SS
        fmt = _TIMEONLY_NO_FRACTION
    elif len(value) == 15:  # HH:MM:SS.micros
        fmt = _TIMEONLY_W_MICROS
    else:
        raise ValueError("Unknown time format: {}".format(value))
    return datetime.datetime.strptime(value, fmt).time() 
Example 2
Project: neurips2019challenge   Author: tumeteor   File: loader.py    Apache License 2.0 6 votes vote down vote up
def list_filenames(directory, excluded_dates=None):
    """Auxilliary function which returns list of file names in directory in random order,
        filtered by excluded dates.

        Args.:
            directory (str): path to directory
            excluded_dates (list): list of dates which should not be included in result list,
                e.g., ['2018-01-01', '2018-12-31']

        Returns: list
    """
    filenames = os.listdir(directory)
    np.random.shuffle(filenames)

    # check if in excluded dates
    _excluded_dates = [datetime.datetime.strptime(x, '%Y-%m-%d').date() for x in excluded_dates] if excluded_dates \
        else None
    filenames = [x for x in filenames if return_date(x) not in _excluded_dates] if excluded_dates else filenames
    return filenames 
Example 3
Project: Spyder_cta   Author: nkuzhengwt   File: get_oir_c2.py    MIT License 6 votes vote down vote up
def this_contract(self,windSymbol):
        symbol = windSymbol.split('.')[0]
        def change_spot(y_month):
            weekday = datetime.strptime(y_month+'-01', "%Y-%m-%d").weekday()
            if weekday <= 5:
                return (14 + 5 - weekday)
            else:
                return (14 + 6)
        def this_month(date):
            day = np.int32(str(date)[6:8])
            if day >= change_spot(str(date)[:4]+'-'+str(date)[4:6]):
                month = np.int32(str(date)[4:6])
                if month == 12:
                    return str(np.int32(str(date)[2:4])+1)+'01'
                else:
                    return str(date)[2:4]+"%02d"%(month%12+1)
            else:
                return str(date)[2:6]
        self.tradeDateList[symbol+'_contract'] = \
         self.tradeDateList['tradeDate'].apply(lambda x : symbol+this_month(x))
        self.tradeDateList.to_csv(self.homePath +'tradeDateList.csv', index=None) 
Example 4
Project: docker-teleservices   Author: IMIO   File: town.py    GNU General Public License v3.0 6 votes vote down vote up
def diff_dates(self, date1, date2):
        try:
            try:
                d1 = datetime.strptime(date1, '%d/%m/%Y')
            except:
                d1 = datetime.combine(date1, datetime.min.time())
            d2 = datetime.strptime(date2, '%d/%m/%Y')
            diff = abs((d1 - d2).days)
            return str(diff)
        except:
            return "diff_dates_error"

    # Try to sum each row from a given column in a table (each value from the given column must be integer)
    # table_var : this is the variable name of the table in forms.
    # id_colonne : the id of the column in the table!
    # price : default value = 1.
    # return =  [sum of all row in given column] * price. 
Example 5
Project: buck-cpp-example   Author: njlr   File: gtest_xml_output_unittest.py    MIT License 6 votes vote down vote up
def testTimestampValue(self):
    """Checks whether the timestamp attribute in the XML output is valid.

    Runs a test program that generates an empty XML output, and checks if
    the timestamp attribute in the testsuites tag is valid.
    """
    actual = self._GetXmlOutput('gtest_no_test_unittest', [], 0)
    date_time_str = actual.documentElement.getAttributeNode('timestamp').value
    # datetime.strptime() is only available in Python 2.5+ so we have to
    # parse the expected datetime manually.
    match = re.match(r'(\d+)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)', date_time_str)
    self.assertTrue(
        re.match,
        'XML datettime string %s has incorrect format' % date_time_str)
    date_time_from_xml = datetime.datetime(
        year=int(match.group(1)), month=int(match.group(2)),
        day=int(match.group(3)), hour=int(match.group(4)),
        minute=int(match.group(5)), second=int(match.group(6)))

    time_delta = abs(datetime.datetime.now() - date_time_from_xml)
    # timestamp value should be near the current local time
    self.assertTrue(time_delta < datetime.timedelta(seconds=600),
                    'time_delta is %s' % time_delta)
    actual.unlink() 
Example 6
Project: archon   Author: economicnetwork   File: cryptofacil.py    MIT License 6 votes vote down vote up
def withdraw():
    # send xbt withdrawal request
    targetAddress = "xxxxxxxxxx"
    currency = "xbt"
    amount = 0.12345678
    result = cfclient.send_withdrawal(targetAddress, currency, amount)
    print("send_withdrawal:\n", result)

    # get xbt transfers
    lastTransferTime = datetime.datetime.strptime("2016-02-01", "%Y-%m-%d").isoformat() + ".000Z"
    result = cfclient.get_transfers(lastTransferTime=lastTransferTime)
    print("get_transfers:\n", result)

    # transfer
    fromAccount = "fi_ethusd"
    toAccount = "cash"
    unit = "eth"
    amount = 0.1
    result = cfclient.transfer(fromAccount, toAccount, unit, amount)
    print("transfer:\n", result) 
Example 7
Project: InplusTrader_Linux   Author: zhengwsh   File: mongo.py    MIT License 6 votes vote down vote up
def test(self):
		cx = self.Symbol_Db['equity'].find()
		symbolSet = set([d['code'] for d in cx])
		for code in symbolSet:
			start = self.Symbol_Db['equity'].find({"code" : code})[0]['timeToMarket']
			try:
				start = datetime.datetime.strptime(str(start), '%Y%m%d')
			except :
				print code
			
			start = start.strftime("%Y-%m-%d")
			print start
		return 


	#---------------------------------------------------------------------- 
Example 8
Project: flasky   Author: RoseOu   File: processors.py    MIT License 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 9
Project: bigquerylayers   Author: smandaric   File: datetime_helpers.py    GNU General Public License v3.0 5 votes vote down vote up
def from_iso8601_date(value):
    """Convert a ISO8601 date string to a date.

    Args:
        value (str): The ISO8601 date string.

    Returns:
        datetime.date: A date equivalent to the date string.
    """
    return datetime.datetime.strptime(value, "%Y-%m-%d").date() 
Example 10
Project: bigquerylayers   Author: smandaric   File: datetime_helpers.py    GNU General Public License v3.0 5 votes vote down vote up
def from_iso8601_time(value):
    """Convert a zoneless ISO8601 time string to a time.

    Args:
        value (str): The ISO8601 time string.

    Returns:
        datetime.time: A time equivalent to the time string.
    """
    return datetime.datetime.strptime(value, "%H:%M:%S").time() 
Example 11
Project: bigquerylayers   Author: smandaric   File: datetime_helpers.py    GNU General Public License v3.0 5 votes vote down vote up
def from_rfc3339(value):
    """Convert a microsecond-precision timestamp to datetime.

    Args:
        value (str): The RFC3339 string to convert.

    Returns:
        datetime.datetime: The datetime object equivalent to the timestamp in
            UTC.
    """
    return datetime.datetime.strptime(value, _RFC3339_MICROS).replace(tzinfo=pytz.utc) 
Example 12
Project: bigquerylayers   Author: smandaric   File: datetime_helpers.py    GNU General Public License v3.0 5 votes vote down vote up
def from_rfc3339_nanos(value):
    """Convert a nanosecond-precision timestamp to a native datetime.

    .. note::
        Python datetimes do not support nanosecond precision; this function
        therefore truncates such values to microseconds.

    Args:
        value (str): The RFC3339 string to convert.

    Returns:
        datetime.datetime: The datetime object equivalent to the timestamp in
            UTC.

    Raises:
        ValueError: If the timestamp does not match the RFC 3339
            regular expression.
    """
    with_nanos = _RFC3339_NANOS.match(value)

    if with_nanos is None:
        raise ValueError(
            "Timestamp: {!r}, does not match pattern: {!r}".format(
                value, _RFC3339_NANOS.pattern
            )
        )

    bare_seconds = datetime.datetime.strptime(
        with_nanos.group("no_fraction"), _RFC3339_NO_FRACTION
    )
    fraction = with_nanos.group("nanos")

    if fraction is None:
        micros = 0
    else:
        scale = 9 - len(fraction)
        nanos = int(fraction) * (10 ** scale)
        micros = nanos // 1000

    return bare_seconds.replace(microsecond=micros, tzinfo=pytz.utc) 
Example 13
Project: bigquerylayers   Author: smandaric   File: datetime_helpers.py    GNU General Public License v3.0 5 votes vote down vote up
def from_rfc3339(cls, stamp):
        """Parse RFC 3339-compliant timestamp, preserving nanoseconds.

        Args:
            stamp (str): RFC 3339 stamp, with up to nanosecond precision

        Returns:
            :class:`DatetimeWithNanoseconds`:
                an instance matching the timestamp string

        Raises:
            ValueError: if `stamp` does not match the expected format
        """
        with_nanos = _RFC3339_NANOS.match(stamp)
        if with_nanos is None:
            raise ValueError(
                "Timestamp: {}, does not match pattern: {}".format(
                    stamp, _RFC3339_NANOS.pattern
                )
            )
        bare = datetime.datetime.strptime(
            with_nanos.group("no_fraction"), _RFC3339_NO_FRACTION
        )
        fraction = with_nanos.group("nanos")
        if fraction is None:
            nanos = 0
        else:
            scale = 9 - len(fraction)
            nanos = int(fraction) * (10 ** scale)
        return cls(
            bare.year,
            bare.month,
            bare.day,
            bare.hour,
            bare.minute,
            bare.second,
            nanosecond=nanos,
            tzinfo=pytz.UTC,
        ) 
Example 14
Project: bigquerylayers   Author: smandaric   File: _helpers.py    GNU General Public License v3.0 5 votes vote down vote up
def _rfc3339_to_datetime(dt_str):
    """Convert a microsecond-precision timestamp to a native datetime.

    :type dt_str: str
    :param dt_str: The string to convert.

    :rtype: :class:`datetime.datetime`
    :returns: The datetime object created from the string.
    """
    return datetime.datetime.strptime(dt_str, _RFC3339_MICROS).replace(tzinfo=UTC) 
Example 15
Project: bigquerylayers   Author: smandaric   File: _helpers.py    GNU General Public License v3.0 5 votes vote down vote up
def _rfc3339_nanos_to_datetime(dt_str):
    """Convert a nanosecond-precision timestamp to a native datetime.

    .. note::

       Python datetimes do not support nanosecond precision;  this function
       therefore truncates such values to microseconds.

    :type dt_str: str
    :param dt_str: The string to convert.

    :rtype: :class:`datetime.datetime`
    :returns: The datetime object created from the string.
    :raises ValueError: If the timestamp does not match the RFC 3339
                        regular expression.
    """
    with_nanos = _RFC3339_NANOS.match(dt_str)
    if with_nanos is None:
        raise ValueError(
            "Timestamp: %r, does not match pattern: %r"
            % (dt_str, _RFC3339_NANOS.pattern)
        )
    bare_seconds = datetime.datetime.strptime(
        with_nanos.group("no_fraction"), _RFC3339_NO_FRACTION
    )
    fraction = with_nanos.group("nanos")
    if fraction is None:
        micros = 0
    else:
        scale = 9 - len(fraction)
        nanos = int(fraction) * (10 ** scale)
        micros = nanos // 1000
    return bare_seconds.replace(microsecond=micros, tzinfo=UTC) 
Example 16
Project: RedTubeAPI   Author: KGerring   File: utils.py    MIT License 5 votes vote down vote up
def get_datetime(s):
	try:
		return datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
	except AttributeError:
		return datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S') 
Example 17
Project: sound-of-sunshine   Author: waldoj   File: sunshine.py    MIT License 5 votes vote down vote up
def is_daylight():
    """Report whether the sun is up."""

    # If we've cached daylight data, and it's less than 3 days old, use it.
    if os.path.exists('.daylight'):
        file_status = os.stat('.daylight')
        if (int(time.time()) - file_status.st_mtime) < (60 * 60 * 72):
            schedule = json.loads(open('.daylight').read(10000))

    # If we don't have cached data, retrieve it.
    try:
        schedule
    except NameError:
        api_url = 'http://api.sunrise-sunset.org/json?lat=' \
            + CONFIG['location']['lat'] + '&lng=' + CONFIG['location']['lon']
        response = urllib.urlopen(api_url)
        schedule = json.loads(response.read())
        if schedule['status'] is 'OK':
            print 'Fatal error: sunrise/sunset times could not be determined'
            sys.exit(1)
        else:
            f = open('.daylight', 'w')
            f.write(json.dumps(schedule))
            f.close()
    else:
        pass

    from datetime import datetime, time
    now = datetime.now()
    print now.time()
    if datetime.strptime(schedule['results']['sunrise'].time(), '%I:%M:%S %p').time < now.time() \
        <= datetime.strptime(schedule['results']['sunset'], '%I:%M:%S %p').time:
        print "yup"
        sys.exit()
        return True
    else:
        print "nope"
        sys.exit()
        return False 
Example 18
Project: sjcx-payments   Author: StorjOld   File: farmersummary.py    MIT License 5 votes vote down vote up
def update_table(conn, cursor, collection): # pragma: no cover
    """Updates the summaries table if there is new data in collection."""
    cursor.execute('SELECT MAX(date) FROM summaries')
    date = cursor.fetchone()[0]
    max_date = dt.strptime(date,  '%Y-%M-%D %H:%M:%S')
    next_date = max_date + timedelta(days = 1)
    last_date = end_date(collection)
    day_count = (last_date - next_date).days
    for single_date in (next_date + timedelta(days=n) for n in range(day_count)):
        create_daily_summary(conn, cursor, collection, single_date)
        assign_points(conn, cursor, single_date)
        assign_percentages(conn, cursor, single_date) 
Example 19
Project: Flask_Blog   Author: sugarguo   File: processors.py    GNU General Public License v3.0 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 20
Project: neurips2019challenge   Author: tumeteor   File: loader.py    Apache License 2.0 5 votes vote down vote up
def extract_date(filename):
    match = re.search(r'\d{4}\d{2}\d{2}', filename)
    return datetime.strptime(match.group(), '%Y%m%d').date() 
Example 21
Project: neurips2019challenge   Author: tumeteor   File: loader.py    Apache License 2.0 5 votes vote down vote up
def return_date(file_name):
    """Auxilliary function which returns datetime object from Traffic4Cast filename.

        Args.:
            file_name (str): file name, e.g., '20180516_100m_bins.h5'

        Returns: date string, e.g., '2018-05-16'
    """

    match = re.search(r'\d{4}\d{2}\d{2}', file_name)
    date = datetime.datetime.strptime(match.group(), '%Y%m%d').date()
    return date 
Example 22
Project: wp1   Author: openzim   File: log.py    GNU General Public License v2.0 5 votes vote down vote up
def timestamp_dt(self):
    return datetime.strptime(self.quality_timestamp.decode('utf-8'),
                             '%Y%m%dT%H%M%S') 
Example 23
Project: 20180116-FullStack-Day   Author: PdxCodeGuild   File: l24_rain.py    GNU General Public License v3.0 5 votes vote down vote up
def extract_data_from_file(rain_file):
    # original data in 0.01 inches converted to inches
    list_rain_dicts = []
    with open(rain_file, 'r', newline='') as f:
        lines = f.read().split('\n')
        # headers = lines[9].split()
        lines = lines[12:-2]
        for row in lines:
            row = row.split()
            date = datetime.strptime((row[0]), '%d-%b-%Y')
            line_dict = {'date': date, 'year': date.year, 'month': date.month, 'total': float(row[1])*0.01}
            list_rain_dicts.append(line_dict)
    return list_rain_dicts 
Example 24
Project: jbox   Author: jpush   File: processors.py    MIT License 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 25
Project: rucio   Author: rucio   File: srmdumps.py    Apache License 2.0 5 votes vote down vote up
def get_newest(base_url, url_pattern, links):
    '''
    Returns a tuple with the newest url in the `links` list matching the
    pattern `url_pattern` and a datetime object representing the creation
    date of the url.

    The creation date is extracted from the url using datetime.strptime().
    '''
    logger = logging.getLogger('auditor.srmdumps')
    times = []

    pattern_components = url_pattern.split('/')

    date_pattern = '{0}/{1}'.format(base_url, pattern_components[0])
    if len(pattern_components) > 1:
        postfix = '/' + '/'.join(pattern_components[1:])
    else:
        postfix = ''

    for link in links:
        try:
            time = datetime.datetime.strptime(link, date_pattern)
        except ValueError:
            pass
        else:
            times.append((str(link) + postfix, time))

    if not times:
        msg = 'No links found matching the pattern {0} in {1}'.format(date_pattern, links)
        logger.error(msg)
        raise Exception(msg)

    return max(times, key=operator.itemgetter(1)) 
Example 26
Project: voicebot   Author: JustinaPetr   File: community_events.py    GNU General Public License v3.0 5 votes vote down vote up
def parse_community_date(date_string: Text) -> 'datetime':
    from datetime import datetime

    dates = date_string.split("-")

    try:
        return datetime.strptime(dates[-1].strip(), DATE_FORMAT)
    except Exception as e:
        logger.warning(e)
        return datetime.min 
Example 27
Project: Mahjong-Solitaire   Author: MEASHY   File: processors.py    MIT License 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 28
Project: Mahjong-Solitaire   Author: MEASHY   File: processors.py    MIT License 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 29
Project: oa_qian   Author: sunqb   File: processors.py    Apache License 2.0 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 30
Project: analyzePF   Author: analyzeDFIR   File: processors.py    MIT License 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 31
Project: KALanguageReport   Author: alani1   File: loadPKLData.py    MIT License 5 votes vote down vote up
def createStatObject(self, date, lang, type, leftSecs, percent, leftString):
        if ( date > '2014-12-08' ):
            #print('Percent'+percent)
            dt = created  = datetime.strptime(date, "%Y-%m-%d")
            try:   
                ls = LanguageStatistic.objects.get(date=dt,lang=lang.code,target=trgt[lang.target],type=type)            
                print("LS Object found, updating")
            except LanguageStatistic.DoesNotExist:
                ls = LanguageStatistic(date=dt,lang=lang.code,target=trgt[lang.target],type=type)
                ls.total            = 0
                ls.count            = 0
            
            if (percent < 1 ):
                ls.totalSecs        = leftSecs / (1-percent)
                
            ls.countSecs        = ls.totalSecs - leftSecs
            
            if (percent < 1 ):
                ls.totalStrings     = leftString / (1-percent)
            ls.leftStrings      = leftString
            
            ls.calculateSpeed()
            
            
            
            ls.save() 
Example 32
Project: xuemc   Author: skycucumber   File: processors.py    GNU General Public License v2.0 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 33
Project: chihu   Author: yelongyu   File: processors.py    GNU General Public License v3.0 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 34
Project: docker-teleservices   Author: IMIO   File: town.py    GNU General Public License v3.0 5 votes vote down vote up
def dt_to_date(self, dt, format_date='%Y-%m-%d', format_time='%H:%M:%S'):
        return datetime.strptime(dt, '{0} {1}'.format(format_date, format_time)).date() 
Example 35
Project: SolrClient   Author: moonlitesolutions   File: reindexer.py    Apache License 2.0 5 votes vote down vote up
def _get_date_facet_counts(self, timespan, date_field, start_date=None, end_date=None):
        '''
        Returns Range Facet counts based on
        '''
        if 'DAY' not in timespan:
            raise ValueError("At this time, only DAY date range increment is supported. Aborting..... ")

        #Need to do this a bit better later. Don't like the string and date concatenations.
        if not start_date:
            start_date = self._get_edge_date(date_field, 'asc')
            start_date = datetime.strptime(start_date,'%Y-%m-%dT%H:%M:%S.%fZ').date().isoformat()+'T00:00:00.000Z'
        else:
            start_date = start_date+'T00:00:00.000Z'

        if not end_date:
            end_date = self._get_edge_date(date_field, 'desc')
            end_date = datetime.strptime(end_date,'%Y-%m-%dT%H:%M:%S.%fZ').date()
            end_date += timedelta(days=1)
            end_date = end_date.isoformat()+'T00:00:00.000Z'
        else:
            end_date = end_date+'T00:00:00.000Z'


        self.log.info("Processing Items from {} to {}".format(start_date, end_date))

        #Get facet counts for source and destination collections
        source_facet = self._source.query(self._source_coll,
            self._get_date_range_query(timespan=timespan, start_date=start_date, end_date=end_date)
            ).get_facets_ranges()[date_field]
        dest_facet = self._dest.query(
            self._dest_coll, self._get_date_range_query(
                    timespan=timespan, start_date=start_date, end_date=end_date
                    )).get_facets_ranges()[date_field]
        return source_facet, dest_facet 
Example 36
Project: Emotion-Evaluator   Author: Attriumph   File: datetime_helpers.py    MIT License 5 votes vote down vote up
def from_iso8601_date(value):
    """Convert a ISO8601 date string to a date.

    Args:
        value (str): The ISO8601 date string.

    Returns:
        datetime.date: A date equivalent to the date string.
    """
    return datetime.datetime.strptime(value, "%Y-%m-%d").date() 
Example 37
Project: Emotion-Evaluator   Author: Attriumph   File: datetime_helpers.py    MIT License 5 votes vote down vote up
def from_iso8601_time(value):
    """Convert a zoneless ISO8601 time string to a time.

    Args:
        value (str): The ISO8601 time string.

    Returns:
        datetime.time: A time equivalent to the time string.
    """
    return datetime.datetime.strptime(value, "%H:%M:%S").time() 
Example 38
Project: Emotion-Evaluator   Author: Attriumph   File: datetime_helpers.py    MIT License 5 votes vote down vote up
def from_rfc3339(value):
    """Convert a microsecond-precision timestamp to datetime.

    Args:
        value (str): The RFC3339 string to convert.

    Returns:
        datetime.datetime: The datetime object equivalent to the timestamp in
            UTC.
    """
    return datetime.datetime.strptime(value, _RFC3339_MICROS).replace(tzinfo=pytz.utc) 
Example 39
Project: Emotion-Evaluator   Author: Attriumph   File: datetime_helpers.py    MIT License 5 votes vote down vote up
def from_rfc3339_nanos(value):
    """Convert a nanosecond-precision timestamp to a native datetime.

    .. note::
        Python datetimes do not support nanosecond precision; this function
        therefore truncates such values to microseconds.

    Args:
        value (str): The RFC3339 string to convert.

    Returns:
        datetime.datetime: The datetime object equivalent to the timestamp in
            UTC.

    Raises:
        ValueError: If the timestamp does not match the RFC 3339
            regular expression.
    """
    with_nanos = _RFC3339_NANOS.match(value)

    if with_nanos is None:
        raise ValueError(
            "Timestamp: {!r}, does not match pattern: {!r}".format(
                value, _RFC3339_NANOS.pattern
            )
        )

    bare_seconds = datetime.datetime.strptime(
        with_nanos.group("no_fraction"), _RFC3339_NO_FRACTION
    )
    fraction = with_nanos.group("nanos")

    if fraction is None:
        micros = 0
    else:
        scale = 9 - len(fraction)
        nanos = int(fraction) * (10 ** scale)
        micros = nanos // 1000

    return bare_seconds.replace(microsecond=micros, tzinfo=pytz.utc) 
Example 40
Project: Emotion-Evaluator   Author: Attriumph   File: datetime_helpers.py    MIT License 5 votes vote down vote up
def from_rfc3339(cls, stamp):
        """Parse RFC 3339-compliant timestamp, preserving nanoseconds.

        Args:
            stamp (str): RFC 3339 stamp, with up to nanosecond precision

        Returns:
            :class:`DatetimeWithNanoseconds`:
                an instance matching the timestamp string

        Raises:
            ValueError: if `stamp` does not match the expected format
        """
        with_nanos = _RFC3339_NANOS.match(stamp)
        if with_nanos is None:
            raise ValueError(
                "Timestamp: {}, does not match pattern: {}".format(
                    stamp, _RFC3339_NANOS.pattern
                )
            )
        bare = datetime.datetime.strptime(
            with_nanos.group("no_fraction"), _RFC3339_NO_FRACTION
        )
        fraction = with_nanos.group("nanos")
        if fraction is None:
            nanos = 0
        else:
            scale = 9 - len(fraction)
            nanos = int(fraction) * (10 ** scale)
        return cls(
            bare.year,
            bare.month,
            bare.day,
            bare.hour,
            bare.minute,
            bare.second,
            nanosecond=nanos,
            tzinfo=pytz.UTC,
        ) 
Example 41
Project: url_shortener   Author: martydill   File: processors.py    MIT License 5 votes vote down vote up
def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    has_named_groups = bool(regexp.groupindex)

    def process(value):
        if value is None:
            return None
        else:
            try:
                m = rmatch(value)
            except TypeError:
                raise ValueError("Couldn't parse %s string '%r' "
                                 "- value is not a string." %
                                 (type_.__name__, value))
            if m is None:
                raise ValueError("Couldn't parse %s string: "
                                 "'%s'" % (type_.__name__, value))
            if has_named_groups:
                groups = m.groupdict(0)
                return type_(**dict(list(zip(
                    iter(groups.keys()),
                    list(map(int, iter(groups.values())))
                ))))
            else:
                return type_(*list(map(int, m.groups(0))))
    return process 
Example 42
Project: magdalena   Author: KaiRo-at   File: datautils.py    Mozilla Public License 2.0 5 votes vote down vote up
def verifyForcedDates(fdates):
    from datetime import datetime
    import re
    force_dates = [];
    for fdate in fdates:
        if (re.match(r"\d+-\d+-\d+", fdate) and
            datetime.strptime(fdate, '%Y-%m-%d').strftime('%Y-%m-%d') == fdate):
            force_dates.append(fdate);
    return force_dates 
Example 43
Project: magdalena   Author: KaiRo-at   File: datautils.py    Mozilla Public License 2.0 5 votes vote down vote up
def dayStringAdd(anaday, days = 0, weeks = 0):
    from datetime import datetime, timedelta
    return (datetime.strptime(anaday, '%Y-%m-%d') + timedelta(days=days, weeks=weeks)).strftime('%Y-%m-%d') 
Example 44
Project: magdalena   Author: KaiRo-at   File: datautils.py    Mozilla Public License 2.0 5 votes vote down vote up
def dayStringBeforeDelta(anaday, tdelta):
    from datetime import datetime
    return (datetime.strptime(anaday, '%Y-%m-%d') - tdelta).strftime('%Y-%m-%d') 
Example 45
Project: geofire-python   Author: ininex   File: _helpers.py    MIT License 5 votes vote down vote up
def _rfc3339_to_datetime(dt_str):
    """Convert a microsecond-precision timetamp to a native datetime.

    :type dt_str: str
    :param dt_str: The string to convert.

    :rtype: :class:`datetime.datetime`
    :returns: The datetime object created from the string.
    """
    return datetime.datetime.strptime(
        dt_str, _RFC3339_MICROS).replace(tzinfo=UTC) 
Example 46
Project: geofire-python   Author: ininex   File: _helpers.py    MIT License 5 votes vote down vote up
def _rfc3339_nanos_to_datetime(dt_str):
    """Convert a nanosecond-precision timestamp to a native datetime.

    .. note::

       Python datetimes do not support nanosecond precision;  this function
       therefore truncates such values to microseconds.

    :type dt_str: str
    :param dt_str: The string to convert.

    :rtype: :class:`datetime.datetime`
    :returns: The datetime object created from the string.
    """
    with_nanos = _RFC3339_NANOS.match(dt_str)
    if with_nanos is None:
        raise ValueError(
            'Timestamp: %r, does not match pattern: %r' % (
                dt_str, _RFC3339_NANOS.pattern))
    bare_seconds = datetime.datetime.strptime(
        with_nanos.group('no_fraction'), _RFC3339_NO_FRACTION)
    fraction = with_nanos.group('nanos')
    scale = 9 - len(fraction)
    nanos = int(fraction) * (10 ** scale)
    micros = nanos // 1000
    return bare_seconds.replace(microsecond=micros, tzinfo=UTC) 
Example 47
Project: qpp-claims-to-quality-public   Author: CMSgov   File: calculate_measure_from_csv.py    Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def _valid_date(s):
    """Validate date format."""
    try:
        return datetime.strptime(s, '%Y-%m-%d')
    except ValueError:
        msg = "Not a valid date: '{0}'.".format(s)
        raise argparse.ArgumentTypeError(msg) 
Example 48
Project: smc-python   Author: gabstopper   File: util.py    Apache License 2.0 5 votes vote down vote up
def datetime_to_ms(dt):
    """
    Convert an unaware datetime object to milliseconds. This will
    be a UTC time. The SMC stores all times in UTC and will do the
    time conversions based on the local timezone.
    Example of converting a datetime to milliseconds::
    
        utc_time = datetime.strptime("2018-06-04T00:00:00", "%Y-%m-%dT%H:%M:%S")
        datetime_to_ms(utc_time)
    
    :param dt datetime: pass in python datetime object.
    :return: value representing the datetime in milliseconds
    :rtype: int
    """
    return int(time.mktime(dt.timetuple()) * 1000) 
Example 49
Project: invader   Author: entynetproject   File: helpers.py    MIT License 5 votes vote down vote up
def lastseen(stamp, delay, jitter):
    """
    Colorize the Last Seen field based on measured delays
    """
    try:
        delta = datetime.now() - datetime.strptime(stamp, "%Y-%m-%d %H:%M:%S")
        if delta.seconds > delay * (jitter + 1) * 5:
            return color(stamp, "red")
        elif delta.seconds > delay * (jitter + 1):
            return color(stamp, "yellow")
        else:
            return color(stamp, "green")
    except Exception:
        return stamp 
Example 50
Project: Predictive-Maintenance   Author: limingwu8   File: Sensor.py    MIT License 5 votes vote down vote up
def parser(self, x):
        return datetime.strptime('190' + x, '%Y-%m')

    # convert time series into supervised learning problem