<commit_before># Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime

def datetime_to_avro_timestamp(dt, micros=True):
    """
    This is a convienence function for converting datetime objects to 
    timestamps in either milliseconds or microseconds since the Unix
    Epoch.
    Args:
        dt: (datetime.datetime) to be converted.
        micros: (bool) should we use microsecond precision. Default behavior
            is millisecond precision. This should be dictated by the avsc file.
    """
    _UNIX_EPOCH = datetime.datetime(1970, 1, 1)
    _MILLISECONDS_PER_SECOND = 10 ** 3
    _MICROSECONDS_PER_SECOND = 10 ** 6

    if isinstance(dt, str):
        try:
            dt = datetime.datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S')
        except:
            dt = datetime.datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S.%f')
            
    seconds_since_epoch = (dt - _UNIX_EPOCH).total_seconds()

    multiplier = _MICROSECONDS_PER_SECOND if micros else _MILLISECONDS_PER_SECOND

    return long(seconds_since_epoch * multiplier)

def date_to_avro_date(dt):
    """
    This is a convienence function for converting datetime objects to 
    timestamps in either milliseconds or microseconds since the Unix
    Epoch.
    Args:
        dt: (datetime.datetime) to be converted.
        micros: (bool) should we use microsecond precision. Default behavior
            is millisecond precision. This should be dictated by the avsc file.
    """
    _UNIX_EPOCH = datetime.datetime(1970, 1, 1)

    if isinstance(dt, str):
       dt = datetime.datetime.strptime(dt, '%Y-%m-%d')
            
    days_since_epoch = (dt - _UNIX_EPOCH).days

    return int(days_since_epoch)

def time_to_avro_time(t, micros=True):
    """
    This is a convienence function for converting datetime objects to 
    timestamps in either milliseconds or microseconds since the Unix
    Epoch.
    Args:
        dt: (datetime.datetime) to be converted.
        micros: (bool) should we use microsecond precision. Default behavior
            is millisecond precision. This should be dictated by the avsc file.
    """
    _MIDNIGHT = datetime.time(0, 0, 0) 
    _MILLISECONDS_PER_SECOND = 10 ** 3
    _MICROSECONDS_PER_SECOND = 10 ** 6
    if isinstance(t, str):
        try:
            t = datetime.datetime.strptime(t, '%H:%M:%S').time()
        except:
            t = datetime.datetime.strptime(t, '%H:%M:%S.%f').time()

    _TODAY = datetime.date.today()        
<commit_msg>fixing str to unicode in AvroUtils.py<commit_after># Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime

def datetime_to_avro_timestamp(dt, micros=True):
    """
    This is a convienence function for converting datetime objects to 
    timestamps in either milliseconds or microseconds since the Unix
    Epoch.
    Args:
        dt: (datetime.datetime) to be converted.
        micros: (bool) should we use microsecond precision. Default behavior
            is millisecond precision. This should be dictated by the avsc file.
    """
    _UNIX_EPOCH = datetime.datetime(1970, 1, 1)
    _MILLISECONDS_PER_SECOND = 10 ** 3
    _MICROSECONDS_PER_SECOND = 10 ** 6

    if isinstance(dt, unicode):
        try:
            dt = datetime.datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S')
        except:
            dt = datetime.datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S.%f')
            
    seconds_since_epoch = (dt - _UNIX_EPOCH).total_seconds()

    multiplier = _MICROSECONDS_PER_SECOND if micros else _MILLISECONDS_PER_SECOND

    return long(seconds_since_epoch * multiplier)

def date_to_avro_date(dt):
    """
    This is a convienence function for converting datetime objects to 
    timestamps in either milliseconds or microseconds since the Unix
    Epoch.
    Args:
        dt: (datetime.datetime) to be converted.
        micros: (bool) should we use microsecond precision. Default behavior
            is millisecond precision. This should be dictated by the avsc file.
    """
    _UNIX_EPOCH = datetime.datetime(1970, 1, 1)

    if isinstance(dt, unicode):
       dt = datetime.datetime.strptime(dt, '%Y-%m-%d')
            
    days_since_epoch = (dt - _UNIX_EPOCH).days

    return int(days_since_epoch)

def time_to_avro_time(t, micros=True):
    """
    This is a convienence function for converting datetime objects to 
    timestamps in either milliseconds or microseconds since the Unix
    Epoch.
    Args:
        dt: (datetime.datetime) to be converted.
        micros: (bool) should we use microsecond precision. Default behavior
            is millisecond precision. This should be dictated by the avsc file.
    """
    _MIDNIGHT = datetime.time(0, 0, 0) 
    _MILLISECONDS_PER_SECOND = 10 ** 3
    _MICROSECONDS_PER_SECOND = 10 ** 6
    if isinstance(t, unicode):
        try:
            t = datetime.datetime.strptime(t, '%H:%M:%S').time()
        except:
            t = datetime.datetime.strptime(t, '%H:%M:%S.%f').time()

    _TODAY = datetime.date.today()        

{'fraction_non_alphanumeric': 0.0880597014925373, 'fraction_numerical': 0.011608623548922056, 'mean_word_length': 2.934116112198304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5566953', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1804, 'n_words': 609}