fix localtime_to_timestamp tool throws 'no attribute localize error' when it executes without specifying a timezone parameter (#23517)
This commit is contained in:
@@ -37,11 +37,11 @@ class LocaltimeToTimestampTool(BuiltinTool):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def localtime_to_timestamp(localtime: str, time_format: str, local_tz=None) -> int | None:
|
def localtime_to_timestamp(localtime: str, time_format: str, local_tz=None) -> int | None:
|
||||||
try:
|
try:
|
||||||
if local_tz is None:
|
|
||||||
local_tz = datetime.now().astimezone().tzinfo
|
|
||||||
if isinstance(local_tz, str):
|
|
||||||
local_tz = pytz.timezone(local_tz)
|
|
||||||
local_time = datetime.strptime(localtime, time_format)
|
local_time = datetime.strptime(localtime, time_format)
|
||||||
|
if local_tz is None:
|
||||||
|
localtime = local_time.astimezone() # type: ignore
|
||||||
|
elif isinstance(local_tz, str):
|
||||||
|
local_tz = pytz.timezone(local_tz)
|
||||||
localtime = local_tz.localize(local_time) # type: ignore
|
localtime = local_tz.localize(local_time) # type: ignore
|
||||||
timestamp = int(localtime.timestamp()) # type: ignore
|
timestamp = int(localtime.timestamp()) # type: ignore
|
||||||
return timestamp
|
return timestamp
|
||||||
|
Reference in New Issue
Block a user