@@ -824,24 +824,20 @@ def format_is_iso(f: str) -> bint:
824
824
Generally of form YYYY-MM-DDTHH:MM:SS - date separator can be different
825
825
but must be consistent. Leading 0s in dates and times are optional.
826
826
"""
827
- iso_regex = \
828
- r"^\d{4}(-\d{2}(-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(([+-]\d{2}:\d{2})| Z )?)?)?)?$ "
829
-
830
- # ^ -> matches the start of the string
831
- # \d{4} -> matches the four digit(0-9) which represent the year
832
- # -\d{2} -> matches the dash followed by 2 digits
833
- # T\d{2}:\d{2}:\d{2} \
834
- # -> "T" followed by three groups seperated by colon: represent H:M: S
835
- # (\.\d+)? -> represents the fractions of seconds
836
- # (([+-]\d{2}:\d{2})| Z )? -> optional part match the time zone info
837
- # ? -> The ? at the end makes the part of regex optional
838
- # $ -> matches with the end
839
-
827
+ iso_regex = re.compile(
828
+ r "" "
829
+ ^ # start of string
830
+ \d {4} # match a 4-digit year
831
+ ( - \d {2} ) ? # optionally match a 2-digit month
832
+ ( -\d {2} ) ? # optionally match a 2-digit day
833
+ ( T\d {2} :\d {2} :\d {2} # match time in the format "THH:MM:SS"
834
+ ( \. \d + ) ? # optionally match a decimal and fractional seconds
835
+ ( [ +- ] \d {2} : \d {2} | Z ) ? ) ? # optional match timezone in the format " + HH:MM" or "Z"
836
+ $ # end of string
837
+ """ ,
838
+ re.VERBOSE,
839
+ )
840
840
excluded_formats = [" %Y%m%d " , " %Y%m " , " %Y " ]
841
-
842
- # uses the 're' module to check if the string matches the regular
843
- # expression and not in the list of 'excluded_formats' then it will return true
844
-
845
841
if re.match(iso_regex , f ) and f not in excluded_formats:
846
842
return True
847
843
return False
0 commit comments