question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

datetime support break default function

See original GitHub issue

Before version 2.0.2 we use DjangoJSONEncoder.default to handle datatime, but it doesn’t works now, and the orjson datetime is not compatible with the logic below:

https://github.com/django/django/blob/master/django/core/serializers/json.py#L81

def default(self, o):
    # See "Date Time String Format" in the ECMA-262 specification.
    if isinstance(o, datetime.datetime):
        r = o.isoformat()
        if o.microsecond:
            r = r[:23] + r[26:]
        if r.endswith('+00:00'):
            r = r[:-6] + 'Z'
        return r
    elif isinstance(o, datetime.date):
        return o.isoformat()
    elif isinstance(o, datetime.time):
        if is_aware(o):
            raise ValueError("JSON can't represent timezone-aware times.")
        r = o.isoformat()
        if o.microsecond:
            r = r[:12]
        return r
    elif isinstance(o, datetime.timedelta):
        return duration_iso_string(o)
    elif isinstance(o, (decimal.Decimal, uuid.UUID, Promise)):
        return str(o)
    else:
        return super().default(o)

Is there a way to make default works with datetime object?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ijlcommented, Mar 28, 2019

I’ll look into overriding orjson serializing datetime objects. It also makes sense for another use case to allow serializing datetime.datetime objects without a tzinfo. I could make orjson output three digits of microseconds and match Django, but I don’t see a reason to prefer that over Python’s full precision, so it looks like you do need to avoid its handling.

0reactions
phyngcommented, Apr 3, 2019

I think it is ok now, three digits or six digits is ok. Thank you very much!

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import orjson
>>> import datetime
>>> orjson.dumps(datetime.datetime.now())
b'"2019-04-03T08:00:28.473661"'
Read more comments on GitHub >

github_iconTop Results From Across the Web

Does inheritance of DateTime change any default function ...
When you implement/extend a class, the parent class (in this case DateTime ) will pass its methods to the child class( OwnDate ),...
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
This in-depth guide to DateTime manipulation should help you understand the programming concepts and best practices relevant to time and date without having ......
Read more >
DateTime Functions - Alteryx Help
Use a DateTime function to add or subtract intervals, find the current date, find the first or last day of the month, extract...
Read more >
Datetime functions | BigQuery - Google Cloud
Constructs a DATETIME object using a TIMESTAMP object. It supports an optional parameter to specify a time zone. If no time zone is...
Read more >
Choose the right date function - Microsoft Support
Date functions like DateAdd, DatePart, and DateSerial help you calculate and display dates the way you want.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found