# Path: typepy/_common.py
# def strip_ansi_escape(value: Any) -> str:
#     return ansi_escape.sub("", value)
# 
# Path: typepy/_const.py
# class DefaultValue:
#     FLOAT_TYPE = Decimal
#     STRIP_ANSI_ESCAPE = True
# 
# class ParamKey:
#     STRIP_ANSI_ESCAPE = "strip_ansi_escape"
#     TIMEZONE = "timezone"
# 
# Path: typepy/error.py
# class TypeConversionError(TypeError):
#     """
#     Exception raised when failed to convert data.
#     """
# 
# Path: typepy/converter/_interface.py
# class AbstractValueConverter(ValueConverterInterface):
#     __slots__ = "_value"
# 
#     def __init__(self, value, params):
#         self._value = value
#         self._params = params
# 
#     def __repr__(self):
#         try:
#             string = str(self.force_convert())
#         except TypeConversionError:
#             string = "[ValueConverter ERROR] failed to force_convert"
# 
#         return string


from datetime import date, datetime
from .._common import strip_ansi_escape
from .._const import DefaultValue, ParamKey
from ..error import TypeConversionError
from ._interface import AbstractValueConverter
        from ..type._integer import Integer
        from ..type._realnumber import RealNumber
        from packaging.version import InvalidVersion, Version
        import dateutil.parser
        import pytz
        if timestamp:
            try:
                self.__datetime = datetime.fromtimestamp(timestamp, self.__timezone)
            except (ValueError, OSError, OverflowError):
                raise conv_error

            return self.__datetime

        timestamp = RealNumber(self._value, strict_level=1).try_convert()
        if timestamp:
            try:
                self.__datetime = datetime.fromtimestamp(int(timestamp), self.__timezone).replace(
                    microsecond=int((timestamp - int(timestamp)) * 1000000)
                )
            except (ValueError, OSError, OverflowError):
                raise conv_error

            return self.__datetime

        return None

    def __from_datetime_string(self):

        self.__validate_datetime_string()

        try:
            self.__datetime = dateutil.parser.parse(self._value)
        except (AttributeError, ValueError, OverflowError):
            if self._params.get(ParamKey.STRIP_ANSI_ESCAPE, DefaultValue.STRIP_ANSI_ESCAPE):
                try:

# Path: typepy/_common.py
# def strip_ansi_escape(value: Any) -> str:
#     return ansi_escape.sub("", value)
# 
# Path: typepy/_const.py
# class DefaultValue:
#     FLOAT_TYPE = Decimal
#     STRIP_ANSI_ESCAPE = True
# 
# class ParamKey:
#     STRIP_ANSI_ESCAPE = "strip_ansi_escape"
#     TIMEZONE = "timezone"
# 
# Path: typepy/error.py
# class TypeConversionError(TypeError):
#     """
#     Exception raised when failed to convert data.
#     """
# 
# Path: typepy/converter/_interface.py
# class AbstractValueConverter(ValueConverterInterface):
#     __slots__ = "_value"
# 
#     def __init__(self, value, params):
#         self._value = value
#         self._params = params
# 
#     def __repr__(self):
#         try:
#             string = str(self.force_convert())
#         except TypeConversionError:
#             string = "[ValueConverter ERROR] failed to force_convert"
# 
#         return string


# Path: typepy/converter/_datetime.py
from datetime import date, datetime
from .._common import strip_ansi_escape
from .._const import DefaultValue, ParamKey
from ..error import TypeConversionError
from ._interface import AbstractValueConverter
        from ..type._integer import Integer
        from ..type._realnumber import RealNumber
        from packaging.version import InvalidVersion, Version
        import dateutil.parser
        import pytz

        if timestamp:
            try:
                self.__datetime = datetime.fromtimestamp(timestamp, self.__timezone)
            except (ValueError, OSError, OverflowError):
                raise conv_error

            return self.__datetime

        timestamp = RealNumber(self._value, strict_level=1).try_convert()
        if timestamp:
            try:
                self.__datetime = datetime.fromtimestamp(int(timestamp), self.__timezone).replace(
                    microsecond=int((timestamp - int(timestamp)) * 1000000)
                )
            except (ValueError, OSError, OverflowError):
                raise conv_error

            return self.__datetime

        return None

    def __from_datetime_string(self):

        self.__validate_datetime_string()

        try:
            self.__datetime = dateutil.parser.parse(self._value)
        except (AttributeError, ValueError, OverflowError):
            if self._params.get(ParamKey.STRIP_ANSI_ESCAPE, DefaultValue.STRIP_ANSI_ESCAPE):
                try:

                    self.__datetime = dateutil.parser.parse(strip_ansi_escape(self._value))