Skip to content

Datetime cleanup (mostly for Python 3) #161

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

Closed
wants to merge 12 commits into from

Conversation

mwiebe
Copy link
Member

@mwiebe mwiebe commented Sep 18, 2011

This set of patches fixes up the datetime functionality to pass all tests in Python 3.1 and 3.2.

Done by making the unit given by datetime_data be a string instead of
bytes in Python 3.
This includes warnings about shadowed variables, some bad casts,
and others. Fixing these warnings also fixed a memory leak in one instance.
@charris
Copy link
Member

charris commented Sep 18, 2011

Looks good to me.

@mwiebe
Copy link
Member Author

mwiebe commented Sep 18, 2011

Thanks for the review!

/* Create the output string data type with a big enough length */
/* Get a string size long enough for any datetimes we're given */
strsize = get_datetime_iso_8601_strlen(local, unit);
#if defined(NPY_PY3K)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to factor this out, to avoid defines inside function bodies ? I.e.

#ifdef NPY_PY3K
void foo()
{
// some code here
}
#endif

rather than

some_large_function(...)
{
....
#ifdef NPY_PY3K
// some code here
#endif
}

I know this is not strictly followed in numpy codebase, but it would be nice to avoid too much of this IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've thought a bit about how to do this, but splitting this part out feels very artificial and doesn't seem like it would improve the clarity of the code at all. I'd rather keep it inline as it is. It's an artifact of the design choice to make NPY_STRING arrays not return strings in Python 3, something that's probably too late to change.

@87
Copy link
Contributor

87 commented Sep 19, 2011

I noticed an interesting feature in the np.datetime64 construction (on Linux):
It cannot parse datetimes beyond 2038,

>>> np.datetime64('2038-01-20T11:52')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: Failed to use mktime to convert local time to UTC

but specifying a timezone makes it loop around:

>>> np.datetime64('2038-01-20T11:52+0100')
numpy.datetime64('1901-12-15T04:43+0019')

@mwiebe
Copy link
Member Author

mwiebe commented Sep 30, 2011

The Y2038 bug seems to be restricted to 32-bit Linux as far as I can tell. On Windows, there is datetime64_t, which I believe all your code changes still use, correct? In searching, it appears as if glibc didn't follow Microsoft in adding 64-bit variants of these important functions, and reading comments on forums reveals some perspectives like "all computers will be 64-bit before 2038 rolls around, so you shouldn't have to worry about it."

The hack I've thought of is to shift years later than 2038 to before 2038, do the OS-specific stuff, then add the year offset back in once it's in NumPy's hands again.

@mwiebe
Copy link
Member Author

mwiebe commented Sep 30, 2011

Looking at your specific example where you specify the timezone, I wouldn't expect that to fail, since it shouldn't be passing through a time_t in that case. Does it fail with the "Z" time zone as well?

edit: The fail point would be converting to a local time for printing, so hopefully this works with the Y2038 hack I've added.

This only seems to be cropping up on 32-bit Linux, where time_t is 32 bits,
and there appears to be no 64-bit time_t extension like on Windows.
Also incorporated part of Han Genuit's pull request numpy#23, to try
and reduce the difficulty dealing with the merge conflict that
will result from these concurrent changes
@mwiebe
Copy link
Member Author

mwiebe commented Sep 30, 2011

What do people think is a reasonable way to deal with the issue on Windows, where the standard library doesn't want to apply timezones to dates before 1970? Should the 'Z' timezone be always used for dates prior to 1970 on that platform, or on all platforms?

…nd later, because of Win32

On MS Windows, dates earlier than 1970 cause mktime to error. This patch
also applies to parsing the same Y2038 hack that was done for printing.
@mwiebe
Copy link
Member Author

mwiebe commented Sep 30, 2011

I've restricted local parsing to dates >= 1970 as I suggested, and applied the Y2038 hack I proposed to both printing and parsing. I don't have platforms to test this on, so will need help with that!

@87
Copy link
Contributor

87 commented Oct 1, 2011

Thanks for the fixes! It tests OK on Linux (32bit). (Except for an unrelated polyfit issue.)

Hopefully, the 2038 hack wont give problems with calendar conversions, but it might be the best solution on 32bit for now..

@charris
Copy link
Member

charris commented Oct 1, 2011

I've merged this in aded70c..10ed90c.

@charris charris closed this Oct 1, 2011
luyahan pushed a commit to plctlab/numpy that referenced this pull request Apr 25, 2024
feat: Add vabaq_[s8|s16|s32|u8|u16|u32]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants