Skip to content

Commit 9697d2a

Browse files
committed
feat: helpers to calculate prior (local) month range
by default use the America/Los_Angeles time zone override with TZ_NAME env var
1 parent 1166cab commit 9697d2a

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ TOGGL_USER_INFO=data/toggl-user-info-sample.json
88
TOGGL_API_TOKEN=token
99
TOGGL_CLIENT_ID=client
1010
TOGGL_WORKSPACE_ID=workspace
11+
12+
TZ_NAME=America/Los_Angeles

compiler_admin/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from argparse import ArgumentParser, _SubParsersAction
2+
from datetime import datetime, timedelta
3+
import os
24
import sys
5+
from zoneinfo import ZoneInfo
36

47
from compiler_admin import __version__ as version
58
from compiler_admin.commands.info import info
@@ -9,6 +12,24 @@
912
from compiler_admin.commands.user.convert import ACCOUNT_TYPE_OU
1013

1114

15+
TZINFO = ZoneInfo(os.environ.get("TZ_NAME", "America/Los_Angeles"))
16+
17+
18+
def local_now():
19+
return datetime.now(tz=TZINFO)
20+
21+
22+
def prior_month_end():
23+
now = local_now()
24+
first = now.replace(day=1)
25+
return first - timedelta(days=1)
26+
27+
28+
def prior_month_start():
29+
end = prior_month_end()
30+
return end.replace(day=1)
31+
32+
1233
def add_sub_cmd_parser(parser: ArgumentParser, dest="subcommand", help=None):
1334
"""Helper adds a subparser for the given dest."""
1435
return parser.add_subparsers(dest=dest, help=help)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ requires-python = ">=3.11"
1313
dependencies = [
1414
"advanced-gam-for-google-workspace @ git+https://github.com/taers232c/[email protected]#subdirectory=src",
1515
"pandas==2.2.3",
16+
"tzdata",
1617
]
1718

1819
[project.urls]

tests/test_main.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
from argparse import Namespace
2+
from datetime import datetime
23
import subprocess
34
import sys
45

56
import pytest
67

78
import compiler_admin.main
8-
from compiler_admin.main import main, __name__ as MODULE
9+
from compiler_admin.main import main, prior_month_start, prior_month_end, TZINFO, __name__ as MODULE
910
from compiler_admin.services.google import DOMAIN
1011

1112

13+
@pytest.fixture
14+
def mock_local_now(mocker):
15+
dt = datetime(2024, 9, 25, tzinfo=TZINFO)
16+
mocker.patch(f"{MODULE}.local_now", return_value=dt)
17+
return dt
18+
19+
20+
@pytest.fixture
21+
def mock_start(mock_local_now):
22+
return datetime(2024, 8, 1, tzinfo=TZINFO)
23+
24+
25+
@pytest.fixture
26+
def mock_end(mock_local_now):
27+
return datetime(2024, 8, 31, tzinfo=TZINFO)
28+
29+
1230
@pytest.fixture
1331
def mock_commands_info(mock_commands_info):
1432
return mock_commands_info(MODULE)
@@ -29,6 +47,18 @@ def mock_commands_user(mock_commands_user):
2947
return mock_commands_user(MODULE)
3048

3149

50+
def test_prior_month_start(mock_start):
51+
start = prior_month_start()
52+
53+
assert start == mock_start
54+
55+
56+
def test_prior_month_end(mock_end):
57+
end = prior_month_end()
58+
59+
assert end == mock_end
60+
61+
3262
def test_main_info(mock_commands_info):
3363
main(argv=["info"])
3464

0 commit comments

Comments
 (0)