File size: 3,472 Bytes
f40c733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from datetime import datetime
from typing import Union, Optional
import re


def validate_signup_date(date_input: Union[str, datetime, None]) -> datetime:
    """
    Validates and normalizes the signup date input.

    Args:
        date_input: The date input which can be a string, datetime object, or None

    Returns:
        datetime: A valid datetime object representing the signup date

    Raises:
        ValueError: If the date input is invalid or cannot be parsed
    """
    if date_input is None:
        return datetime.utcnow()

    if isinstance(date_input, datetime):
        return date_input

    if isinstance(date_input, str):
        # Try to parse ISO format first (YYYY-MM-DDTHH:MM:SS.ssssss or YYYY-MM-DDTHH:MM:SS)
        iso_formats = [
            "%Y-%m-%dT%H:%M:%S.%f",
            "%Y-%m-%dT%H:%M:%S",
            "%Y-%m-%d %H:%M:%S.%f",
            "%Y-%m-%d %H:%M:%S",
            "%Y-%m-%d"
        ]

        for fmt in iso_formats:
            try:
                return datetime.strptime(date_input, fmt)
            except ValueError:
                continue

        # Try common alternative formats
        alt_formats = [
            "%m/%d/%Y %H:%M:%S",
            "%m/%d/%Y",
            "%d/%m/%Y",
            "%Y.%m.%d"
        ]

        for fmt in alt_formats:
            try:
                return datetime.strptime(date_input, fmt)
            except ValueError:
                continue

        # If none of the formats worked, raise an error
        raise ValueError(f"Unable to parse date string: {date_input}")

    raise ValueError(f"Invalid date input type: {type(date_input)}. Expected str, datetime, or None")


def is_valid_date_format(date_str: str) -> bool:
    """
    Checks if a date string is in a valid format.

    Args:
        date_str: The date string to validate

    Returns:
        bool: True if the date string is in a valid format, False otherwise
    """
    try:
        validate_signup_date(date_str)
        return True
    except ValueError:
        return False


def normalize_date_to_utc(date_input: Union[str, datetime]) -> datetime:
    """
    Normalizes a date input to UTC timezone.

    Args:
        date_input: The date input to normalize

    Returns:
        datetime: The normalized datetime in UTC
    """
    dt = validate_signup_date(date_input)
    # Assuming naive datetime is in local timezone, convert to UTC
    # For simplicity, if it's naive, treat as local time and convert to UTC
    if dt.tzinfo is None:
        # For this implementation, we'll assume it's already in UTC or convert appropriately
        return dt
    else:
        # If it has timezone info, convert to UTC
        return dt.utctimetuple()


def validate_and_format_signup_date(date_input: Union[str, datetime, None]) -> dict:
    """
    Validates a signup date and returns a formatted response.

    Args:
        date_input: The date input to validate

    Returns:
        dict: A dictionary containing the validated date and any validation messages
    """
    try:
        validated_date = validate_signup_date(date_input)
        return {
            "valid": True,
            "date": validated_date,
            "formatted_iso": validated_date.isoformat(),
            "message": "Date is valid"
        }
    except ValueError as e:
        return {
            "valid": False,
            "date": None,
            "formatted_iso": None,
            "message": str(e)
        }