geomagpy / magpy

MagPy (or GeomagPy) is a Python package for analysing and displaying geomagnetic data.
BSD 3-Clause "New" or "Revised" License
45 stars 27 forks source link

Wrongly converting datetime.datetime to wx.DateTime #111

Closed stephanbracke closed 2 years ago

stephanbracke commented 2 years ago

While working with the fit dialog box we noticed strange behaviour on windows. The problem however is not only with windows but is hidden due to the way exception handling was applied. While converting a datetime.datetime towards wx.DateTime the method wx.DateTime.FromDMY was used as seen in these code snippet

https://github.com/geomagpy/magpy/blob/71c46c9d5debf30681e1896b53d9ddd94650eb09/magpy/gui/dialogclasses.py#L2084-L2087

However in this method the month is zero based. So when we used data from january this could cause an error because 31 januari was converted 31 february ( the same was for december ) Once it got an error it goes into the exception handling https://github.com/geomagpy/magpy/blob/71c46c9d5debf30681e1896b53d9ddd94650eb09/magpy/gui/dialogclasses.py#L2087-L2093

For some reason this worked fine for Linux but not always in windows. The solution is simple just deduct 1 from the month

stfit =   wx.DateTime.FromDMY(day=self.mintime.day,month=self.mintime.month-1,year=self.mintime.year)
etfit = wx.DateTime.FromDMY(day=self.maxtime.day,month=self.maxtime.month-1,year=self.maxtime.year)

The part in the except can be removed because it doesn't always work on windows

It is best to verify all convertions done if there are other places where it needs to be corrected for the moment I only saw these https://github.com/geomagpy/magpy/blob/71c46c9d5debf30681e1896b53d9ddd94650eb09/magpy/gui/dialogclasses.py#L2396-L2403

where it is correctly done with the -1 for the month

leonro commented 2 years ago

All references to DateTimeFromTimeT(...mktime...) where replaced using DateTime.fromDMY. Solved with a9352b4d65f00c8be2cb196c4512934e72e9f8c0 and 429e3425fe1ad4b77830a4f5cad2b34172b02418