diff options
| author | Bobby <[email protected]> | 2022-09-10 20:55:18 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-09-10 20:55:18 -0400 |
| commit | abf8d97800626e059fc8ae828bcb8b176eec0f5f (patch) | |
| tree | dd9fc3f72c42a45ee5f271088cf772358ab91a45 | |
| parent | 7b8661bd9ee0c7e88a68a3f8a5391f73a32138fa (diff) | |
| download | edify-abf8d97800626e059fc8ae828bcb8b176eec0f5f.tar.xz edify-abf8d97800626e059fc8ae828bcb8b176eec0f5f.zip | |
Added date pattern
| -rw-r--r-- | docs/built-in/index.rst | 42 | ||||
| -rw-r--r-- | src/edify/library/__init__.py | 1 | ||||
| -rw-r--r-- | src/edify/library/date.py | 34 | ||||
| -rw-r--r-- | tests/test_date.py | 56 |
4 files changed, 133 insertions, 0 deletions
diff --git a/docs/built-in/index.rst b/docs/built-in/index.rst index c9407ec..c14923b 100644 --- a/docs/built-in/index.rst +++ b/docs/built-in/index.rst @@ -91,4 +91,46 @@ You can use the ``ipv6`` function as follows: ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334') # returns True ipv6('2001:0db8:85a3:0000:0000:8a2e:0370') # returns False +date() +------ + +The ``date`` function verifies that a string is a valid date. The function takes a ``string`` argument which is supposed to be a valid date. The function returns ``True`` if the string is a valid date, and ``False`` otherwise. + +.. warning:: + The ``date`` function validates the string against a date format (D/M/YYYY or M/D/YYYY). This however does not guarantee that the date would be valid. For example, the string ``31-02-2017`` is a valid date according to the date format, but it is not a valid date. + + While there are some regular expressions that allow more complex date validations, it is usually better to validate dates using special date and time libraries. For example, in Python datetime package can be used for these purposes. In this case, the validation will look like this: + + .. code-block:: python + + from datetime import datetime + + try: + datetime.strptime('31-02-2017', '%d-%m-%Y') + except ValueError: + print('Invalid date') + else: + print('Valid date') + +You can use the ``date`` function as follows: + +.. code-block:: python + + from edify.library import date + + date('31/12/2017') # returns True + date('31-12-2017') # returns False + +iso_date() +---------- + +The ISO 8061 is an international standard for exchanging and serializing date and time data. The ``iso_date`` function verifies that a string is a valid ISO date. The function takes a ``string`` argument which is supposed to be a valid ISO date. The function returns ``True`` if the string is a valid ISO date, and ``False`` otherwise. + +You can use the ``iso_date`` function as follows: + +.. code-block:: python + + from edify.library import iso_date + iso_date('2021-11-04T22:32:47.142354-10:00') # returns True + iso_date('12/12/2022') # returns False diff --git a/src/edify/library/__init__.py b/src/edify/library/__init__.py index 6bf12a5..64c94e7 100644 --- a/src/edify/library/__init__.py +++ b/src/edify/library/__init__.py @@ -6,3 +6,4 @@ from .ip import ipv6 from .mail import email from .mail import email_rfc_5322 from .phone import phone_number +from .date import date, iso_date diff --git a/src/edify/library/date.py b/src/edify/library/date.py new file mode 100644 index 0000000..94b536b --- /dev/null +++ b/src/edify/library/date.py @@ -0,0 +1,34 @@ +import re + +date_pattern = "^[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}$" +iso_date_pattern = "^(?:\\d{4})-(?:\\d{2})-(?:\\d{2})T(?:\\d{2}):(?:\\d{2}):(?:\\d{2}(?:\\.\\d*)?)(?:(?:-(?:\\d{2}):(?:\\d{2})|Z)?)$" # noqa + + +def date(date: str) -> bool: + """Checks if a string is a valid date. + + Args: + date (str): The string to check. + Returns: + bool: True if the string is a valid date, False otherwise. + """ + + if re.match(date_pattern, date): + return True + else: + return False + + +def iso_date(date: str) -> bool: + """Checks if a string is a valid ISO date. + + Args: + date (str): The string to check. + Returns: + bool: True if the string is a valid ISO date, False otherwise. + """ + + if re.match(iso_date_pattern, date): + return True + else: + return False diff --git a/tests/test_date.py b/tests/test_date.py new file mode 100644 index 0000000..dd5dd22 --- /dev/null +++ b/tests/test_date.py @@ -0,0 +1,56 @@ +from edify.library import date, iso_date + + +def test_date(): + dates = { + "1/1/2020": True, + "01/01/2020": True, + "1/01/2020": True, + "01/1/2020": True, + "1/1/20": False, + "01/01/20": False, + "1/1/202": False, + "01/01/202": False, + "12/12/2022": True, + "12/12/2": False, + "2021-11-04T22:32:47.142354-10:00": False, + "2021-11-04T22:32:47.142354Z": False, + "2021-11-04T22:32:47.142354": False, + "2021-11-04T22:32:47": False, + "2021-11-04T22:32": False, + "2021-11-04T22": False, + "2021-11-04": False, + "2021-11": False, + "2021": False, + "1-1-2020": False + } + + for date_string, expectation in dates.items(): + assert date(date_string) == expectation + + +def test_iso_date(): + dates = { + "1/1/2020": False, + "01/01/2020": False, + "1/01/2020": False, + "01/1/2020": False, + "1/1/20": False, + "01/01/20": False, + "1/1/202": False, + "01/01/202": False, + "12/12/2022": False, + "12/12/2": False, + "2021-11-04T22:32:47.142354-10:00": True, + "2021-11-04T22:32:47.142354Z": True, + "2021-11-04T22:32:47.142354": True, + "2021-11-04T22:32:47": True, + "2021-11-04T22:32": False, + "2021-11-04T22": False, + "2021-11-04": False, + "2021-11": False, + "2021": False, + } + + for date_string, expectation in dates.items(): + assert iso_date(date_string) == expectation |
