Opened 13 years ago
Closed 13 years ago
#18951 closed Bug (fixed)
Datetime microseconds cutoff first zero in Django templates
| Reported by: | Owned by: | Aymeric Augustin | |
|---|---|---|---|
| Component: | Template system | Version: | 1.4 |
| Severity: | Normal | Keywords: | datetime microseconds |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
So I had a datetime object that I wanted to display on a web page and then post back through a hidden field. This didn't work (unless I use the 'c' flag with the date filter to get the ISO format, then it works fine).
>>> datetime.datetime(2013, 9, 12, 9, 24, 16, 14070) datetime.datetime(2013, 9, 12, 9, 24, 16, 14070) >>> str(datetime.datetime(2013, 9, 12, 9, 24, 16, 14070)) '2013-09-12 09:24:16.014070'
BUT {{ timestamp|date:'Y-m-d H:i:s.u' }} prints:
2013-09-12 09:24:16.14070
without the first zero. Then when trying to convert this back to a datetime object I get:
>>> datetime.datetime.strptime('2013-09-12 09:24:16.14070', "%Y-%m-%d %H:%M:%S.%f")
datetime.datetime(2013, 9, 12, 9, 24, 16, 140700)
>>> str(datetime.datetime.strptime('2013-09-12 09:24:16.14070', "%Y-%m-%d %H:%M:%S.%f"))
'2013-09-12 09:24:16.140700'
So in the end I ended up with the microseconds: 140700 instead of 014070, but the real bug is that the 'u' flag of date prints 14070 instead of 014070.
Change History (4)
comment:1 by , 13 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 13 years ago
| Owner: | changed from to |
|---|
comment:3 by , 13 years ago
comment:4 by , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Technically, this is the documented behavior. https://docs.djangoproject.com/en/dev/ref/templates/builtins/ says:
and not:
That said, it isn't a very useful behavior :)
Django's
dateformatmimicks PHP'sdate. I just checked that PHP left-pads the result with zeros.I think it's just a bug that dates back to 2005, and I'm going to commit a fix shortly.