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
monthordateis 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
dateby combining the givenyearwith it.>>> MonthDay(12, 25).date(2015) datetime.date(2015, 12, 25)
It may raise
ValueErrorif 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.datewith the givenyearReturn type: datetime.dateRaises ValueError: when yearis not a leap year while it’sMonthDay(2, 29)
-
dates(years, error_invalid_dates=True)¶ Get
dates by combining the givenyearswith 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
ValueErrorif 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_datestoFalsee.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
yearslist.If you want to match the length of the input and the result, set
error_invalid_datestoNone— it will replace invalid dates in the result withNonevalues 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, raiseValueErrorfor invalid dates. if set toFalse, just ignore invalid dates — the result length might be shorter than the inputyearslist. if set toNone, fillNonevalues instead of invalid dates — the result length must be the same to the inputyearlist.Trueby default
Returns: datetime.datevalues with the givenyears. the order corresponds to the inputyears‘ orderReturn type: Raises: - ValueError – if
error_invalid_datesis set toTrueand there happend to be any invalid dates in the result - TypeError – if
yearsis not iterable of integers
- years (
-
classmethod
from_date(date)¶ Get only
MonthDayfrom the givendate.Parameters: date ( datetime.date,datetime.datetime) – the date or date/timeReturns: MonthDaywithoutdate‘syearReturn type: MonthDay
- month (