Skip to content

Commit

Permalink
chore: migrate from deprecated datetime.datetime.utcfromtimestamp (ca…
Browse files Browse the repository at this point in the history
  • Loading branch information
aciba90 authored and mathmarchand committed Nov 21, 2024
1 parent 71db15c commit 19bb139
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
14 changes: 9 additions & 5 deletions cloudinit/analyze/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import argparse
import re
import sys
from datetime import datetime
from datetime import datetime, timezone
from typing import IO

from cloudinit.analyze import dump, show
Expand Down Expand Up @@ -128,17 +128,21 @@ def analyze_boot(name, args):
infh, outfh = configure_io(args)
kernel_info = show.dist_check_timestamp()
status_code, kernel_start, kernel_end, ci_sysd_start = kernel_info
kernel_start_timestamp = datetime.utcfromtimestamp(kernel_start)
kernel_end_timestamp = datetime.utcfromtimestamp(kernel_end)
ci_sysd_start_timestamp = datetime.utcfromtimestamp(ci_sysd_start)
kernel_start_timestamp = datetime.fromtimestamp(kernel_start, timezone.utc)
kernel_end_timestamp = datetime.fromtimestamp(kernel_end, timezone.utc)
ci_sysd_start_timestamp = datetime.fromtimestamp(
ci_sysd_start, timezone.utc
)
try:
last_init_local = [
e
for e in _get_events(infh)
if e["name"] == "init-local"
and "starting search" in e["description"]
][-1]
ci_start = datetime.utcfromtimestamp(last_init_local["timestamp"])
ci_start = datetime.fromtimestamp(
last_init_local["timestamp"], timezone.utc
)
except IndexError:
ci_start = "Could not find init-local log-line in cloud-init.log"
status_code = show.FAIL_CODE
Expand Down
4 changes: 3 additions & 1 deletion cloudinit/analyze/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ def event_timestamp(event):


def event_datetime(event):
return datetime.datetime.utcfromtimestamp(event_timestamp(event))
return datetime.datetime.fromtimestamp(
event_timestamp(event), datetime.timezone.utc
)


def delta_seconds(t1, t2):
Expand Down
6 changes: 4 additions & 2 deletions cloudinit/reporting/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import threading
import time
import uuid
from datetime import datetime
from datetime import datetime, timezone
from threading import Event
from typing import Union

Expand Down Expand Up @@ -375,7 +375,9 @@ def _encode_event(self, event):
"name": event.name,
"type": event.event_type,
"ts": (
datetime.utcfromtimestamp(event.timestamp).isoformat() + "Z"
datetime.fromtimestamp(
event.timestamp, timezone.utc
).isoformat()
),
}
if hasattr(event, self.RESULT_KEY):
Expand Down
10 changes: 6 additions & 4 deletions cloudinit/sources/helpers/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import textwrap
import zlib
from contextlib import contextmanager
from datetime import datetime
from datetime import datetime, timezone
from time import sleep, time
from typing import Callable, List, Optional, TypeVar, Union
from xml.etree import ElementTree as ET # nosec B405
Expand Down Expand Up @@ -122,9 +122,11 @@ def get_boot_telemetry():
"boot-telemetry",
"kernel_start=%s user_start=%s cloudinit_activation=%s"
% (
datetime.utcfromtimestamp(kernel_start).isoformat() + "Z",
datetime.utcfromtimestamp(user_start).isoformat() + "Z",
datetime.utcfromtimestamp(cloudinit_activation).isoformat() + "Z",
datetime.fromtimestamp(kernel_start, timezone.utc).isoformat(),
datetime.fromtimestamp(user_start, timezone.utc).isoformat(),
datetime.fromtimestamp(
cloudinit_activation, timezone.utc
).isoformat(),
),
events.DEFAULT_EVENT_ORIGIN,
)
Expand Down

0 comments on commit 19bb139

Please sign in to comment.