Granulate / gprofiler

gProfiler is a system-wide profiler, combining multiple sampling profilers to produce unified visualization of what your CPU is spending time on.
https://profiler.granulate.io
Apache License 2.0
746 stars 54 forks source link

metrics: Fix timestamp #710

Closed d3dave closed 1 year ago

d3dave commented 1 year ago

time.monotonic does not represent current time.

d3dave commented 1 year ago

@roi-granulate Please test, thanks.

d3dave commented 1 year ago

LGTM. What's between datetime.now() and datetime.utcnow() btw?

utcnow() returns a TZ-unaware object. now() returns a TZ-aware object. So if you try to get timestamp() of TZ-unaware object, it will assume it is in local time and you'd get a wrong result:

In [5]: datetime.now(tz=timezone.utc)
Out[5]: datetime.datetime(2023, 3, 6, 14, 8, 39, 670833, tzinfo=datetime.timezone.utc)

In [6]: _.timestamp()
Out[6]: 1678111719.670833

In [7]: datetime.utcnow()
Out[7]: datetime.datetime(2023, 3, 6, 14, 8, 48, 270091)

In [8]: _.timestamp()
Out[8]: 1678104528.270091
Jongy commented 1 year ago

LGTM. What's between datetime.now() and datetime.utcnow() btw?

utcnow() returns a TZ-unaware object. now() returns a TZ-aware object. So if you try to get timestamp() of TZ-unaware object, it will assume it is in local time and you'd get a wrong result:

In [5]: datetime.now(tz=timezone.utc)
Out[5]: datetime.datetime(2023, 3, 6, 14, 8, 39, 670833, tzinfo=datetime.timezone.utc)

In [6]: _.timestamp()
Out[6]: 1678111719.670833

In [7]: datetime.utcnow()
Out[7]: datetime.datetime(2023, 3, 6, 14, 8, 48, 270091)

In [8]: _.timestamp()
Out[8]: 1678104528.270091

Oh wow. TIL