Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: swap out deprecated datetime's utcnow and utcfromtimestamp #11850

Merged
merged 11 commits into from
Jan 9, 2025

Conversation

wantsui
Copy link
Collaborator

@wantsui wantsui commented Jan 2, 2025

See #11608 and #11497.

Starting with Python 3.12, there were changes to datetime:

datetime: datetime.datetime’s utcnow() and utcfromtimestamp() are deprecated and will be removed in a future version. Instead, use timezone-aware objects to represent datetimes in UTC: respectively, call now() and fromtimestamp() with the tz parameter set to datetime.UTC. (Contributed by Paul Ganssle in gh-103857.)

The result is that the usage of utcnow and utcfromtimestamp now throw deprecation warnings when used, ie:

DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).

There's a difference of +00:00 between the old version and the new format.

For utcnow -> now

  • datetime.datetime.utcnow().isoformat() | '2025-01-02T19:51:32.579733'
  • datetime.datetime.now(datetime.timezone.utc).isoformat() | '2025-01-02T19:51:02.275232+00:00'

For utcfromtimestamp -> fromtimestamp

Assume that end_time_ns=1735848645000000000:

  • (datetime.datetime.fromtimestamp(end_time_ns / 1e9, tz=datetime.timezone.utc).replace(microsecond=0).isoformat() + "Z")
    • returns '2025-01-02T20:10:45+00:00Z'
  • (datetime.datetime.utcfromtimestamp(end_time_ns / 1e9).replace(microsecond=0).isoformat() + "Z")
    • returns '2025-01-02T20:10:45Z'

As a result, I attempted remove the trailing ones to be consistent with the old format, but can bring it back.

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@wantsui wantsui changed the title swap out deprecated datetime.utcnow with now swap out deprecated datetime's utcnow and utcfromtimestamp Jan 2, 2025
Copy link
Contributor

github-actions bot commented Jan 2, 2025

CODEOWNERS have been resolved as:

ddtrace/internal/debug.py                                               @DataDog/apm-core-python
ddtrace/profiling/exporter/http.py                                      @DataDog/profiling-python
tests/appsec/iast_packages/packages/pkg_pyjwt.py                        @DataDog/asm-python

@pr-commenter
Copy link

pr-commenter bot commented Jan 2, 2025

Benchmarks

Benchmark execution time: 2025-01-09 17:35:48

Comparing candidate commit 264fcd5 in PR branch wantsui/changedatetime with baseline commit 35fe7b5 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 346 metrics, 2 unstable metrics.

@wantsui wantsui marked this pull request as ready for review January 3, 2025 14:57
@wantsui wantsui requested review from a team as code owners January 3, 2025 14:57
@wantsui wantsui requested a review from emmettbutler January 3, 2025 14:57
Copy link
Contributor

@nsrip-dd nsrip-dd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for profiling. Timestamps are consistent with what we had before. Thanks!

@emmettbutler emmettbutler added the changelog/no-changelog A changelog entry is not required for this PR. label Jan 6, 2025
@emmettbutler emmettbutler changed the title swap out deprecated datetime's utcnow and utcfromtimestamp chore: swap out deprecated datetime's utcnow and utcfromtimestamp Jan 6, 2025
@datadog-dd-trace-py-rkomorn
Copy link

datadog-dd-trace-py-rkomorn bot commented Jan 8, 2025

Datadog Report

Branch report: wantsui/changedatetime
Commit report: 264fcd5
Test service: dd-trace-py

✅ 0 Failed, 87 Passed, 1468 Skipped, 4m 1.45s Total duration (35m 33.76s time saved)

@wantsui wantsui enabled auto-merge (squash) January 9, 2025 15:13
@wantsui wantsui merged commit bfa3b82 into main Jan 9, 2025
684 checks passed
@wantsui wantsui deleted the wantsui/changedatetime branch January 9, 2025 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog/no-changelog A changelog entry is not required for this PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants