date conversion fails for dates < 1900

Issue #33 resolved
gritschel created an issue

Hi AJ,

the function datetime.datetime.strftime() that you use in savReader.spss2strDate() cannot handle dates before the year 1900. Instead of something like

return bytez(datetime.datetime.strftime(theDate, "%Y-%m-%d"))

you could probably simply use

return bytez('{0.year}-{0.month:02}-{0.day}'.format(theDate))

to format the date.

Another option would be to use the .isoformat() method of a datetime object, see http://stackoverflow.com/questions/1643967/is-there-any-way-to-use-a-strftime-like-function-for-dates-before-1900-in-python

(Can hours, minutes, seconds ever be relevant in an SPSS date???)

Cheers, Gerhard

Comments (4)

  1. Albert-Jan Roskam repo owner

    Hi Gerhard,

    That's with Python 3? I use the mx package for that particular case, however, this package is Python 2 only. I actually first used the isoformat solution, but I later changed it to mx because it's easier to change from ISO dates to something else if needed. But I am partial about mx too, I just realized too late that it's Python 2-only. Your suggestion looks interesting. Does it also handle pre-1900 datetimes? Very rare, I suppose, to also have a time part with such data.

    Best wishes, Albert-Jan

  2. gritschel reporter

    Hi AJ,

    I apologize, I haven't checked the more recent versions of the code. This was still for build 433b5a51a64e, where the method in question looks like

    def spss2strDate(self, spssDateValue, fmt, recodeSysmisTo):
        try:
            if not hasattr(self, "gregorianEpoch"):
                self.gregorianEpoch = datetime.datetime(1582, 10, 14, 0, 0, 0)
            theDate = (self.gregorianEpoch +
                       datetime.timedelta(seconds=spssDateValue))
            return bytez(datetime.datetime.strftime(theDate, fmt))
        except (OverflowError, TypeError, ValueError):
            return recodeSysmisTo
    

    (I stripped the documentation part.) You can mark this as solved. But, yes, the version I cited (with the explicit references to the attributes of the datetime object) can handle dates before 1900. This is a limitation of the .strftime() method, not of the datetime object itself, which can handle years down to year 1 (if I remember correctly).

    Cheers, Gerhard

  3. Albert-Jan Roskam repo owner

    already solved a couple of dozen commits ago. Still interesting to look for alternatives that also work with Python 3. isoformat is perhaps the most straightforward way. Or use some of the matplotlib code.

  4. priyanka o

    Hi , I have a Date in SPSS , as 17-NOV-2016

    In the Pandas DataFrame, its shown as 1.36987e+10

    By applying the conversion logic mentioned above, I get incorrect date by 1 Day. It does work correctly for other dates but for some there is a difference of 1 Day.

    import datetime
    gregorianEpoch = datetime.datetime(1582, 10, 14, 0, 0, 0)

    dt=1.36987e+10

    theDate = gregorianEpoch + datetime.timedelta(seconds=dt)
    fmt="%Y-%m-%d"
    theDate2=datetime.datetime.strftime(theDate, fmt)
    print(theDate2)

    OUT PUT : 2016-11-16

  5. Log in to comment