monthday¶
This package provides the MonthDay
value type for dealing dates without
year. It is useful for dealing with birthdays, or anniversaries.
Works on Python 2.6, 2.7, 3.2–3.5, PyPy, PyPy3.
>>> from monthday import *
>>> aug_4 = MonthDay(8, 4)
>>> aug_4
monthday.MonthDay(8, 4)
>>> aug_4.date(1988)
datetime.date(1988, 8, 4)
>>> list(aug_4.dates(range(2013, 2016)))
[datetime.date(2013, 8, 4),
datetime.date(2014, 8, 4),
datetime.date(2015, 8, 4)]
>>> from datetime import date
>>> MonthDay.from_date(date(2015, 12, 25))
monthday.MonthDay(12, 25)
It’s available on PyPI:
$ pip install monthday
Written by Hong Minhee, and distributed under LGPLv3 or later. Find the source code from the GitHub repository.
monthday
— Date without year¶
-
class
monthday.
MonthDay
(month, day)¶ Date without year. Useful for birthdays, or anniversaries.
Parameters: - month (
numbers.Integral
) – a month number, from 1 to 12 - day (
numbers.Integral
) – a day of themonth
, from 1 to 31 (or 30, or 29)
Raises ValueError: if
month
ordate
is out of valid range-
month
¶ (
numbers.Integral
) The month number, from 1 to 12.
-
day
¶ (
numbers.Integral
) The day of themonth
, from 1 to 31.
-
date
(year)¶ Get a
date
by combining the givenyear
with it.>>> MonthDay(12, 25).date(2015) datetime.date(2015, 12, 25)
It may raise
ValueError
if February 29 is tried to be combined with a non-leap year e.g.:>>> feb_29 = MonthDay(2, 29) >>> feb_29.date(2012) datetime.date(2012, 2, 29) >>> feb_29.date(2013) Traceback (most recent call last): ... ValueError: since 2013 is not a leap year, monthday.MonthDay(2, 29) can't be combined with 2013
Parameters: year ( numbers.Integral
) – a year to combine withReturns: a datetime.date
with the givenyear
Return type: datetime.date
Raises ValueError: when year
is not a leap year while it’sMonthDay(2, 29)
-
dates
(years, error_invalid_dates=True)¶ Get
date
s by combining the givenyears
with it.>>> list(MonthDay(8, 4).dates(range(1988, 1992))) [datetime.date(1988, 8, 4), datetime.date(1989, 8, 4), datetime.date(1990, 8, 4), datetime.date(1991, 8, 4)]
It may raise
ValueError
if there happen to be any invalid dates in the result, e.g. Feburary 29 for non-leap years:>>> feb_29 = MonthDay(2, 29) >>> list(feb_29.dates(range(2011, 2017))) Traceback (most recent call last): ... ValueError: since 2010 is not a leap year, monthday.MonthDay(2, 29) can't be combined with 2010
If you want to simply ignore these invalid dates in the result, set
error_invalid_dates
toFalse
e.g.:>>> list(feb_29.dates(range(2011, 2017), error_invalid_dates=False)) [datetime.date(2012, 2, 29), datetime.date(2016, 2, 29)]
But the result length might be shorter than the input
years
list.If you want to match the length of the input and the result, set
error_invalid_dates
toNone
— it will replace invalid dates in the result withNone
values e.g.:>>> list(feb_29.dates(range(2011, 2017), error_invalid_dates=None)) [None, datetime.date(2012, 2, 29), None, None, None, datetime.date(2016, 2, 29)]
Parameters: - years (
Iterable
) – years to combine with - error_invalid_dates (
bool
,type(None)
) – if set toTrue
, raiseValueError
for invalid dates. if set toFalse
, just ignore invalid dates — the result length might be shorter than the inputyears
list. if set toNone
, fillNone
values instead of invalid dates — the result length must be the same to the inputyear
list.True
by default
Returns: datetime.date
values with the givenyears
. the order corresponds to the inputyears
‘ orderReturn type: Raises: - ValueError – if
error_invalid_dates
is set toTrue
and there happend to be any invalid dates in the result - TypeError – if
years
is not iterable of integers
- years (
-
classmethod
from_date
(date)¶ Get only
MonthDay
from the givendate
.Parameters: date ( datetime.date
,datetime.datetime
) – the date or date/timeReturns: MonthDay
withoutdate
‘syear
Return type: MonthDay
- month (
Changelog¶
Version 0.9.1¶
To be released.
Version 0.9.0¶
The first alpha version. Released on November 29, 2015.