add excel
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
from io import BytesIO
|
||||
|
||||
import jdatetime
|
||||
@@ -324,4 +325,56 @@ def add_chart(
|
||||
# chart_title="نمودار تغییرات وزن در سردخانهها",
|
||||
# x_axis_title="سردخانهها",
|
||||
# y_axis_title="وزن (کیلوگرم)"
|
||||
# )
|
||||
# )
|
||||
|
||||
|
||||
def convert_str_to_date(string, with_datetime=None):
|
||||
"""
|
||||
Convert a string to a datetime.date object.
|
||||
|
||||
This function tries multiple common date formats, including ISO 8601 with or
|
||||
without milliseconds, and plain 'YYYY-MM-DD'. If the string cannot be parsed,
|
||||
it returns None.
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
string : str
|
||||
The date string to convert.
|
||||
|
||||
Returns:
|
||||
--------
|
||||
datetime.date or None
|
||||
A datetime.date object if conversion succeeds, otherwise None.
|
||||
|
||||
Supported formats:
|
||||
------------------
|
||||
- 'YYYY-MM-DDTHH:MM:SS.sssZ' (ISO 8601 with milliseconds)
|
||||
- 'YYYY-MM-DDTHH:MM:SSZ' (ISO 8601 without milliseconds)
|
||||
- 'YYYY-MM-DD' (Simple date)
|
||||
"""
|
||||
string = str(string).strip()
|
||||
|
||||
# فرمتهای مختلف تاریخ
|
||||
date_formats = [
|
||||
'%Y-%m-%dT%H:%M:%S.%fZ',
|
||||
'%Y-%m-%dT%H:%M:%SZ',
|
||||
'%Y-%m-%dT%H:%M:%S.%f%z', # ✅ با میلیثانیه و تایمزون
|
||||
'%Y-%m-%dT%H:%M:%S%z', # ✅ مثل: 2025-02-26T03:30:00+03:30
|
||||
'%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 date_formats:
|
||||
try:
|
||||
if with_datetime:
|
||||
date = datetime.strptime(string, fmt)
|
||||
else:
|
||||
date = datetime.strptime(string, fmt).date()
|
||||
return date
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
return None
|
||||
Reference in New Issue
Block a user