Skip to content

Fix parse_date losing a microsecond on some fractional seconds - #2243

Open
aad1tyaaaaa wants to merge 2 commits into
apache:trunkfrom
aad1tyaaaaa:fix-parse-date-microseconds
Open

aad1tyaaaaa wants to merge 2 commits into
apache:trunkfrom
aad1tyaaaaa:fix-parse-date-microseconds

Conversation

@aad1tyaaaaa

Copy link
Copy Markdown

Fix parse_date losing a microsecond on some fractional seconds

Description

libcloud.utils.iso8601.parse_date converts the fractional seconds with int(float("0.%s" % fraction) * 1e6). The float product is sometimes just below the integer, so the result comes out one microsecond short:

>>> from libcloud.utils.iso8601 import parse_date
>>> parse_date("2020-01-01T00:00:00.000249Z").microsecond
248

Of all 1,000,000 six-digit fractions, 11,549 were parsed one microsecond short (e.g. .000249, .000251, .000489). parse_date is used by many drivers to parse timestamps from provider APIs (EC2, Azure ARM, OpenStack, DigitalOcean, Scaleway, ...).

This shows up in the existing tests too: the Scaleway list_nodes.json fixture has "creation_date": "2014-05-22T12:57:22.514299+00:00", while test_list_nodes_fills_created_datetime expected 514298, the value the float conversion produced.

The fix takes the microseconds directly from the digits: int(fraction[:6].ljust(6, "0")). With it, all 1,000,000 six-digit fractions parse exactly. Fractions with more than six digits are still truncated to microseconds, as before.

Status

  • done, ready for review

Checklist

  • Code linting (black, isort)
  • Documentation (CHANGES.rst entry)
  • Tests: new test_parse_date_fraction in libcloud/test/test_utils.py (the cases that used to come out short fail without this change), and the Scaleway expectation corrected to match its fixture. The utils tests and the compute driver tests that parse dates (EC2, Azure ARM, OpenStack, DigitalOcean, gridscale, Scaleway, vCloud) pass.
  • ICLA not required for this small change

I used an AI assistant (Claude Code) to help find this bug and write the fix and tests. I reviewed and ran everything myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant