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, timezone
 8import re
 9
10
11def build_timestamp_str() -> str:
12    """
13    Build a timestamp string in the format yyyy-mm-dd__HH_MM_SS__[+/-HH:MM]."""
14    timestamp = get_now_for_system_timezone()
15    timestamp_str: str = timestamp.strftime("%Y-%m-%d__%H_%M_%S")
16    # Get offset with colon (e.g. +02:00)
17    offset = timestamp.strftime("%z")  # gives +0200
18    if offset:
19        offset = offset[:3] + ":" + offset[3:]  # insert colon
20        timestamp_str += f"__[{offset}]"
21    return timestamp_str
22
23
24def parse_yyyy_mm_dd(arg_date: str) -> datetime:
25    """
26    Parse a date string in the yyyy-mm-dd format to a datetime object.
27
28    Args:
29    arg_date (str): Date string in the format yyyy-mm-dd.
30
31    Returns:
32    datetime: Datetime object representing the parsed date.
33    """
34    return datetime.strptime(arg_date, "%Y-%m-%d")
35
36
37def _date_to_yyyy_mm_dd(arg_date: datetime) -> str:
38    """
39    Format a datetime object to a date string in the yyyy-mm-dd format.
40
41    Args:
42    arg_date (datetime): Datetime object to format.
43
44    Returns:
45    str: Date string in the format yyyy-mm-dd.
46    """
47    return arg_date.strftime("%Y-%m-%d")
48
49
50def add_day_to_date(str_date: str, days: int) -> str:
51    """
52    Add a specified number of days to a date string in the yyyy-mm-dd format.
53
54    Args:
55    str_date (str): Date string in the format yyyy-mm-dd.
56    days (int): Number of days to add to the date.
57
58    Returns:
59    str: Date string in the format yyyy-mm-dd after adding the specified days.
60    """
61    date_value = parse_yyyy_mm_dd(str_date)
62    date_value += timedelta(days)
63    return _date_to_yyyy_mm_dd(date_value)
64
65
66def is_valid_date_yyyy_mm_dd(value: str) -> bool:
67    """
68    Check if a date string is in the valid yyyy-mm-dd format.
69
70    Args:
71    value (str): Date string to validate.
72
73    Returns:
74    bool: True if the date string is in the correct format, False otherwise.
75    """
76    pat = re.compile(r"[0-9]{4}-[0-9]{2}-[0-9]{2}")
77    return True if re.fullmatch(pat, value) else False
78
79
80def get_now_for_system_timezone() -> datetime:
81    """
82    Get the current date and time, for the system timezone.
83
84    Returns:
85    datetime: Date and time for the system timezone.
86    """
87    return datetime.now(timezone.utc).astimezone()
def build_timestamp_str() -> str:
12def build_timestamp_str() -> str:
13    """
14    Build a timestamp string in the format yyyy-mm-dd__HH_MM_SS__[+/-HH:MM]."""
15    timestamp = get_now_for_system_timezone()
16    timestamp_str: str = timestamp.strftime("%Y-%m-%d__%H_%M_%S")
17    # Get offset with colon (e.g. +02:00)
18    offset = timestamp.strftime("%z")  # gives +0200
19    if offset:
20        offset = offset[:3] + ":" + offset[3:]  # insert colon
21        timestamp_str += f"__[{offset}]"
22    return timestamp_str

Build a timestamp string in the format yyyy-mm-dd__HH_MM_SS__[+/-HH:MM].

def parse_yyyy_mm_dd(arg_date: str) -> datetime.datetime:
25def parse_yyyy_mm_dd(arg_date: str) -> datetime:
26    """
27    Parse a date string in the yyyy-mm-dd format to a datetime object.
28
29    Args:
30    arg_date (str): Date string in the format yyyy-mm-dd.
31
32    Returns:
33    datetime: Datetime object representing the parsed date.
34    """
35    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: str, days: int) -> str:
51def add_day_to_date(str_date: str, days: int) -> str:
52    """
53    Add a specified number of days to a date string in the yyyy-mm-dd format.
54
55    Args:
56    str_date (str): Date string in the format yyyy-mm-dd.
57    days (int): Number of days to add to the date.
58
59    Returns:
60    str: Date string in the format yyyy-mm-dd after adding the specified days.
61    """
62    date_value = parse_yyyy_mm_dd(str_date)
63    date_value += timedelta(days)
64    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: str) -> bool:
67def is_valid_date_yyyy_mm_dd(value: str) -> bool:
68    """
69    Check if a date string is in the valid yyyy-mm-dd format.
70
71    Args:
72    value (str): Date string to validate.
73
74    Returns:
75    bool: True if the date string is in the correct format, False otherwise.
76    """
77    pat = re.compile(r"[0-9]{4}-[0-9]{2}-[0-9]{2}")
78    return True if re.fullmatch(pat, value) else False

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.

def get_now_for_system_timezone() -> datetime.datetime:
81def get_now_for_system_timezone() -> datetime:
82    """
83    Get the current date and time, for the system timezone.
84
85    Returns:
86    datetime: Date and time for the system timezone.
87    """
88    return datetime.now(timezone.utc).astimezone()

Get the current date and time, for the system timezone.

Returns: datetime: Date and time for the system timezone.