wxWidgets / Phoenix

wxPython's Project Phoenix. A new implementation of wxPython, better, stronger, faster than he was before.
http://wxpython.org/
2.21k stars 509 forks source link

wx.adv.CalendarCtrl --> .GetDate().GetValue() , Int to long #2513

Closed moppi79 closed 4 months ago

moppi79 commented 4 months ago

Operating system: Windows 10 wxPython version & source: Pypi Python version & source: 4.2.1

Description of the problem:

The Orginal Value from .GetDate().GetValue() is to long, it has milli-Seconds on Python sends OS error

Here is Example:

print (self.calendar_ctrl_1.GetDate().GetValue())

[...]1704927600000

print (datetime.datetime.fromtimestamp(self.calendar_ctrl_1.GetDate().GetValue()).strftime('%Y,%m,%d'))

[...]print (datetime.datetime.fromtimestamp(self.calendar_ctrl_1.GetDate().GetValue()).strftime('%Y,%m,%d')) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [...]OSError: [Errno 22] Invalid argument

When i short the int it is working:

self.day_stamp = int(str(self.calendar_ctrl_1.GetDate().GetValue())[0:10])
print (self.day_stamp)

[...] 1704927600

print (datetime.datetime.fromtimestamp(self.day_stamp).strftime('%Y,%m,%d'))

[...]2024,01,11

Greetings

DietmarSchwertberger commented 4 months ago

GetValue() behaves as intended: "Directly returns the internal representation of wxDateTime object as the number of milliseconds (positive or negative) since the Unix/C epoch."

You can convert from milliseconds to second via x.GetData().GetValue()/1000.0 You can also use GetTicks() to get seconds: https://docs.wxwidgets.org/3.2/classwx_date_time.html#a99263946a9a2ece83421411081c02378 Be aware that the valid range of ticks is much less, though. Usually you can't rely on ticks as they are only valid between 1970 and 2038. If your date control allows values outside, use wxDateTime or Python datetime.

swt2c commented 4 months ago

Thanks, Dietmar. Yes, this is working as designed as noted by Dietmar.

moppi79 commented 3 months ago

Thx for the answer