Skip to content

Latest commit

 

History

History
96 lines (56 loc) · 1.64 KB

README.rst

File metadata and controls

96 lines (56 loc) · 1.64 KB

mp-yearmonth

A year-month datatype for Python.

Installation

pip install mp-yearmonth

Usage

from mp_yearmonth import YearMonth

ym = YearMonth(2019, 1)
print(ym) # 2019-01

Getting the current year-month

ym = YearMonth.current()

You can also specify a timezone using the tz argument:

import pytz

ym = YearMonth.current(tz=pytz.timezone("Asia/Tokyo"))

Parsing ISO 8601 strings

ym = YearMonth.parse("2019-01")

print(ym.year) # 2019
print(ym.month) # 1

Comparing year-months

ym1 = YearMonth(2019, 1)
ym2 = YearMonth(2019, 2)

print(ym1 == ym2) # False
print(ym1 < ym2) # True

Adding or subtracting months

ym = YearMonth(2019, 1)
ym += 1

print(ym) # 2019-02

Iterating over a range of year-months

ym1 = YearMonth(2019, 1)
ym2 = YearMonth(2019, 3)

for ym in YearMonth.range(ym1, ym2):
    print(ym) # 2019-01, 2019-02, 2019-03

Calculating the distance between two year-months

ym1 = YearMonth(2019, 1)
ym2 = YearMonth(2019, 3)

distance = ym1.distance_to(ym2) # 2

License

mp-yearmonth is licensed under the MIT license. See LICENSE for details.