deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

truncate_datetime - millisecond support

Open chriswyatt opened this issue 1 year ago • 2 comments
trafficstars

Is your feature request related to a problem? Please describe. Some databases (e.g. Mongo) store date/time to millisecond precision. Being able to truncate to this would be useful, and a very simple addition.

Describe the solution you'd like Simply to be able to pass truncate_datetime='millisecond'.

Describe alternatives you've considered N/A

Additional context N/A

chriswyatt avatar Aug 21 '24 13:08 chriswyatt

Hi @chriswyatt Can you please provide an example where truncate_datetime will be used for? Are you saying if the difference between 2 datetimes is less than a millisecond, we shouldn't report it as a diff then?

seperman avatar Aug 26 '24 06:08 seperman

I was just thinking, work exactly the same way as the other truncate methods, but just down to the millisecond.

def datetime_normalize(truncate_datetime, obj)

...
        elif truncate_datetime == 'millisecond':
            obj = obj.replace(microsecond=(obj.microsecond // 1000) * 1000)

The BSON serializer that comes with PyMongo also floors the value in this way. This is useful for diffing datetimes before and after they get truncated by the BSON serializer. I'm just not interested in the microsecond part as it gets thrown away.

chriswyatt avatar Aug 27 '24 13:08 chriswyatt