Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying timezone #12

Merged
merged 5 commits into from Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## v0.4.0

### Added

- `YearMonth.current()` now accepts a `tz` argument to specify the timezone.

### Changed

- Made `YearMonth` instances immutable.
Expand Down
25 changes: 20 additions & 5 deletions README.md
Expand Up @@ -17,7 +17,21 @@ ym = YearMonth(2019, 1)
print(ym) # 2019-01
```

Parsing ISO 8601 strings:
### Getting the current year-month

```python
ym = YearMonth.current()
```

You can also specify a timezone using the `tz` argument:

```python
import pytz

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

### Parsing ISO 8601 strings

```python
ym = YearMonth.parse("2019-01")
Expand All @@ -26,16 +40,17 @@ print(ym.year) # 2019
print(ym.month) # 1
```

Comparisons:
### Comparing year-months

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

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

Addition:
### Adding or subtracting months

```python
ym = YearMonth(2019, 1)
Expand All @@ -44,7 +59,7 @@ ym += 1
print(ym) # 2019-02
```

Range:
### Iterating over a range of year-months

```python
ym1 = YearMonth(2019, 1)
Expand All @@ -54,7 +69,7 @@ for ym in YearMonth.range(ym1, ym2):
print(ym) # 2019-01, 2019-02, 2019-03
```

Calculate the distance between two year-months:
### Calculating the distance between two year-months

```python
ym1 = YearMonth(2019, 1)
Expand Down
14 changes: 9 additions & 5 deletions mp_yearmonth/yearmonth.py
Expand Up @@ -7,7 +7,7 @@
import re
import calendar
import datetime
from typing import Iterator, Tuple
from typing import Iterator, Optional, Tuple

_FORMAT_PATTERN = re.compile(r"^(\d{4})-(\d{2})$")

Expand Down Expand Up @@ -46,7 +46,7 @@ class YearMonth:
def __init__(self, year: int, month: int):
"""Create a new YearMonth object.

Args:
Arguments:
year: The year.
month: The month."""
if not isinstance(year, int):
Expand Down Expand Up @@ -139,9 +139,13 @@ def numdays(self) -> int:
return days

@classmethod
def current(cls) -> "YearMonth":
"""Return the current year and month."""
now = datetime.date.today()
def current(cls, tz: Optional[datetime.tzinfo] = None) -> "YearMonth":
"""Return the current year and month.

Arguments:
tz: The timezone to use. If None, the system's local timezone is used.
"""
now = datetime.datetime.now(tz)
return cls(now.year, now.month)

@classmethod
Expand Down
47 changes: 43 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Expand Up @@ -25,6 +25,7 @@ repository = "https://github.com/raymondjavaxx/mp-yearmonth"

[tool.poetry.group.test.dependencies]
pytest = "^7.4.3"
freezegun = "^1.4.0"

[build-system]
requires = ["poetry-core"]
Expand Down
10 changes: 9 additions & 1 deletion tests/test_yearmonth.py
@@ -1,5 +1,6 @@
import pytest
import datetime
from freezegun import freeze_time
from mp_yearmonth import YearMonth


Expand Down Expand Up @@ -102,8 +103,15 @@ def test_numdays():
assert YearMonth(2020, 2).numdays == 29


@freeze_time("2021-01-01") # UTC
def test_current():
assert YearMonth.current() == YearMonth.parse(str(YearMonth.current()))
assert YearMonth.current() == YearMonth(2021, 1)


@freeze_time("2021-01-01") # UTC
def test_current_handles_timezone():
tz = datetime.timezone(datetime.timedelta(hours=-1))
assert YearMonth.current(tz) == YearMonth(2020, 12)


def test_parse():
Expand Down