cornsnake.util_date

Functions for date manipulation. It includes functions to parse, format, add days to, and validate dates in the yyyy-mm-dd format.

Documentation

 1"""
 2Functions for date manipulation. It includes functions to parse, format, add days to, and validate dates in the yyyy-mm-dd format.
 3
 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_date.html)
 5"""
 6
 7from datetime import datetime, timedelta
 8import re
 9
10
11def parse_yyyy_mm_dd(arg_date):
12    """
13    Parse a date string in the yyyy-mm-dd format to a datetime object.
14
15    Args:
16    arg_date (str): Date string in the format yyyy-mm-dd.
17
18    Returns:
19    datetime: Datetime object representing the parsed date.
20    """
21    return datetime.strptime(arg_date, "%Y-%m-%d")
22
23
24def _date_to_yyyy_mm_dd(arg_date):
25    """
26    Format a datetime object to a date string in the yyyy-mm-dd format.
27
28    Args:
29    arg_date (datetime): Datetime object to format.
30
31    Returns:
32    str: Date string in the format yyyy-mm-dd.
33    """
34    return arg_date.strftime("%Y-%m-%d")
35
36
37def add_day_to_date(str_date, days):
38    """
39    Add a specified number of days to a date string in the yyyy-mm-dd format.
40
41    Args:
42    str_date (str): Date string in the format yyyy-mm-dd.
43    days (int): Number of days to add to the date.
44
45    Returns:
46    str: Date string in the format yyyy-mm-dd after adding the specified days.
47    """
48    date_value = parse_yyyy_mm_dd(str_date)
49    date_value += timedelta(days)
50    return _date_to_yyyy_mm_dd(date_value)
51
52
53def is_valid_date_yyyy_mm_dd(value):
54    """
55    Check if a date string is in the valid yyyy-mm-dd format.
56
57    Args:
58    value (str): Date string to validate.
59
60    Returns:
61    bool: True if the date string is in the correct format, False otherwise.
62    """
63    pat = re.compile(r"[0-9]{4}-[0-9]{2}-[0-9]{2}")
64    return re.fullmatch(pat, value)
def parse_yyyy_mm_dd(arg_date):
12def parse_yyyy_mm_dd(arg_date):
13    """
14    Parse a date string in the yyyy-mm-dd format to a datetime object.
15
16    Args:
17    arg_date (str): Date string in the format yyyy-mm-dd.
18
19    Returns:
20    datetime: Datetime object representing the parsed date.
21    """
22    return datetime.strptime(arg_date, "%Y-%m-%d")

Parse a date string in the yyyy-mm-dd format to a datetime object.

Args: arg_date (str): Date string in the format yyyy-mm-dd.

Returns: datetime: Datetime object representing the parsed date.

def add_day_to_date(str_date, days):
38def add_day_to_date(str_date, days):
39    """
40    Add a specified number of days to a date string in the yyyy-mm-dd format.
41
42    Args:
43    str_date (str): Date string in the format yyyy-mm-dd.
44    days (int): Number of days to add to the date.
45
46    Returns:
47    str: Date string in the format yyyy-mm-dd after adding the specified days.
48    """
49    date_value = parse_yyyy_mm_dd(str_date)
50    date_value += timedelta(days)
51    return _date_to_yyyy_mm_dd(date_value)

Add a specified number of days to a date string in the yyyy-mm-dd format.

Args: str_date (str): Date string in the format yyyy-mm-dd. days (int): Number of days to add to the date.

Returns: str: Date string in the format yyyy-mm-dd after adding the specified days.

def is_valid_date_yyyy_mm_dd(value):
54def is_valid_date_yyyy_mm_dd(value):
55    """
56    Check if a date string is in the valid yyyy-mm-dd format.
57
58    Args:
59    value (str): Date string to validate.
60
61    Returns:
62    bool: True if the date string is in the correct format, False otherwise.
63    """
64    pat = re.compile(r"[0-9]{4}-[0-9]{2}-[0-9]{2}")
65    return re.fullmatch(pat, value)

Check if a date string is in the valid yyyy-mm-dd format.

Args: value (str): Date string to validate.

Returns: bool: True if the date string is in the correct format, False otherwise.