# Path: quantrocket/utils/dt.py
# def segmented_date_range(start_date, end_date, segment="A"):
#     """
#     Split a date range into smaller segments.
# 
#     Parameters
#     ----------
# 
#     start_date : str (YYYY-MM-DD), required
#         start date, inclusive
# 
#     end_date : str (YYYY-MM-DD), required
#         end date, inclusive
# 
#     segment : str, required
#         split date range into segments of this size (use Pandas
#         frequency string, e.g. 'A' for annual segments or 'Q' for quarterly
#         segments; default 'A')
# 
#     Returns
#     -------
#     list
#         list of tuples of (start_date, end_date)
# 
#     Examples
#     --------
#     >>> segmented_date_range("2013-06-01", "2015-12-15")
#     [('2013-06-01', '2013-12-30'),
#     ('2013-12-31', '2014-12-30'),
#     ('2014-12-31', '2015-12-15')]
#     """
# 
#     # Import pandas lazily since it can take a moment to import
#     try:
#         import pandas as pd
#     except ImportError:
#         raise ImportError("pandas must be installed to use this function")
# 
#     date_segments = []
#     period_boundaries = list(pd.date_range(start_date, end_date, freq=segment))
#     start_date = pd.Timestamp(start_date)
#     end_date = pd.Timestamp(end_date)
#     if start_date not in period_boundaries:
#         period_boundaries.insert(0, start_date)
#     if end_date not in period_boundaries:
#         period_boundaries.append(end_date)
# 
#     for i in range(len(period_boundaries)-1):
#         period_start_date = period_boundaries[i].date()
#         period_end_date = period_boundaries[i+1].date()
#         if period_end_date < end_date.date():
#             period_end_date -= pd.Timedelta(days=1)
# 
#         period_start_date = period_start_date.isoformat()
#         period_end_date = period_end_date.isoformat()
#         date_segments.append((period_start_date, period_end_date))
# 
#     return date_segments


import unittest
from quantrocket.utils import segmented_date_range
class DateUtilsTestCase(unittest.TestCase):
    """
    Test cases for `quantrocket.utils.segment_date_range`.
    """

    def test_segmented_date_range(self):
        segments = segmented_date_range("2010-01-01", "2018-01-01")
        self.assertListEqual(
            segments,
            [('2010-01-01', '2010-12-30'),
             ('2010-12-31', '2011-12-30'),
             ('2011-12-31', '2012-12-30'),
             ('2012-12-31', '2013-12-30'),
             ('2013-12-31', '2014-12-30'),
             ('2014-12-31', '2015-12-30'),
             ('2015-12-31', '2016-12-30'),
             ('2016-12-31', '2017-12-30'),
             ('2017-12-31', '2018-01-01')]
        )

        segments = segmented_date_range("2010-06-15","2014-04-05", segment="A")
        self.assertListEqual(
            segments,
            [('2010-06-15', '2010-12-30'),
             ('2010-12-31', '2011-12-30'),
             ('2011-12-31', '2012-12-30'),
             ('2012-12-31', '2013-12-30'),
             ('2013-12-31', '2014-04-05')]
        )


# Path: quantrocket/utils/dt.py
# def segmented_date_range(start_date, end_date, segment="A"):
#     """
#     Split a date range into smaller segments.
# 
#     Parameters
#     ----------
# 
#     start_date : str (YYYY-MM-DD), required
#         start date, inclusive
# 
#     end_date : str (YYYY-MM-DD), required
#         end date, inclusive
# 
#     segment : str, required
#         split date range into segments of this size (use Pandas
#         frequency string, e.g. 'A' for annual segments or 'Q' for quarterly
#         segments; default 'A')
# 
#     Returns
#     -------
#     list
#         list of tuples of (start_date, end_date)
# 
#     Examples
#     --------
#     >>> segmented_date_range("2013-06-01", "2015-12-15")
#     [('2013-06-01', '2013-12-30'),
#     ('2013-12-31', '2014-12-30'),
#     ('2014-12-31', '2015-12-15')]
#     """
# 
#     # Import pandas lazily since it can take a moment to import
#     try:
#         import pandas as pd
#     except ImportError:
#         raise ImportError("pandas must be installed to use this function")
# 
#     date_segments = []
#     period_boundaries = list(pd.date_range(start_date, end_date, freq=segment))
#     start_date = pd.Timestamp(start_date)
#     end_date = pd.Timestamp(end_date)
#     if start_date not in period_boundaries:
#         period_boundaries.insert(0, start_date)
#     if end_date not in period_boundaries:
#         period_boundaries.append(end_date)
# 
#     for i in range(len(period_boundaries)-1):
#         period_start_date = period_boundaries[i].date()
#         period_end_date = period_boundaries[i+1].date()
#         if period_end_date < end_date.date():
#             period_end_date -= pd.Timedelta(days=1)
# 
#         period_start_date = period_start_date.isoformat()
#         period_end_date = period_end_date.isoformat()
#         date_segments.append((period_start_date, period_end_date))
# 
#     return date_segments


# Path: tests/test_utils.py
import unittest
from quantrocket.utils import segmented_date_range

class DateUtilsTestCase(unittest.TestCase):
    """
    Test cases for `quantrocket.utils.segment_date_range`.
    """

    def test_segmented_date_range(self):
        segments = segmented_date_range("2010-01-01", "2018-01-01")
        self.assertListEqual(
            segments,
            [('2010-01-01', '2010-12-30'),
             ('2010-12-31', '2011-12-30'),
             ('2011-12-31', '2012-12-30'),
             ('2012-12-31', '2013-12-30'),
             ('2013-12-31', '2014-12-30'),
             ('2014-12-31', '2015-12-30'),
             ('2015-12-31', '2016-12-30'),
             ('2016-12-31', '2017-12-30'),
             ('2017-12-31', '2018-01-01')]
        )

        segments = segmented_date_range("2010-06-15","2014-04-05", segment="A")
        self.assertListEqual(
            segments,
            [('2010-06-15', '2010-12-30'),
             ('2010-12-31', '2011-12-30'),
             ('2011-12-31', '2012-12-30'),
             ('2012-12-31', '2013-12-30'),
             ('2013-12-31', '2014-04-05')]
        )


        segments = segmented_date_range("2010-06-15","2014-04-05", segment="2A")