Skip to content

Commit

Permalink
Fix utc for datetime.now
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Van Brunt authored and avanbrunt-cb committed Mar 25, 2024
1 parent 4e95790 commit 83cf679
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ flake8==5.0.4
flake8-colors==0.1.9
flake8-docstrings==1.7.0
pre-commit>=2.15.0
freezegun==1.2.2
freezegun==1.4.0
7 changes: 4 additions & 3 deletions src/cbc_syslog/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pathlib

from configparser import ConfigParser, NoSectionError
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from cbc_syslog.util import CarbonBlackCloud, Transform, Output
from cbc_syslog.util.example import (EXAMPLE_ALERT_CEF_TEMPLATE,
EXAMPLE_ALERT_LEEF_TEMPLATE,
Expand Down Expand Up @@ -61,11 +61,12 @@ def poll(config):
previous_state = {}

# Use last end_time unless no state is available use 90s ago
new_start_time_str = previous_state.get("end_time", (datetime.now() - timedelta(seconds=90)).strftime(TIME_FORMAT))
new_start_time_str = previous_state.get("end_time", (datetime.now(timezone.utc) - timedelta(seconds=90)).strftime(TIME_FORMAT))
new_start_time = datetime.strptime(new_start_time_str, TIME_FORMAT)
new_start_time = new_start_time.replace(tzinfo=timezone.utc)

# Should stay behind 30s to ensure backend data is available
end_time = datetime.now() - timedelta(seconds=30)
end_time = datetime.now(timezone.utc) - timedelta(seconds=30)

if end_time < new_start_time:
log.error("Unable to fetch data please wait a minimum of 60s before next poll")
Expand Down
4 changes: 2 additions & 2 deletions src/cbc_syslog/util/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import ssl
import traceback

from datetime import datetime
from datetime import datetime, timezone

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -113,7 +113,7 @@ def send(self, data):

elif type == "file":
try:
encoded = base64.b64encode(datetime.now().strftime('%Y-%m-%dT%H:%M:00.000Z').encode("ascii"))
encoded = base64.b64encode(datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:00.000Z').encode("ascii"))
file_name = f"{encoded.decode('ascii')}.txt"
new_file_path = pathlib.Path(self.output_params.get("file_path")).joinpath(file_name)
with open(new_file_path, "a") as new_file:
Expand Down

0 comments on commit 83cf679

Please sign in to comment.