Skip to content

Instantly share code, notes, and snippets.

@toaco
Created February 8, 2018 01:45
Show Gist options
  • Save toaco/b8fcaf0d79a6238ebb86bd97c440bcd6 to your computer and use it in GitHub Desktop.
Save toaco/b8fcaf0d79a6238ebb86bd97c440bcd6 to your computer and use it in GitHub Desktop.

Revisions

  1. toaco created this gist Feb 8, 2018.
    103 changes: 103 additions & 0 deletions demo.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    import datetime

    import arrow


    def diff_month(d1, d2):
    return (d1.year - d2.year) * 12 + d1.month - d2.month


    def calc_current_period(start_date, period, now=None, unit='months'):
    now = now or datetime.datetime.now()

    now = arrow.get(now)

    if unit == 'months':
    if now.day < start_date.day:
    now = now.shift(months=-1)

    start_date = arrow.get(start_date)

    remainder = diff_month(now, start_date) % period

    current_start_date = now.shift(months=-remainder).replace(day=start_date.day)
    current_end_date = now.shift(months=period - remainder).replace(day=start_date.day)
    return current_start_date.date(), current_end_date.date()
    elif unit == 'days':
    start_date = arrow.get(start_date)

    remainder = (now - start_date).days % period

    current_start_date = now.shift(days=-remainder)
    current_end_date = now.shift(days=period - remainder)
    return current_start_date.date(), current_end_date.date()
    else:
    raise ValueError


    # now 2.6
    class NewDateTime(datetime.datetime):
    @classmethod
    def now(cls, tz=None):
    return cls(2018, 2, 6)


    datetime.datetime = NewDateTime

    assert calc_current_period(datetime.datetime(2018, 2, 7), 1) == (datetime.date(2018, 1, 7), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 2, 6), 1) == (datetime.date(2018, 2, 6), datetime.date(2018, 3, 6))
    assert calc_current_period(datetime.datetime(2018, 2, 5), 1) == (datetime.date(2018, 2, 5), datetime.date(2018, 3, 5))
    assert calc_current_period(datetime.datetime(2018, 2, 7), 2) == (datetime.date(2017, 12, 7), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 2, 6), 2) == (datetime.date(2018, 2, 6), datetime.date(2018, 4, 6))
    assert calc_current_period(datetime.datetime(2018, 2, 5), 2) == (datetime.date(2018, 2, 5), datetime.date(2018, 4, 5))
    assert calc_current_period(datetime.datetime(2018, 1, 7), 1) == (datetime.date(2018, 1, 7), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 1, 6), 1) == (datetime.date(2018, 2, 6), datetime.date(2018, 3, 6))
    assert calc_current_period(datetime.datetime(2018, 1, 5), 1) == (datetime.date(2018, 2, 5), datetime.date(2018, 3, 5))
    assert calc_current_period(datetime.datetime(2018, 1, 7), 2) == (datetime.date(2018, 1, 7), datetime.date(2018, 3, 7))
    assert calc_current_period(datetime.datetime(2018, 1, 6), 2) == (datetime.date(2018, 1, 6), datetime.date(2018, 3, 6))
    assert calc_current_period(datetime.datetime(2018, 1, 5), 2) == (datetime.date(2018, 1, 5), datetime.date(2018, 3, 5))
    assert calc_current_period(datetime.datetime(2018, 3, 7), 2) == (datetime.date(2018, 1, 7), datetime.date(2018, 3, 7))
    assert calc_current_period(datetime.datetime(2018, 3, 6), 2) == (datetime.date(2018, 1, 6), datetime.date(2018, 3, 6))
    assert calc_current_period(datetime.datetime(2018, 3, 5), 2) == (datetime.date(2018, 1, 5), datetime.date(2018, 3, 5))

    assert calc_current_period(datetime.datetime(2018, 2, 6), 1, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 2, 5), 1, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 2, 7), 1, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 7))

    assert calc_current_period(datetime.datetime(2018, 1, 6), 1, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 1, 5), 1, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 1, 7), 1, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 7))

    assert calc_current_period(datetime.datetime(2018, 3, 6), 1, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 3, 5), 1, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 3, 7), 1, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 7))

    assert calc_current_period(datetime.datetime(2018, 2, 6), 2, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 8))
    assert calc_current_period(datetime.datetime(2018, 2, 5), 2, unit='days') == (
    datetime.date(2018, 2, 5), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 2, 7), 2, unit='days') == (
    datetime.date(2018, 2, 5), datetime.date(2018, 2, 7))

    assert calc_current_period(datetime.datetime(2018, 1, 6), 2, unit='days') == (
    datetime.date(2018, 2, 5), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 1, 5), 2, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 8))
    assert calc_current_period(datetime.datetime(2018, 1, 7), 2, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 8))

    assert calc_current_period(datetime.datetime(2018, 3, 6), 2, unit='days') == (
    datetime.date(2018, 2, 6), datetime.date(2018, 2, 8))
    assert calc_current_period(datetime.datetime(2018, 3, 5), 2, unit='days') == (
    datetime.date(2018, 2, 5), datetime.date(2018, 2, 7))
    assert calc_current_period(datetime.datetime(2018, 3, 7), 2, unit='days') == (
    datetime.date(2018, 2, 5), datetime.date(2018, 2, 7))