API Documentation

Bases: when_exactly.custom_interval.CustomInterval

The Year represents an entire year, starting from January 1 to December 31.

Creating a Year

>>> import when_exactly as we

>>> year = we.Year(2025)
>>> year
Year(2025)

>>> str(year)
'2025'

The Months of a Year

Get the Months of a year.

>>> months = year.months
>>> len(months)
12

>>> months[0]
Month(2025, 1)

>>> months[-2:]
Months([Month(2025, 11), Month(2025, 12)])

The Weeks of a Year

Get the Weeks of a year.

>>> weeks = year.weeks
>>> len(weeks)
52

>>> weeks[0]
Week(2025, 1)

Source code in src\when_exactly\api.py
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
@dataclasses.dataclass(frozen=True, init=False, repr=False)
class Year(CustomInterval):
    """The `Year` represents an entire year, starting from _January 1_ to _December 31_.

    ## Creating a Year

    ```python
    >>> import when_exactly as we

    >>> year = we.Year(2025)
    >>> year
    Year(2025)

    >>> str(year)
    '2025'

    ```

    ## The Months of a Year

    Get the [`Months`](months.md) of a year.

    ```python
    >>> months = year.months
    >>> len(months)
    12

    >>> months[0]
    Month(2025, 1)

    >>> months[-2:]
    Months([Month(2025, 11), Month(2025, 12)])

    ```

    ## The Weeks of a Year

    Get the [`Weeks`](weeks.md) of a year.

    ```python
    >>> weeks = year.weeks
    >>> len(weeks)
    52

    >>> weeks[0]
    Week(2025, 1)

    ```
    """

    def __init__(self, year: int) -> None:
        """# Create a Year.

        Parameters:
            year: The year to represent.

        Examples:
            ```python
            >>> import when_exactly as we

            >>> year = we.Year(2025)
            >>> year
            Year(2025)

            >>> str(year)
            '2025'

            ```
        """

        Interval.__init__(
            self,
            start=Moment(year, 1, 1, 0, 0, 0),
            stop=Moment(year + 1, 1, 1, 0, 0, 0),
        )

    def __repr__(self) -> str:
        return f"Year({self.start.year})"

    def __str__(self) -> str:
        return f"{self.start.year:04}"

    @classmethod
    def from_moment(cls, moment: Moment) -> Year:
        return Year(moment.year)

    @cached_property
    def months(self) -> Months:
        return Months([Month(self.start.year, self.start.month + i) for i in range(12)])

    @cached_property
    def weeks(self) -> Weeks:
        return Weeks(
            _gen_until(
                Week(self.start.year, 1),
                Week(self.start.year + 1, 1),
            )
        )

    def month(self, month: int) -> Month:
        """Get a specific month of the year.
        Args:
            month (int): The month number (1-12).
        """
        return Month(
            self.start.year,
            month,
        )

    def __next__(self) -> Year:
        return Year.from_moment(self.stop)

    def week(self, week: int) -> Week:
        return Week(
            self.start.year,
            week,
        )

    def ordinal_day(self, ordinal_day: int) -> OrdinalDay:
        return OrdinalDay(
            self.start.year,
            ordinal_day,
        )

__init__(year)

Create a Year.

Parameters:

Name Type Description Default
year int

The year to represent.

required

Examples:

>>> import when_exactly as we

>>> year = we.Year(2025)
>>> year
Year(2025)

>>> str(year)
'2025'

Source code in src\when_exactly\api.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def __init__(self, year: int) -> None:
    """# Create a Year.

    Parameters:
        year: The year to represent.

    Examples:
        ```python
        >>> import when_exactly as we

        >>> year = we.Year(2025)
        >>> year
        Year(2025)

        >>> str(year)
        '2025'

        ```
    """

    Interval.__init__(
        self,
        start=Moment(year, 1, 1, 0, 0, 0),
        stop=Moment(year + 1, 1, 1, 0, 0, 0),
    )

month(month)

Get a specific month of the year. Args: month (int): The month number (1-12).

Source code in src\when_exactly\api.py
126
127
128
129
130
131
132
133
134
def month(self, month: int) -> Month:
    """Get a specific month of the year.
    Args:
        month (int): The month number (1-12).
    """
    return Month(
        self.start.year,
        month,
    )