Skip to content

Commit 863b3a9

Browse files
committed
Adds a Limitations section to documentation
1 parent 104f88f commit 863b3a9

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,53 @@ Those are a few examples showing that Arrow cannot always be trusted to have a c
120120
behavior with the data you are passing to it.
121121

122122

123+
Limitations
124+
===========
125+
126+
Even though the ``Pendulum`` class is a subclass of ``datetime`` there are some rare cases where
127+
it can't replace the native class directly. Here is a list (non-exhaustive) of the reported cases with
128+
a possible solution, if any:
129+
130+
* ``sqlite3`` will use the the ``type()`` function to determine the type of the object by default. To work around it you can register a new adapter:
131+
132+
.. code-block:: python
133+
134+
from pendulum import Pendulum
135+
from sqlite3 import register_adapter
136+
137+
register_adapter(Pendulum, lambda val: val.isoformat(' '))
138+
139+
* ``mysqlclient`` (former ``MySQLdb``) and ``PyMySQL`` will use the the ``type()`` function to determine the type of the object by default. To work around it you can register a new adapter:
140+
141+
.. code-block:: python
142+
143+
import MySQLdb.converters
144+
import pymysql.converters
145+
146+
from pendulum import Pendulum
147+
148+
MySQLdb.converters.conversions[Pendulum] = MySQLdb.converters.DateTime2literal
149+
pymysql.converters.conversions[Pendulum] = pymysql.converters.escape_datetime
150+
151+
* ``django`` will use the ``isoformat()`` method to store datetimes in the database. However since ``pendulum`` is always timezone aware the offset information will always be returned by ``isoformat()`` raising an error, at least for MySQL databases. To work around it you can either create your own ``DateTimeField`` or use the previous workaround for ``MySQLdb``:
152+
153+
.. code-block:: python
154+
155+
from django.db.models import DateTimeField as BaseDateTimeField
156+
from pendulum import Pendulum
157+
158+
159+
class DateTimeField(BaseDateTimeField):
160+
161+
def value_to_string(self, obj):
162+
val = self.value_from_object(obj)
163+
164+
if isinstance(value, Pendulum):
165+
return value.to_datetime_string()
166+
167+
return '' if val is None else val.isoformat()
168+
169+
123170
Resources
124171
=========
125172

docs/_docs/limitations.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Limitations
2+
===========
3+
4+
Even though the ``Pendulum`` class is a subclass of ``datetime`` there are some rare cases where
5+
it can't replace the native class directly. Here is a list (non-exhaustive) of the reported cases with
6+
a possible solution, if any:
7+
8+
* ``sqlite3`` will use the the ``type()`` function to determine the type of the object by default. To work around it you can register a new adapter:
9+
10+
.. code-block:: python
11+
12+
from pendulum import Pendulum
13+
from sqlite3 import register_adapter
14+
15+
register_adapter(Pendulum, lambda val: val.isoformat(' '))
16+
17+
* ``mysqlclient`` (former ``MySQLdb``) and ``PyMySQL`` will use the the ``type()`` function to determine the type of the object by default. To work around it you can register a new adapter:
18+
19+
.. code-block:: python
20+
21+
import MySQLdb.converters
22+
import pymysql.converters
23+
24+
from pendulum import Pendulum
25+
26+
MySQLdb.converters.conversions[Pendulum] = MySQLdb.converters.DateTime2literal
27+
pymysql.converters.conversions[Pendulum] = pymysql.converters.escape_datetime
28+
29+
* ``django`` will use the ``isoformat()`` method to store datetimes in the database. However since ``pendulum`` is always timezone aware the offset information will always be returned by ``isoformat()`` raising an error, at least for MySQL databases. To work around it you can either create your own ``DateTimeField`` or use the previous workaround for ``MySQLdb``:
30+
31+
.. code-block:: python
32+
33+
from django.db.models import DateTimeField as BaseDateTimeField
34+
from pendulum import Pendulum
35+
36+
37+
class DateTimeField(BaseDateTimeField):
38+
39+
def value_to_string(self, obj):
40+
val = self.value_from_object(obj)
41+
42+
if isinstance(value, Pendulum):
43+
return value.to_datetime_string()
44+
45+
return '' if val is None else val.isoformat()

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
.. include:: _docs/testing.rst
1414
.. include:: _docs/interval.rst
1515
.. include:: _docs/period.rst
16+
.. include:: _docs/limitations.rst
1617
.. include:: _docs/date.rst
18+
.. include:: _docs/time.rst

0 commit comments

Comments
 (0)