# Path: pq/utils.py
# def get_restricted_datetime(at, between='', weekdays=None):
#     """
#     Returns a new datetime that always falls in
#     the timerange ``between`` an iso 8601 string or variant,
#     and days where days is a list or tuple of relativedelta weekday
#     objects.
# 
#     If the time part of the datetime falls before the
#     timerange the datetime will be moved forward to the
#     start of the range. In the event the time is after the
#     range the datetime will be moved forward to the next
#     day.
# 
#     >>> dt = datetime(2013,1,1,6,30)
#     >>> get_restricted_datetime(dt, '7-12')
#     datetime(2013,1,1,7)
#     >>> get_restricted_datetime(dt, '1:00/6:00')
#     datetime(2013,1,2,1)
#     >>> get_restricted_datetime(dt, '1:00-6:00')
#     datetime(2013,1,2,1)
#     >>> get_restricted_datetime(dt, '1:00:10/6:00:59')
#     datetime(2013,1,2,1)
#     """
#     if between:
#         pattern = re.compile(
#             r"(\d{1,2})[:.]?(\d{0,2})[:.]?\d{0,2}\s*[/-]+" +
#             r"\s*(\d{1,2})[:.]?(\d{0,2})[:.]?\d{0,2}"
#             )
#         r = pattern.search(between)
#         if not r:
#             raise InvalidBetween("Invalid between range %s" % between)
# 
#         shour, smin, ehour, emin = r.groups()
#         shour = int(shour)
#         smin = int(smin) if smin else 0
#         ehour = int(ehour)
#         emin = int(emin) if emin else 0
#         if ehour < shour:
#             raise InvalidBetween("Between end cannot be before start")
#         elif ehour == 24:
#             ehour = 23
#             emin = 59
#         st = time(shour, smin, tzinfo=at.tzinfo)
#         et = time(ehour, emin, tzinfo=at.tzinfo)
#         date = at.date()
#         compare_st = datetime.combine(date, st)
#         compare_et = datetime.combine(date, et)
#         if at < compare_st:
#             at = compare_st
#         elif at > compare_et:
#             at = compare_st + timedelta(days=1)
#     if weekdays:
#         weekdays = list(weekdays)
#         for i, value in enumerate(weekdays):
#             if isinstance(value, relativedelta.weekday):
#                 weekdays[i] = value.weekday
#             elif isinstance(value, integer_types) and value >=0 and value <=6:
#                 continue
#             else:
#                 msg = "Invalid weekday %s. Weekdays must be a" % str(value)
#                 msg = ' '.join([msg, "list or tuple of relativedelta.weekday",
#                                "instances or integers between 0 and 6"])
#                 raise InvalidWeekdays(msg)
#         weekdays = sorted(weekdays)
#         nextdays = relativedelta.weekdays[at.weekday():]
#         priordays = relativedelta.weekdays[:at.weekday()]
#         for i, day in enumerate(nextdays + priordays):
#             if day.weekday in weekdays:
#                 at += timedelta(days=i)
#                 break
#     return at
# 
# Path: pq/exceptions.py
# class InvalidWeekdays(Exception):
#     pass


import unittest
from datetime import datetime
from django.utils.timezone import utc
from nose2.tools import params
from dateutil.relativedelta import weekdays
from pq.utils import get_restricted_datetime
from pq.exceptions import InvalidWeekdays
        ('7:00-8:00', datetime(2013,1,1,7,0, tzinfo=utc)),
        ('7:00/8:00', datetime(2013,1,1,7,0, tzinfo=utc)),
        ('7:00:01-8:00:59', datetime(2013,1,1,7,0, tzinfo=utc)),


    )
    def test_get_restricted_datetime(self, between, result):
        dt = get_restricted_datetime(self.dt, between)
        self.assertEqual(dt, result)

    def test_get_naive_datetime(self):
        dt = datetime(2013,1,1,6)
        rdt = get_restricted_datetime(dt, '0-24')
        self.assertEqual(dt, rdt)


class TestGetRestrictedDatetimeWeekdays(unittest.TestCase):
    def setUp(self):
        self.dt = datetime(2013, 1, 1)

    @params(
        ((1,), datetime(2013,1,1)),
        ((0,), datetime(2013,1,7)),
        ((weekdays[6],weekdays[5]), datetime(2013,1,5)),
    )
    def test_get_restricted_weekdays(self, weekdays, result):
        dt = get_restricted_datetime(self.dt, weekdays=weekdays)
        self.assertEqual(dt, result)

    def test_invalid_weekdays(self):

# Path: pq/utils.py
# def get_restricted_datetime(at, between='', weekdays=None):
#     """
#     Returns a new datetime that always falls in
#     the timerange ``between`` an iso 8601 string or variant,
#     and days where days is a list or tuple of relativedelta weekday
#     objects.
# 
#     If the time part of the datetime falls before the
#     timerange the datetime will be moved forward to the
#     start of the range. In the event the time is after the
#     range the datetime will be moved forward to the next
#     day.
# 
#     >>> dt = datetime(2013,1,1,6,30)
#     >>> get_restricted_datetime(dt, '7-12')
#     datetime(2013,1,1,7)
#     >>> get_restricted_datetime(dt, '1:00/6:00')
#     datetime(2013,1,2,1)
#     >>> get_restricted_datetime(dt, '1:00-6:00')
#     datetime(2013,1,2,1)
#     >>> get_restricted_datetime(dt, '1:00:10/6:00:59')
#     datetime(2013,1,2,1)
#     """
#     if between:
#         pattern = re.compile(
#             r"(\d{1,2})[:.]?(\d{0,2})[:.]?\d{0,2}\s*[/-]+" +
#             r"\s*(\d{1,2})[:.]?(\d{0,2})[:.]?\d{0,2}"
#             )
#         r = pattern.search(between)
#         if not r:
#             raise InvalidBetween("Invalid between range %s" % between)
# 
#         shour, smin, ehour, emin = r.groups()
#         shour = int(shour)
#         smin = int(smin) if smin else 0
#         ehour = int(ehour)
#         emin = int(emin) if emin else 0
#         if ehour < shour:
#             raise InvalidBetween("Between end cannot be before start")
#         elif ehour == 24:
#             ehour = 23
#             emin = 59
#         st = time(shour, smin, tzinfo=at.tzinfo)
#         et = time(ehour, emin, tzinfo=at.tzinfo)
#         date = at.date()
#         compare_st = datetime.combine(date, st)
#         compare_et = datetime.combine(date, et)
#         if at < compare_st:
#             at = compare_st
#         elif at > compare_et:
#             at = compare_st + timedelta(days=1)
#     if weekdays:
#         weekdays = list(weekdays)
#         for i, value in enumerate(weekdays):
#             if isinstance(value, relativedelta.weekday):
#                 weekdays[i] = value.weekday
#             elif isinstance(value, integer_types) and value >=0 and value <=6:
#                 continue
#             else:
#                 msg = "Invalid weekday %s. Weekdays must be a" % str(value)
#                 msg = ' '.join([msg, "list or tuple of relativedelta.weekday",
#                                "instances or integers between 0 and 6"])
#                 raise InvalidWeekdays(msg)
#         weekdays = sorted(weekdays)
#         nextdays = relativedelta.weekdays[at.weekday():]
#         priordays = relativedelta.weekdays[:at.weekday()]
#         for i, day in enumerate(nextdays + priordays):
#             if day.weekday in weekdays:
#                 at += timedelta(days=i)
#                 break
#     return at
# 
# Path: pq/exceptions.py
# class InvalidWeekdays(Exception):
#     pass


# Path: test_pq/test_utils.py
import unittest
from datetime import datetime
from django.utils.timezone import utc
from nose2.tools import params
from dateutil.relativedelta import weekdays
from pq.utils import get_restricted_datetime
from pq.exceptions import InvalidWeekdays

        ('7:00-8:00', datetime(2013,1,1,7,0, tzinfo=utc)),
        ('7:00/8:00', datetime(2013,1,1,7,0, tzinfo=utc)),
        ('7:00:01-8:00:59', datetime(2013,1,1,7,0, tzinfo=utc)),


    )
    def test_get_restricted_datetime(self, between, result):
        dt = get_restricted_datetime(self.dt, between)
        self.assertEqual(dt, result)

    def test_get_naive_datetime(self):
        dt = datetime(2013,1,1,6)
        rdt = get_restricted_datetime(dt, '0-24')
        self.assertEqual(dt, rdt)


class TestGetRestrictedDatetimeWeekdays(unittest.TestCase):
    def setUp(self):
        self.dt = datetime(2013, 1, 1)

    @params(
        ((1,), datetime(2013,1,1)),
        ((0,), datetime(2013,1,7)),
        ((weekdays[6],weekdays[5]), datetime(2013,1,5)),
    )
    def test_get_restricted_weekdays(self, weekdays, result):
        dt = get_restricted_datetime(self.dt, weekdays=weekdays)
        self.assertEqual(dt, result)

    def test_invalid_weekdays(self):

        with self.assertRaises(InvalidWeekdays):