Message261033
| Author |
gregory.p.smith |
| Recipients |
Sriram Rajagopalan, belopolsky, gregory.p.smith |
| Date |
2016年02月29日.23:44:22 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1456789462.88.0.132575184838.issue26460@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
time.strptime() is "working" (not raising an exception) as it appears not to validate the day of the month when a year is not specified, yet the return value from either of these APIs is a date which has no concept of an ambiguous year.
## Via the admittedly old Python 2.7.6 from Ubuntu 14.04: ##
# 1900 was not a leap year as it is not divisible by 400.
>>> time.strptime("1900 Feb 29", "%Y %b %d")
ValueError: day is out of range for month
>>> time.strptime("Feb 29", "%b %d")
time.struct_time(tm_year=1900, tm_mon=2, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=60, tm_isdst=-1)
So what should the validation behavior be?
>>> datetime.datetime.strptime("Feb 29", "%b %d")
ValueError: day is out of range for month
>>> datetime.datetime.strptime("2016 Feb 29", "%Y %b %d")
datetime.datetime(2016, 2, 29, 0, 0)
>>> datetime.datetime.strptime("1900 Feb 29", "%Y %b %d")
ValueError: day is out of range for month
>>> datetime.datetime(year=1900, month=2, day=29)
ValueError: day is out of range for month
datetime objects cannot be constructed with the invalid date (as the time.strptime return value allows).
Changing the API to assume the current year or a +/- 6 months from "now" when no year is parsed is likely to break existing code. |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2016年02月29日 23:44:22 | gregory.p.smith | set | recipients:
+ gregory.p.smith, belopolsky, Sriram Rajagopalan |
| 2016年02月29日 23:44:22 | gregory.p.smith | set | messageid: <1456789462.88.0.132575184838.issue26460@psf.upfronthosting.co.za> |
| 2016年02月29日 23:44:22 | gregory.p.smith | link | issue26460 messages |
| 2016年02月29日 23:44:22 | gregory.p.smith | create |
|