diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-01-05 10:38:34 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-01-05 10:38:34 +0000 |
commit | e3bdad36cc3a1a00c1e6772ca1c1898085ab73e0 (patch) | |
tree | 34512072a667ae716fd262e7b37e733e60fe4d89 /docs/docs/limitations.md | |
parent | Adding upstream version 2.1.2. (diff) | |
download | pendulum-e3bdad36cc3a1a00c1e6772ca1c1898085ab73e0.tar.xz pendulum-e3bdad36cc3a1a00c1e6772ca1c1898085ab73e0.zip |
Adding upstream version 3.0.0~a1.upstream/3.0.0_a1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'docs/docs/limitations.md')
-rw-r--r-- | docs/docs/limitations.md | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/docs/docs/limitations.md b/docs/docs/limitations.md new file mode 100644 index 0000000..7deff23 --- /dev/null +++ b/docs/docs/limitations.md @@ -0,0 +1,43 @@ +# Limitations + +Even though the `DateTime` class is a subclass of `datetime`, +there are some rare cases where it can't replace the native class directly. +Here is a list (non-exhaustive) of the reported cases with a possible solution, if any: + +* `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: + + ```python + import pendulum + from sqlite3 import register_adapter + + register_adapter(pendulum.DateTime, lambda val: val.isoformat(' ')) + ``` + +* `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: + + ```python + import pendulum + import MySQLdb.converters + import pymysql.converters + + MySQLdb.converters.conversions[pendulum.DateTime] = MySQLdb.converters.DateTime2literal + pymysql.converters.conversions[pendulum.DateTime] = pymysql.converters.escape_datetime + ``` + +* `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`: + + ```python + import pendulum + from django.db.models import DateTimeField as BaseDateTimeField + + + class DateTimeField(BaseDateTimeField): + + def value_to_string(self, obj): + val = self.value_from_object(obj) + + if isinstance(value, pendulum.DateTime): + return value.format('YYYY-MM-DD HH:mm:ss') + + return '' if val is None else val.isoformat() + ``` |