Skip to main content

A year-month datatype for Python.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mp_yearmonth-0.4.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

mp_yearmonth-0.4.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file mp_yearmonth-0.4.0.tar.gz.

File metadata

  • Download URL: mp_yearmonth-0.4.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for mp_yearmonth-0.4.0.tar.gz
Algorithm Hash digest
SHA256 50ff293f3216e669edd986df1e3b54c3fe5fdffe797b663f726b2589fababae8
MD5 e35338c2b4dd8b06c979629e8be50f73
BLAKE2b-256 8c144ae721cdcb23a1fd6ebcb42302fe393a22a283989ed5ea609c82d506426d

See more details on using hashes here.

File details

Details for the file mp_yearmonth-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mp_yearmonth-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae8a0581359ac777e1078a5ad957ea37712238b0308cf48fea345238b852f9f3
MD5 2010e1c550a40aff54de68fde812cbf8
BLAKE2b-256 7799ba01205adba96e2383dcdff707701cd3c3bf1a80565bed40442dfeec8192

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page