There are two failure cases:

1. "I wanted to use this as a string but it's a datetime"
2. "I wanted to parse this string as a datetime but it's already a datetime"

I'll present a hack for each case that should make code work in either
version of launchpadlib (actually wadllib).

In case 1 you should probably rewrite your code to understand datetime
objects, and then move to case 2. But you can use code like the
following to turn datetime objects into strings, undoing the
conversion found in later versions of launchpadlib.

---------
import datetime

def no_datetimes_allowed(t):
    if isinstance(t, datetime.datetime) or isinstance(t, datetime.date):
        return t.isoformat()
    return t

no_datetimes_allowed("2005-01-01T10:20:30")
# "2005-01-01T10:20:30"

no_datetimes_allowed(datetime.datetime(2005, 1, 1, 10, 20, 30))
# "2005-01-01T10:20:30"
---------

For case 2, you can use the following code to force a value to be a
datetime.

---------
import datetime

def must_be_datetime(t):
    if isinstance(t, datetime.datetime) or isinstance(t, datetime.date):
       return t
    try:
        return datetime.datetime.strptime(t[:19], "%Y-%m-%dT%H:%M:%S")
    except ValueError:
        return datetime.datetime.strptime(t[:11], "%Y-%m-%d")


must_be_datetime("2005-01-01T10:20:30")
# datetime.datetime(2005, 1, 1, 10, 20, 30)

must_be_datetime("2005-01-01")
# datetime.datetime(2005, 1, 1, 0, 0)

must_be_datetime(datetime.datetime(2005, 1, 1, 10, 20, 30))
# datetime.datetime(2005, 1, 1, 10, 20, 30)
----------

But! That's not a great solution. Launchpad serves dates in ISO 8601
format, and there's no easy way in Python to parse an ISO 8601
date. The code above works for the two most common formats, and it
should get you a datetime for every value served by Launchpad, but I
might have missed something. There are also two other potential
problems with the generated datetime objects.

1) The parsed datetime objects won't have timezone info. All dates
served by Launchpad are UTC, so this shouldn't be a problem if you
know this.

2) Because the fractions of a second are not parsed, you'll lose
resolution. You might accidentally overwrite a high-resolution value
on the server with a low-resolution value, or Launchpad might
interpret your low-resolution value as an attempt to modify a
read-only field. I don't think this should happen in ordinary
applications, but if something goes wrong, you'll know what happened.

These problems are the reason why we made this change in the first
place. The date strings are complicated and you shouldn't have to
parse them. wadllib (a launchpadlib dependency) has code that will
reliably parse any ISO 8601 date, with timezone and fractions of a
second. If you need to, you can copy that library into your code and
do what wadllib does.

-- 
bug task date types changed
https://bugs.launchpad.net/bugs/356632
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.

-- 
ubuntu-bugs mailing list
ubuntu-bugs@lists.ubuntu.com
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to