#16906: Avoid strftime when isoformat can do the job
-------------------------------------+-------------------------------------
Reporter: aaugustin | Owner: nobody
Type: | Status: new
Cleanup/optimization | Component: Core (Other)
Milestone: | Severity: Normal
Version: 1.3 | Keywords:
Resolution: | Has patch: 0
Triage Stage: | Needs tests: 0
Unreviewed | Easy pickings: 0
Needs documentation: 0 |
Patch needs improvement: 0 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
Description changed by aaugustin:
Old description:
> r7946 introduced a `datetime_safe` module that provides a Python
> implementation of `datetime.[date|datetime].strftime` for dates before
> 1900.
>
> However, this is overkill for the purpose of just showing a `date` or
> `datetime` in ISO format (`%Y-%m-%d` and `%Y-%m-%d %H:%M:%S`). It makes
> the code needlessly complicated, sometimes inconistent, and inefficient.
> The `isoformat` method achieves the same result, works for any date, and
> is implemented in C.
>
> Also, some code still uses the `strftime` function from the standard
> library. Using `isoformat` is better because it isn't subject to the 1900
> limit.
>
> See attached patch.
>
> ----
>
> For more background, see `isoformat_date` in
> http://svn.python.org/view/python/tags/r271/Modules/datetimemodule.c?view=markup.
> Note that `date` objects have no `__unicode__` and their `__str__` just
> call `isoformat`, so `str(date)` or `unicode(date)` is the same as
> `datetime_safe.date(date.year, date.month,
> date.day).strftime('%Y-%m-%d')`. It's pretty much the same for `datetime`
> objects, except that `isoformat` uses `T` as a separator, while `str` and
> `unicode` use a space.
>
> Changing `date.format('%Y-%m-%d')` to `date.isoformat()` is safe.
> Changing `datetime.format('%Y-%m-%d %H:%M:%S')` to `datetime.isoformat()`
> requires removing the microseconds and the timezone first, if there is
> one, so the correct pattern is `datetime.replace(microsecond=0,
> tzinfo=None)`. This also applies for time objects.
New description:
While working on timezone support in Django, the inconsistency in these
three functions surprised me:
https://code.djangoproject.com/browser/django/trunk/django/db/backends/__init__.py#L714
----
In fact, r7946 introduced a `datetime_safe` module that provides a Python
implementation of `datetime.[date|datetime].strftime` for dates before
1900.
However, this is overkill for the purpose of just showing a `date` or
`datetime` in ISO format (`%Y-%m-%d` and `%Y-%m-%d %H:%M:%S`). It makes
the code needlessly complicated, sometimes inconistent, and inefficient.
The `isoformat` method achieves the same result, works for any date, and
is implemented in C.
Also, some code still uses the `strftime` function from the standard
library. Using `isoformat` is better because it isn't subject to the 1900
limit.
See attached patch.
----
For more background, see `isoformat_date` in
http://svn.python.org/view/python/tags/r271/Modules/datetimemodule.c?view=markup.
Note that `date` objects have no `__unicode__` and their `__str__` just
call `isoformat`, so `str(date)` or `unicode(date)` is the same as
`datetime_safe.date(date.year, date.month,
date.day).strftime('%Y-%m-%d')`. It's pretty much the same for `datetime`
objects, except that `isoformat` uses `T` as a separator, while `str` and
`unicode` use a space.
Changing `date.format('%Y-%m-%d')` to `date.isoformat()` is safe. Changing
`datetime.format('%Y-%m-%d %H:%M:%S')` to `datetime.isoformat()` requires
removing the microseconds and the timezone first, if there is one, so the
correct pattern is `datetime.replace(microsecond=0, tzinfo=None)`. This
also applies for time objects.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/16906#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.