Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

54 lines
2.2KB

  1. # Copyright (C) 2019 Campaign Against Arms Trade
  2. #
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. """Collection of functions to deal with datetime manipulation"""
  15. from datetime import datetime, timedelta
  16. from . import LOG
  17. def get_week_datetime(start: int = 0):
  18. """
  19. Gets the current week's Monday and Friday to be used to filter a calendar.
  20. If this script is ran during the work week (Monday-Friday), it will be the current week.
  21. If it is ran on the weekend, it will generate for next week.
  22. :param start: Specifies the today variable, used for make future weeks if given.
  23. :return: Monday and Friday: Datetime objects
  24. """
  25. today = datetime.now() + timedelta(weeks=start)
  26. if start > 5:
  27. LOG.warning("Extra weeks exceeds 5, script may run slowly.")
  28. weekday = today.weekday()
  29. extra_time = timedelta(
  30. hours=today.hour,
  31. minutes=today.minute,
  32. seconds=today.second,
  33. microseconds=today.microsecond
  34. )
  35. monday = today - timedelta(days=weekday) - extra_time # Monday = 0-0, Friday = 4-4
  36. friday = (today + timedelta(days=4 - weekday)) - extra_time + timedelta(hours=23, minutes=59)
  37. # If this script is run during the week
  38. if weekday <= 4: # 0 = Monday, 6 = Sunday
  39. # - the hour and minutes to get start of the day instead of when the
  40. return monday, friday
  41. # If the date the script is ran on is the weekend, do next week instead
  42. monday = monday + timedelta(days=7) # Monday = 0-0, Friday = 4-4
  43. friday = friday + timedelta(days=7) # Fri to Fri 4 + (4 - 4), Tues to Fri = 2 + (4 - 2)
  44. return monday, friday