Skip to content

Commit

Permalink
Fix error that only occurs on July 31st
Browse files Browse the repository at this point in the history
  • Loading branch information
rdb committed Jul 31, 2024
1 parent 88b505a commit d4dda79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "toggl2moneybird"
version = "0.5.0"
version = "0.5.1"
authors = [
{name = "rdb", email = "[email protected]"},
]
Expand Down
50 changes: 26 additions & 24 deletions toggl2moneybird/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import keyring
import getpass
import webbrowser
from datetime import datetime, date
from datetime import datetime, date, timedelta
from collections import defaultdict
from difflib import ndiff
from typing import DefaultDict
Expand Down Expand Up @@ -40,6 +40,25 @@ def format_currency(amount, currency=None):
return f'{amount:,.2f}'


def relaxed_date(year, month, day):
if day > 28:
days_in_month = (relaxed_date(year, month + 1, 1) - timedelta(days=1)).day
if day > days_in_month:
# Too many days for month, roll over
month += 1
day -= days_in_month

while month < 1:
month += 12
year -= 1

while month > 12:
month -= 12
year += 1

return date(year, month, day)


def ask_hourly_rate_for(mb_project, default_currency=None):
while True:
ret = Prompt.ask(f"Hourly rate for project {mb_project.__rich__()}")
Expand Down Expand Up @@ -315,24 +334,15 @@ def do_mutations(console, args, mb_admin, mutations):
def cmd_sync(console, args, mb_admin):
today = date.today()
if back_months < 3:
if today.month - back_months < 1:
start_date = date(today.year - 1, 12 + today.month - back_months, 1)
else:
start_date = date(today.year, today.month - back_months, 1)
start_date = relaxed_date(today.year, today.month - back_months, 1)
else:
# Limit of the Toggl Track API
if today.month - 3 < 1:
start_date = date(today.year - 1, 12 + today.month - 3, today.day)
else:
start_date = date(today.year, today.month - 3, today.day)
start_date = relaxed_date(today.year, today.month - 3, today.day)

if start_date < earliest_start_date:
start_date = earliest_start_date

if today.month == 12:
end_date = date(today.year + 1, 1, 1)
else:
end_date = date(today.year, today.month + 1, 1)
end_date = relaxed_date(today.year, today.month + 1, 1)

console.print(Text.assemble("Synchronizing time entries from ", (start_date.isoformat(), "bold green"), " to ", (end_date.isoformat(), "bold magenta")))

Expand Down Expand Up @@ -395,26 +405,18 @@ def cmd_sync(console, args, mb_admin):

def cmd_invoice(console, args, mb_admin):
today = date.today()
if today.month - back_months < 1:
start_date = date(today.year - 1, 12 + today.month - back_months, 1)
else:
start_date = date(today.year, today.month - back_months, 1)
start_date = relaxed_date(today.year, today.month - back_months, 1)

# Toggl Track has a limit on the earliest start date
if back_months < 3:
tt_start_date = start_date
elif today.month - 3 < 1:
tt_start_date = date(today.year - 1, 12 + today.month - 3, today.day)
else:
tt_start_date = date(today.year, today.month - 3, today.day)
tt_start_date = relaxed_date(today.year, today.month - 3, today.day)

if start_date < earliest_start_date:
start_date = earliest_start_date

if today.month == 12:
end_date = date(today.year + 1, 1, 1)
else:
end_date = date(today.year, today.month + 1, 1)
end_date = relaxed_date(today.year, today.month + 1, 1)

with Progress(console=console, transient=True) as progress:
entries = mb_admin.get_time_entries(start_date, end_date, progress=progress)
Expand Down

0 comments on commit d4dda79

Please sign in to comment.