# Path: performanceplatform/utils/datetimeutil.py
# def period_range(start, end, frequency):
#     start_date = to_date(start)
#     end_date = to_date(end)
#     if start_date > end_date:
#         raise ValueError("Bad dates: !(start_date={0} <= end_date={1})"
#                          .format(start_date, end_date))
#     if frequency == 'daily':
#         start_date = start_date or a_day_ago()
#         end_date = end_date or a_day_ago()
#         period = timedelta(days=1)
#         return generate_date_range(start_date, end_date, period)
#     elif frequency == 'weekly':
#         start_date = start_date or a_week_ago()
#         start_date = start_of_week(start_date)
#         end_date = end_date or a_week_ago()
#         period = timedelta(days=7)
#         return generate_date_range(start_date, end_date, period)
#     elif frequency == 'monthly':
#         start_date = start_date or a_month_ago()
#         start_date = start_of_month(start_date)
#         end_date = end_date or a_month_ago()
#         period = relativedelta(months=+1)
#         return generate_date_range(start_date, end_date, period)
#     else:
#         raise ValueError('Bad value of frequency, should be daily, weekly '
#                          'or monthly')
# 
# def to_date(a_datetime):
#     if a_datetime is None:
#         return None
#     elif isinstance(a_datetime, datetime):
#         return a_datetime.date()
#     elif isinstance(a_datetime, date):
#         return a_datetime
#     else:
#         raise ValueError("{0!r} ({1}) isn't a date or datetime"
#                          .format(a_datetime, type(a_datetime)))
# 
# def to_datetime(a_date):
#     if type(a_date) == datetime:
#         return a_date.replace(tzinfo=pytz.UTC)
#     return datetime.combine(a_date, time(0)).replace(tzinfo=pytz.UTC)


from datetime import date, datetime
from hamcrest import assert_that, only_contains, is_, equal_to
from nose.tools import raises
from freezegun import freeze_time
from performanceplatform.utils.datetimeutil import(
    period_range,
    to_date,
    to_datetime)
import pytz


def test_to_date_passes_none_through():
    assert_that(to_date(None), is_(None))


def test_to_date_converts_datetime_to_date():
    assert_that(
        to_date(datetime(2012, 12, 12)),
        equal_to(date(2012, 12, 12)))


def test_to_date_passes_date_through():
    assert_that(
        to_date(date(2012, 12, 12)),
        equal_to(date(2012, 12, 12)))


def test_to_datetime_passes_through_datetimes():
    a_datetime = datetime(2012, 12, 12, 13).replace(tzinfo=pytz.UTC)
    assert_that(to_datetime(a_datetime), equal_to(a_datetime))


def test_to_datetime_converts_dates():
    a_date = date(2012, 12, 12)
    a_datetime = datetime(2012, 12, 12).replace(tzinfo=pytz.UTC)

# Path: performanceplatform/utils/datetimeutil.py
# def period_range(start, end, frequency):
#     start_date = to_date(start)
#     end_date = to_date(end)
#     if start_date > end_date:
#         raise ValueError("Bad dates: !(start_date={0} <= end_date={1})"
#                          .format(start_date, end_date))
#     if frequency == 'daily':
#         start_date = start_date or a_day_ago()
#         end_date = end_date or a_day_ago()
#         period = timedelta(days=1)
#         return generate_date_range(start_date, end_date, period)
#     elif frequency == 'weekly':
#         start_date = start_date or a_week_ago()
#         start_date = start_of_week(start_date)
#         end_date = end_date or a_week_ago()
#         period = timedelta(days=7)
#         return generate_date_range(start_date, end_date, period)
#     elif frequency == 'monthly':
#         start_date = start_date or a_month_ago()
#         start_date = start_of_month(start_date)
#         end_date = end_date or a_month_ago()
#         period = relativedelta(months=+1)
#         return generate_date_range(start_date, end_date, period)
#     else:
#         raise ValueError('Bad value of frequency, should be daily, weekly '
#                          'or monthly')
# 
# def to_date(a_datetime):
#     if a_datetime is None:
#         return None
#     elif isinstance(a_datetime, datetime):
#         return a_datetime.date()
#     elif isinstance(a_datetime, date):
#         return a_datetime
#     else:
#         raise ValueError("{0!r} ({1}) isn't a date or datetime"
#                          .format(a_datetime, type(a_datetime)))
# 
# def to_datetime(a_date):
#     if type(a_date) == datetime:
#         return a_date.replace(tzinfo=pytz.UTC)
#     return datetime.combine(a_date, time(0)).replace(tzinfo=pytz.UTC)


# Path: tests/performanceplatform/utils/test_datetimeutil.py
from datetime import date, datetime
from hamcrest import assert_that, only_contains, is_, equal_to
from nose.tools import raises
from freezegun import freeze_time
from performanceplatform.utils.datetimeutil import(
    period_range,
    to_date,
    to_datetime)
import pytz



def test_to_date_passes_none_through():
    assert_that(to_date(None), is_(None))


def test_to_date_converts_datetime_to_date():
    assert_that(
        to_date(datetime(2012, 12, 12)),
        equal_to(date(2012, 12, 12)))


def test_to_date_passes_date_through():
    assert_that(
        to_date(date(2012, 12, 12)),
        equal_to(date(2012, 12, 12)))


def test_to_datetime_passes_through_datetimes():
    a_datetime = datetime(2012, 12, 12, 13).replace(tzinfo=pytz.UTC)
    assert_that(to_datetime(a_datetime), equal_to(a_datetime))


def test_to_datetime_converts_dates():
    a_date = date(2012, 12, 12)
    a_datetime = datetime(2012, 12, 12).replace(tzinfo=pytz.UTC)

    assert_that(to_datetime(a_date), equal_to(a_datetime))