[Freeipa-devel] [PATCH 0397] ipapython: Use custom datetime to LDAP generalized time

Christian Heimes cheimes at redhat.com
Mon Jan 18 07:06:53 UTC 2016


On 2016-01-15 13:44, Tomas Babej wrote:
> Hi,
> 
> For the dates older than 1900, Python is unable to convert the datetime
> representation to string using strftime:
> 
> https://bugs.python.org/issue1777412
> 
> Work around the issue adding a custom method to convert the datetime
> objects to LDAP generalized time strings.
> 
> https://fedorahosted.org/freeipa/ticket/5579

I noticed that all previous strftime() calls and the new code ignore any
time zone information. This isn't an issue for tz-naive datetime object
that don't have any time zone information attached. You can't fix them
anyway and just hope they are always UTC. For tz-aware datetime object
your approach returns the wrong value.

You can use datetime.utctimetuple() instead. The method returns a time
tuple in UTC.

>>> value
datetime.datetime(2016, 1, 18, 8, 2, 49, 646270)
>>>
'{0.tm_year:4d}{0.tm_mon:02d}{0.tm_mday:02d}{0.tm_hour:02d}{0.tm_min:02d}{0.tm_sec:02d}Z'.format(value.utctimetuple())
'20160118080249Z'

https://docs.python.org/2/library/datetime.html#datetime.datetime.utctimetuple

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/freeipa-devel/attachments/20160118/e08cebf9/attachment.sig>


More information about the Freeipa-devel mailing list