-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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.strftime dislikes years before 1900 #45329
Comments
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.date(1876, 2, 3).strftime('%Y-%m-%d')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: year=1876 is before 1900; the datetime strftime() methods require year >= 1900 Apparently this is due to platform-specific weirdnesses in implementations of strftime. It is still very annoying however. Perhaps a good implementation of strftime could be found and incorporated into Python itself? |
This is not a bug report, but a feature request. Python works correctly as-is. |
I have a patch for this, but I don't know which platforms have the |
Which version of Python are you using? I could have sworn we just fixed |
The relevant commit seems to be r30224 in 2002:
Of course I'm not dealing with any of this in the attached patch, but |
Ah, I remember now. It was a special case for xmlrpclib to allow |
The patch doesn't work on Python3 because Python3 changes |
See also issue bpo-3173. |
I made this patch apply on release24-maint as i need python24 for my projects. You can find the following patch there: I successfully used it on some linux26 box. |
And forgotten to say that it worked for me for py26 and py24. |
I see this in py3k branch on MacOS X: [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.date(1876, 2, 3).strftime('%Y-%m-%d')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: year=1876 is before 1900; the datetime strftime() methods require year >= 1900 I like the approach taken in the David's patch. The datetime module should stop piggybacking on the time module. |
Adding bpo-7989 as a dependency because one of the stated reasons for not calling system strftime from datetime directly is because pure python implementations cannot do the same. This of course can be resolved by exposing raw strftime in separate module (for example _time), but simply applying this patch before bpo-7989 would mean that year < 1900 would have to be disabled for pure python implementation tests. |
This patch doesnt apply anymore on py26. |
kiorky, Thank you for the updated patch, but since it implements a new feature, it cannot be applied to 2.x series. I am +1 on removing year > 1900 limitation from datetime.strftime in 3.x, but you need to consider how this can be achieved in pure python datetime implementation which is now slated to be released in 3.2. See msg110792 above. |
We must not have the same point of view about new features and bugfixes... |
kiorky: see my msg55157. Python behaves correctly as it stands - raising the exception is fully intentional. It's not a bug that it gets raised; dates before 1900 are just not supported. Adding support for them is a new feature. |
I would like to push this for 3.2. Recent discussions at bpo-10827 and on python-dev seem to favor removal of arbitrary limits on year range. |
After r87829 (see bpo-10827), the insane Y2K behavior of time module functions does not extend beyond year 999. Hopefully in Python 3.3, accept2dyear will be removed completely and datetime.strftime() will be able to support the full intersection of platform year range with that of datetime type. Meanwhile, I propose a simple patch that lowers the 1900 limit down to 1000. I was not able to find any evidence that there are platforms that have issues with formatting years between 1000 and 1899. Are there any objections to committing this patch? I will monitor the build bots if I see failures, I'll modify the test and/or add a platform-specific check to fix them. |
Here is a patch removing the arbitrary datetime.strftime() limit on year: it adds an option "accept2dyear" to time.strftime() and datetime just sets this option to False. |
On Fri, Jan 7, 2011 at 7:26 PM, STINNER Victor <report@bugs.python.org> wrote:
The idea of adding an optional argument to time.strftime() was """ |
-- ^^^ the last lines of msg125738 cut by roundup ^^^ |
strftime_1_9999.patch: replace 1900 <= year limit by 1 <= year or 1 <= year <= 9999 (if compiled with Visual Studio). With more tests on years. |
I like strftime_1_9999.patch, but it really belongs to bpo-10827. Please leave a note there once you commit and mention the issue in commit log. |
r87845+r87846 sets time.strftime() minimum year to 1 instead of 1900. If Python is compiled with Visual Studio, it sets also the maximum year to 9999. |
r87848 removes all limits on the year field; r87850 (Issue bpo-10864) limits year to [1; 9999] on Solaris. |
time.asctime(), time.ctime() and time.strftime() are no more not limited for the year field if accept2dyear=0. Except with Visual Studio or on Solaris: the year is limited to the range [1; 9999]. datetime.strftime() is limited to year >= 1000 (instead of year >= 1900) because it cannot set accept2dyear=0 temporary (it is no thread safe). We might fix it, but it is complex and not really needed. The correct fix is just to remove accept2dyear (and consider that accept2dyear=0), which cannot be done before Python 3.3. Anyway, the situation is much better in Python 3.2 than Python 3.1, and I consider that it's enough. Let's close the issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: