Advertisement
Brovashift

date

Apr 9th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. from datetime import date, timedelta, datetime
  2.  
  3.  
  4. def check_date(date):
  5.     if '-' in date and len(date.split('-')) < 3:
  6.         return valid_date(date.split('-')[0]) and valid_date(date.split('-')[1])
  7.    
  8.     return valid_date(date)
  9.  
  10.  
  11. def convert_date(date):
  12.     dmy = date.split('-')
  13.     return dmy[0] + '-' + dmy[1] + '-' + dmy[2]
  14.  
  15.  
  16. def get_dates(date_str):
  17.     if '-' in date_str:
  18.         start_year, start_month, start_day = date_str.split('-')[0].split('/')
  19.         end_year, end_month, end_day = date_str.split('-')[1].split('/')
  20.  
  21.         start_date = date(int(start_year), int(start_month), int(start_day))
  22.         end_date = date(int(end_year), int(end_month), int(end_day))
  23.        
  24.         return [start_date + timedelta(days=x) for x in range((end_date - start_date).days + 1)]
  25.     else:
  26.         year, month, day = date_str.split('/')
  27.        
  28.         return [date(int(year), int(month), int(day))]
  29.  
  30.  
  31. def parse_years(year_str):
  32.     if '-' in year_str:
  33.         try:
  34.             return [str(x) for x in range(int(year_str.split('-')[0]), int(year_str.split('-')[1]) + 1)]
  35.         except ValueError:
  36.             return []
  37.     else:
  38.         return [year_str]
  39.  
  40.  
  41. def valid_date(date):
  42.     if len(date.split('/')) == 3:
  43.         try:
  44.             year, month, day = [int(x) for x in date.split('/')]
  45.             return 1987 <= year <= int(datetime.today().year) and 0 < month <= 12 and 0 < day <= 31
  46.         except ValueError:
  47.             return False
  48.  
  49.     return False
  50.  
  51.  
  52. def valid_years(years):
  53.     if years:
  54.         return all(year.isdigit() and 1987 <= int(year) <= int(datetime.today().year) for year in years)
  55.  
  56.     return False
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement