"""Reusable Garcon Tasks related to manipulations with dates.""" from datetime import datetime, timedelta from garcon import task @task.decorate(timeout=1000) def get_first_day_of_month(activity, date): """Return the first day of the month of date. Args: activity (ActivityWorker): The activity worker. date (str): Reporting Date (YYYY-MM-DD). Returns: dict: With the first day of month (YYYY-MM-DD). """ date_obj = (datetime.strptime( date, '%Y-%m-%d') if date else datetime.today()).replace(day=1) return {'date': date_obj.strftime('%Y-%m-%d')} @task.decorate(timeout=1000) def get_first_day_of_week(activity, date): """Return the first day of the week of date. Args: activity (ActivityWorker): The activity worker. date (str): Reporting Date (YYYY-MM-DD). Returns: dict: With the first day of week (YYYY-MM-DD). """ date_obj = (datetime.strptime( date, '%Y-%m-%d') if date else datetime.today()) date_obj = date_obj - timedelta(days=date_obj.weekday()) return {'date': date_obj.strftime('%Y-%m-%d')}