datetime.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from datetime import datetime
  2. from typing import List, Dict
  3. def filter_accounts_current_month(accounts: List[Dict[str, int]]) -> List[Dict[str, int]]:
  4. """Filter accounts whose expiry date falls within the current month.
  5. Args:
  6. accounts (List[Dict[str, int]]): A list of account dictionaries, each containing
  7. an 'expiaryDate' key with an epoch timestamp as its value.
  8. Returns:
  9. List[Dict[str, int]]: A list of accounts expiring in the current month.
  10. """
  11. # Get the start of the current month
  12. now = datetime.now()
  13. start_of_month = datetime(now.year, now.month, 1)
  14. # Determine the start of the next month
  15. if now.month == 12:
  16. # If the current month is December, next month is January of the next year
  17. start_of_next_month = datetime(now.year + 1, 1, 1)
  18. else:
  19. # Otherwise, the next month is the following month of the same year
  20. start_of_next_month = datetime(now.year, now.month + 1, 1)
  21. # Convert start and end of the month to epoch timestamps
  22. start_of_month_timestamp = int(start_of_month.timestamp())
  23. start_of_next_month_timestamp = int(start_of_next_month.timestamp())
  24. # Filter accounts with expiryDate in the current month
  25. accounts_in_current_month = [
  26. account for account in accounts
  27. if start_of_month_timestamp <= account['expiaryDate'] < start_of_next_month_timestamp
  28. ]
  29. return accounts_in_current_month
  30. def filter_accounts_expired(accounts: List[Dict[str, int]]) -> List[Dict[str, int]]:
  31. """Filter accounts whose expiry date has passed.
  32. Args:
  33. accounts (List[Dict[str, int]]): A list of account dictionaries, each containing
  34. an 'expiaryDate' key with an epoch timestamp as its value.
  35. Returns:
  36. List[Dict[str, int]]: A list of accounts that have expired.
  37. """
  38. # Get the current epoch timestamp
  39. current_timestamp = int(datetime.now().timestamp())
  40. # Filter accounts where the current date is greater than the expiryDate
  41. expired_accounts = [
  42. account for account in accounts
  43. if account['expiaryDate'] < current_timestamp
  44. ]
  45. return expired_accounts