Skip to content

Commit

Permalink
datetime utc fix (and deprecation proof util)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannymeijer committed Oct 29, 2024
1 parent dba6204 commit c9588ae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/koheesio/integrations/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
model_validator,
)
from koheesio.spark.readers import Reader
from koheesio.utils import utc_now


class BoxFolderNotFoundError(Exception):
Expand Down Expand Up @@ -567,7 +568,7 @@ def action(self, file: File, folder: Folder) -> None:
file.copy(parent_folder=folder).update_info(
data={
"description": "\n".join(
[f"File processed on {datetime.datetime.now(datetime.UTC)}", file.get()["description"]]
[f"File processed on {utc_now()}", file.get()["description"]]
)
}
)
Expand Down Expand Up @@ -607,7 +608,7 @@ def action(self, file: File, folder: Folder) -> None:
file.move(parent_folder=folder).update_info(
data={
"description": "\n".join(
[f"File processed on {datetime.datetime.now(datetime.UTC)}", file.get()["description"]]
[f"File processed on {utc_now()}", file.get()["description"]]
)
}
)
Expand Down
3 changes: 2 additions & 1 deletion src/koheesio/notifications/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from koheesio.models import ConfigDict, Field
from koheesio.notifications import NotificationSeverity
from koheesio.steps.http import HttpPostStep
from koheesio.utils import utc_now


class SlackNotification(HttpPostStep):
Expand Down Expand Up @@ -92,7 +93,7 @@ class SlackNotificationWithSeverity(SlackNotification):
environment: str = Field(default=..., description="Environment description, e.g. dev / qa /prod")
application: str = Field(default=..., description="Pipeline or application name")
timestamp: datetime = Field(
default=datetime.datetime.now(datetime.UTC),
default_factory=utc_now,
alias="execution_timestamp",
description="Pipeline or application execution timestamp",
)
Expand Down
3 changes: 2 additions & 1 deletion src/koheesio/spark/etl_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from koheesio.spark.readers import Reader
from koheesio.spark.transformations import Transformation
from koheesio.spark.writers import Writer
from koheesio.utils import utc_now


class EtlTask(Step):
Expand Down Expand Up @@ -85,7 +86,7 @@ class EtlTask(Step):

# private attrs
etl_date: datetime = Field(
default=datetime.datetime.now(datetime.UTC),
default_factory=utc_now,
description="Date time when this object was created as iso format. Example: '2023-01-24T09:39:23.632374'",
)

Expand Down
13 changes: 13 additions & 0 deletions src/koheesio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"""

import inspect
from sys import version_info as PYTHON_VERSION
import uuid
from typing import Any, Callable, Dict, Optional, Tuple
from functools import partial
from importlib import import_module
from pathlib import Path
import datetime

__all__ = [
"get_args_for_func",
Expand All @@ -18,6 +20,10 @@
]


PYTHON_MINOR_VERSION = PYTHON_VERSION.major + PYTHON_VERSION.minor / 10
"""float: Python minor version as a float (e.g. 3.7)"""


def get_args_for_func(func: Callable, params: Dict) -> Tuple[Callable, Dict[str, Any]]:
"""Helper function that matches keyword arguments (params) on a given function
Expand Down Expand Up @@ -99,3 +105,10 @@ def convert_str_to_bool(value: str) -> Any:
if isinstance(value, str) and (v := value.lower()) in ["true", "false"]:
value = v == "true"
return value


def utc_now() -> datetime.datetime:
"""Get current time in UTC"""
if PYTHON_MINOR_VERSION < 3.11:
return datetime.datetime.utcnow()
return datetime.datetime.now(datetime.timezone.utc)

0 comments on commit c9588ae

Please sign in to comment.