[Python] rrule.between skips months with missing days

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to generate employee monthly anniversaries for a use-case I am working on. RRule offers a very convenient way to do this. However, I am unable to get the results I want from it.

    See snippet below -

    import arrow
    from dateutil.rrule import MONTHLY, rrule

    by_monthday = 31
    start = arrow.get("2021-12-31").to(tz="utc")
    end = arrow.get("2022-06-01").to(tz="utc")

    rule = rrule(
    freq=MONTHLY,
    bymonthday=by_monthday,
    byhour=0,
    byminute=0,
    bysecond=0,
    dtstart=start.datetime,
    )

    for r in rule.between(start.datetime, end.datetime, inc=True):
    print(r)


    Result --

    2021-12-31 00:00:00+00:00
    2022-01-31 00:00:00+00:00
    2022-03-31 00:00:00+00:00
    2022-05-31 00:00:00+00:00


    rrule skips over the months that do not have the 31st which IMO is the expected behaviour of the library.

    How can I handle the missing months in a clean way and set their default value to something like 2022-02-28 or 2022-04-30

    Continue reading...

Compartilhe esta Página