Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datetime error when use ttkbootstrap #481

Open
djesgit opened this issue Aug 22, 2023 · 2 comments
Open

Datetime error when use ttkbootstrap #481

djesgit opened this issue Aug 22, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@djesgit
Copy link

djesgit commented Aug 22, 2023

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.

@djesgit djesgit added the bug Something isn't working label Aug 22, 2023
@cool-dev-guy
Copy link

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')

@ismaelink
Copy link

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 😄.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants