diff --git a/endpoints/api/logs.py b/endpoints/api/logs.py index 92bbcee93..4610dfb91 100644 --- a/endpoints/api/logs.py +++ b/endpoints/api/logs.py @@ -3,6 +3,7 @@ import json from datetime import datetime, timedelta +from dateutil.relativedelta import relativedelta from endpoints.api import (resource, nickname, ApiResource, query_param, parse_args, RepositoryParamResource, require_repo_admin, related_user_resource, @@ -39,15 +40,14 @@ def aggregated_log_view(log, kinds, start_time): # Because we aggregate based on the day of the month in SQL, we only have that information. # Therefore, create a synthetic date based on the day and the month of the start time. # Logs are allowed for a maximum period of one week, so this calculation should always work. - day = int(log.day) - month = start_time.month - if day < start_time.day: - month = month + 1 + synthetic_date = datetime(start_time.year, start_time.month, int(log.day)) + if synthetic_date.day < start_time.day: + synthetic_date = synthetic_date + relativedelta(months=1) view = { 'kind': kinds[log.kind_id], 'count': log.count, - 'datetime': format_date(datetime(start_time.year, month, day)) + 'datetime': format_date(synthetic_date), } return view