<commit_before># -*- coding=utf -*-
"""Date and time utilities."""

import re
import pytz
from dateutil.relativedelta import relativedelta
from dateutil.relativedelta import MO, TU, WE, TH, FR, SA, SU
from datetime import datetime, timedelta
from time import strftime, gmtime

from .model import Hierarchy
from .errors import *


__all__ = (
    "Calendar",
    "calendar_hierarchy_units"
)


_CALENDAR_UNITS = ["year", "quarter", "month", "day", "hour", "minute",
                    "weekday"]


UNIT_YEAR = 8
UNIT_QUARTER = 7
UNIT_MONTH = 6
UNIT_WEEK = 5
UNIT_DAY = 4
UNIT_HOUR = 3
UNIT_MINUTE = 2
UNIT_SECOND = 1


_UNIT_ORDER = {
    "year": UNIT_YEAR,
    "quarter": UNIT_QUARTER,
    "month": UNIT_MONTH,
    "week": UNIT_WEEK,
    "day": UNIT_DAY,
    "hour": UNIT_HOUR,
    "minute": UNIT_MINUTE,
    "second": UNIT_SECOND
}

_DATEUTIL_WEEKDAYS = { 0: MO, 1: TU, 2: WE, 3: TH, 4: FR, 5: SA, 6: SU }

_UTC = pytz.timezone('UTC')


RELATIVE_FINE_TIME_RX = re.compile(r"(?P<offset>\d+)?"
                                    "(?P<unit>\w+)"
                                    "(?P<direction>(ago|forward))")


RELATIVE_TRUNCATED_TIME_RX = re.compile(r"(?P<direction>(last|next))"
                                         "(?P<offset>\d+)?"
                                         "(?P<unit>\w+)")

month_to_quarter = lambda month: ((month - 1) / 3) + 1


def calendar_hierarchy_units(hierarchy):
    """Return time units for levels in the hierarchy. The hierarchy is
    expected to be a date/time hierarchy and every level should have a `role`
    property specified. If the role is not specified, then the role is
    determined from the level name.

    Roles/units: `year`, `quarter`, `month`, `day`, `hour`, `minute`,
    `weekday`

    If unknown role is encountered an exception is raised."""

    units = []

    for level in hierarchy.levels:
        role = level.role or level.name

        if role in _CALENDAR_UNITS:
            units.append(role)
        else:
            raise ArgumentError("Unknown time role '%s' for level '%s'"
                                % (role, str(level)))

    return units


def local_timezone_name():
    """Return system's local timezone"""
    return strftime("%Z", gmtime())


def add_time_units(time, unit, amount):
    """Subtract `amount` number of `unit`s from datetime object `time`."""

    args = {}
    if unit == 'hour':
        args['hours'] = amount
    elif unit == 'day':
        args['days'] = amount
    elif unit == 'week':
        args['days'] = amount * 7
    elif unit == 'month':
        args['months'] = amount
    elif unit == 'quarter':
        args['months'] = amount * 3
    elif unit == 'year':
        args['years'] = amount
    else:
        raise ArgumentError("Unknown unit %s for subtraction.")

    return time + relativedelta(**args)


class Calendar(object):
    def __init__(self, first_weekday=0, timezone=None):
        """Creates a Calendar object for providing date/time paths and for
        relative date/time manipulation.

        Values for `first_weekday` are 0 for Monday, 6 for Sunday. Default is
        0."""

        self.first_weekday = first_weekday

        if timezone:
            self.timezone_name = timezone
        else:
            self.timezone_name = local_timezone_name()
        self.timezone = pytz.timezone(self.timezone_name)
<commit_msg>added Calendar to the workspace + related config options: timezone and first_weekday<commit_after># -*- coding=utf -*-
"""Date and time utilities."""

import re
import pytz
from dateutil.relativedelta import relativedelta
from dateutil.relativedelta import MO, TU, WE, TH, FR, SA, SU
from datetime import datetime, timedelta
from time import strftime, gmtime

from .model import Hierarchy
from .errors import *


__all__ = (
    "Calendar",
    "calendar_hierarchy_units"
)


_CALENDAR_UNITS = ["year", "quarter", "month", "day", "hour", "minute",
                    "weekday"]


UNIT_YEAR = 8
UNIT_QUARTER = 7
UNIT_MONTH = 6
UNIT_WEEK = 5
UNIT_DAY = 4
UNIT_HOUR = 3
UNIT_MINUTE = 2
UNIT_SECOND = 1


_UNIT_ORDER = {
    "year": UNIT_YEAR,
    "quarter": UNIT_QUARTER,
    "month": UNIT_MONTH,
    "week": UNIT_WEEK,
    "day": UNIT_DAY,
    "hour": UNIT_HOUR,
    "minute": UNIT_MINUTE,
    "second": UNIT_SECOND
}

_DATEUTIL_WEEKDAYS = { 0: MO, 1: TU, 2: WE, 3: TH, 4: FR, 5: SA, 6: SU }

_WEEKDAY_NUMBERS = {
    "monday": 0,
    "tuesday": 1,
    "wednesday": 2,
    "thursday": 3,
    "friday": 4,
    "saturday": 5,
    "sunday": 6
}

_UTC = pytz.timezone('UTC')


RELATIVE_FINE_TIME_RX = re.compile(r"(?P<offset>\d+)?"
                                    "(?P<unit>\w+)"
                                    "(?P<direction>(ago|forward))")


RELATIVE_TRUNCATED_TIME_RX = re.compile(r"(?P<direction>(last|next))"
                                         "(?P<offset>\d+)?"
                                         "(?P<unit>\w+)")

month_to_quarter = lambda month: ((month - 1) / 3) + 1


def calendar_hierarchy_units(hierarchy):
    """Return time units for levels in the hierarchy. The hierarchy is
    expected to be a date/time hierarchy and every level should have a `role`
    property specified. If the role is not specified, then the role is
    determined from the level name.

    Roles/units: `year`, `quarter`, `month`, `day`, `hour`, `minute`,
    `weekday`

    If unknown role is encountered an exception is raised."""

    units = []

    for level in hierarchy.levels:
        role = level.role or level.name

        if role in _CALENDAR_UNITS:
            units.append(role)
        else:
            raise ArgumentError("Unknown time role '%s' for level '%s'"
                                % (role, str(level)))

    return units


def local_timezone_name():
    """Return system's local timezone"""
    return strftime("%Z", gmtime())


def add_time_units(time, unit, amount):
    """Subtract `amount` number of `unit`s from datetime object `time`."""

    args = {}
    if unit == 'hour':
        args['hours'] = amount
    elif unit == 'day':
        args['days'] = amount
    elif unit == 'week':
        args['days'] = amount * 7
    elif unit == 'month':
        args['months'] = amount
    elif unit == 'quarter':
        args['months'] = amount * 3
    elif unit == 'year':
        args['years'] = amount
    else:
        raise ArgumentError("Unknown unit %s for subtraction.")

    return time + relativedelta(**args)


class Calendar(object):
    def __init__(self, first_weekday=0, timezone=None):
        """Creates a Calendar object for providing date/time paths and for
        relative date/time manipulation.

        Values for `first_weekday` are 0 for Monday, 6 for Sunday. Default is
        0."""

        if isinstance(first_weekday, basestring):
            try:
                self.first_weekday = _WEEKDAY_NUMBERS[first_weekday.lower()]
            except KeyError:
                raise ConfigurationError("Unknown weekday name %s" %
                                         first_weekday)
        else:
            self.first_weekday = int(first_weekday)

        if timezone:
            self.timezone_name = timezone
        else:
            self.timezone_name = local_timezone_name()
        self.timezone = pytz.timezone(self.timezone_name)

{'fraction_non_alphanumeric': 0.12362334801762115, 'fraction_numerical': 0.0075715859030837, 'mean_word_length': 2.3964469378214117, 'pattern_counts': {'":': 23, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3576807', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2248, 'n_words': 664}