enhancement(util): accept nanos and timestamps without Z/z in timestamp conversion#268
Conversation
Summary of ChangesHello @HassanBahati, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness of timestamp conversion utilities within the SDK. It addresses previous limitations by allowing more flexible input formats for timestamps, specifically by accepting both Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request enhances the timestamp conversion utility by allowing nanos as an alternative to nanoseconds in object and dictionary representations, and by correctly handling string timestamps that lack a 'Z' or 'z' timezone suffix. The changes are well-tested with new unit tests covering these specific scenarios. The overall functionality is improved, making the utility more robust to varied input formats.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Description
This PR extends the timestamp conversion fixes in #262 so the SDK accepts payloads without requiring callers to normalize them:
nanoskey/attribute (protobuf-style) in addition tonanoseconds;"2025-11-06T12:00:00.000") no longer causeValueErroringet_precision_timestamp. The same integer-based normalization (divmod, timedelta) and review feedback from PR fix(util): improve the robustness of timestamp conversion function #262 are preserved.Related Issues
Fixes #260 (fully; PR #262 fixed the dict/object path for
nanosecondsonly; this addsnanosand fixes the string path when Z/z is missing).Changes Made
timestamp_conversion(util.py):getattr(timestamp, "nanoseconds", getattr(timestamp, "nanos", None))so both.nanosecondsand.nanosare accepted.timestamp.get("nanoseconds", timestamp.get("nanos"))so both"nanoseconds"and"nanos"keys are accepted (e.g. protobuf/Firebase backend payloads).get_precision_timestamp(util.py): Only split the fraction on"Z"or"z"when present; otherwise use the fraction as-is. PreventsValueError: not enough values to unpackwhen the Cloud Eventtime(or other string) has no trailing timezone (e.g."2025-11-06T12:00:00.000").test_timestamp_conversion_dict_nanos_equivalent_to_nanoseconds: dict with"nanos"produces the same result as"nanoseconds".test_timestamp_conversion_object_nanos: object with.secondsand.nanos(no.nanoseconds) is accepted and matches dict result.test_get_precision_timestamp_without_timezone_suffix:get_precision_timestamp("2025-11-06T12:00:00.000")does not raise and returnsMICROSECONDS.Testing
Tests Run
pytest tests/test_util.py -v -k "timestamp_conversion or get_precision"POSTwithpayloads/stability_digest_ce.json(payload usesdigestDatewithnanosand includes top-leveltime). Result: handler ran successfully, log showedStability digest: app_id=1:123456789:android:abcdef, curl returnedOK. NoAttributeErrororValueError.Additional Notes
"seconds"and"nanoseconds"and kept the existing string path. This PR; widens accepted keys/attributes to includenanos, and (2) fixes the string path inget_precision_timestampwhen the fraction has noZ/z. Same normalization logic and alignment with review (nofromtimestamp, integer math, docstring/parameter names) are unchanged.digestDateas{"seconds": "...", "nanos": 0}still failed (dict branch required"nanoseconds"). After accommodating that, the event’s top-leveltimefield could be a string without a trailingZ/z;get_precision_timestampthen dids_fraction.split("z")and unpacked to two values, causingValueError: not enough values to unpack (expected 2, got 1). This PR addresses both cases so the SDK accepts varied real-world payloads.