israel-dryer/ttkbootstrap

Datetime error when use ttkbootstrap

djesgit opened this issue · 2 comments

Desktop (please complete the following information):

Windows 10 and Windows 11

Package Version


Pillow 9.1.1
pip 23.1.2
setuptools 65.5.0
ttkbootstrap 1.10.1

Describe the bug

I get the next error:

=========================================================
Exception has occurred: ValueError
time data '4/14/19801:01PM' does not match format '%m/%d/%Y%I:%M%p'
File "C:\Users\Diego\OneDrive\Escritorio\test\test.py", line 6, in
my_date=datetime.strptime(my_date,'%m/%d/%Y%I:%M%p')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: time data '4/14/19801:01PM' does not match format '%m/%d/%Y%I:%M%p'

Explanation:
The error occurs when I add %p in datetime.strptime function, but error occurs only when I used ttkbootstrap.
If you remove the line " from ttkbootstrap import * ", then there is no error.

To Reproduce

from ttkbootstrap import *
from datetime import datetime

my_date='4/14/19801:01PM'

my_date=datetime.strptime(my_date,'%m/%d/%Y%I:%M%p')
print(my_date)

Expected behavior

I expect get correct datetime object from given string usin %p in format in datetime.strptime function.

Screenshots

image

https://drive.google.com/file/d/1Hof7R9eJTJArHn9GsCDqPoQg9CML3cdh/view?usp=sharing

Additional context

The problem occurs only using ttkbootstrap.
I've tested in Windows 10 and Windows 11.

Thank you for this amazing library.

This is probably not an issue with ttkbootstrap instead its caused due to a mistake in the datetime code:

my_date = '4/14/1980 1:01PM'  # Add a space between the year and time
my_date = datetime.strptime(my_date, '%m/%d/%Y %I:%M%p')

Problem:
In my view, the issue is not caused by the Python 'datetime' module; it arises from line 566 in the 'dialogs.py' file within the ttkbootstrap library. This specific line sets the locale to the system's default, causing all date formatting to adhere to the operating system's standard.

Possible solution:
To work around this problem, you should, before executing the line with the 'strptime' function, set the locale to the POSIX standard. After formatting the date using 'strptime,' you can then return to the system's default locale to ensure it doesn't affect ttkbootstrap's behavior.

Exemplo:

from ttkbootstrap import *
from datetime import datetime as dt
import locale


def set_locale_to_posix():
    locale.setlocale(locale.LC_ALL, "C")


def set_locale_to_system_default():
    # This part was taken from line 566 of the "dialogs.py" file present in the "ttkbootstrap" library.
    locale.setlocale(locale.LC_ALL, locale.setlocale(locale.LC_TIME, ""))


my_date = '4/14/19801:01PM'

set_locale_to_posix()
my_date = dt.strptime(my_date, "%m/%d/%Y%I:%M%p")
set_locale_to_system_default()

print(my_date) # Return "1980-04-14 13:01:00"

I hope this helps resolve the issue 😄.