Advertisement
DeaD_EyE

date_convert_and_check

Feb 12th, 2020
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. import datetime
  2. from typing import List
  3. from typing import Tuple
  4. # just for type checking and bit of
  5. # documentation, but it's optional
  6.  
  7.  
  8. def convert_date(date: str, formats: List[str]) -> datetime.date:
  9.     """
  10.    Convert the date-string with the provided formats and return a date object.
  11.    If the provided date-string does not fit to any formats or if
  12.    the provided date-string has wrong values, the function raises a ValueError
  13.  
  14.    Method: https://docs.python.org/3.8/library/datetime.html#datetime.datetime.strptime
  15.    Format: https://docs.python.org/3.8/library/datetime.html#strftime-strptime-behavior
  16.    """
  17.     for dt_format in formats:
  18.         try:
  19.             """
  20.            parse the date from provided date with dt_format
  21.            and create a date object from datetime object.
  22.            """
  23.             date = datetime.datetime.strptime(date, dt_format).date()
  24.         except ValueError:
  25.             """
  26.            ValueError occours if the provided format was wrong or
  27.            if the values itself are not valid.
  28.            """
  29.             pass
  30.         else:
  31.             """
  32.            If no error happens, we know that the format fits and the values
  33.            are in range. Return the created date object
  34.            """
  35.             return date
  36.     """
  37.    If the for loop has been finished, we know that all iterations
  38.    throw an ValueError. So this date is wrong or
  39.    the provided formats are wrong or the date itself was invalid.
  40.    """
  41.     raise ValueError(f'Invalid date: {date}')
  42.  
  43. # the second format is isoformat (iso8601)
  44. # date and datetime do have the method fromisoformat()
  45. # and isoformat()
  46. date_formats: Tuple[str] = ('%d/%m/%Y', '%Y-%m-%d')
  47.  
  48. dates = '30/02/2000, 29/02/2000, 30/02/2100, 29/02/2100, 31/02/2004, 30/02/2004, 29/02/2004, 18/03/2014, 24/08/2011, 06/10/2019, 23/09/2015, 11/01/2019, 12/09/2013, 14/03/2012, 07/03/2015, 26/12/2014, 28/07/2010, 11/07/2013, 22/11/2011, 30/10/2015, 28/12/2016, 25/10/2016, 31/12/2016, 29/01/2019, 20/03/2011, 26/11/2019, 24/08/2015'
  49.  
  50. for date in map(str.strip, dates.split(',')):
  51.     try:
  52.         date_obj: datetime.date = convert_date(date, date_formats)
  53.     except ValueError as error:
  54.         print(error)
  55.     else:
  56.         print(date_obj)
  57.  
  58.  
  59.  
  60.  
  61. #  >> Invalid date: 30/02/2000      
  62. #  >> 2000-02-29                    
  63. #  >> Invalid date: 30/02/2100      
  64. #  >> Invalid date: 29/02/2100      
  65. #  >> Invalid date: 31/02/2004      
  66. #  >> Invalid date: 30/02/2004      
  67. #  >> 2004-02-29                    
  68. #  >> 2014-03-18                    
  69. #  >> 2011-08-24                    
  70. #  >> 2019-10-06                    
  71. #  >> 2015-09-23                    
  72. #  >> 2019-01-11                    
  73. #  >> 2013-09-12                    
  74. #  >> 2012-03-14                    
  75. #  >> 2015-03-07                    
  76. #  >> 2014-12-26                    
  77. #  >> 2010-07-28                    
  78. #  >> 2013-07-11                    
  79. #  >> 2011-11-22                    
  80. #  >> 2015-10-30                    
  81. #  >> 2016-12-28                    
  82. #  >> 2016-10-25                    
  83. #  >> 2016-12-31                    
  84. #  >> 2019-01-29                    
  85. #  >> 2011-03-20                    
  86. #  >> 2019-11-26                    
  87. #  >> 2015-08-24
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement