Skip to content

Commit

Permalink
Datetime fix for older Py versions
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed May 11, 2024
1 parent 14381dc commit 1ccb000
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bw_processing/datapackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from .filesystem import clean_datapackage_name
from .io_helpers import file_reader, file_writer
from .proxies import Proxy, UndefinedInterface
from .utils import check_name, check_suffix, load_bytes, resolve_dict_iterator
from .utils import check_name, check_suffix, load_bytes, resolve_dict_iterator, utc_now


class DatapackageBase(ABC):
Expand Down Expand Up @@ -369,7 +369,7 @@ def _create(
"id": id_ or uuid.uuid4().hex,
"licenses": (metadata or {}).get("licenses", DEFAULT_LICENSES),
"resources": [],
"created": datetime.datetime.now(datetime.UTC).isoformat("T") + "Z",
"created": utc_now().isoformat("T") + "Z",
"combinatorial": combinatorial,
"sequential": sequential,
"seed": seed,
Expand Down
9 changes: 9 additions & 0 deletions bw_processing/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from io import BytesIO
from pathlib import Path
from typing import Any, Union
from datetime import datetime

import numpy as np
from numpy.lib.recfunctions import repack_fields
Expand Down Expand Up @@ -98,3 +99,11 @@ def resolve_dict_iterator(iterator: Any, nrows: int = None) -> tuple:
),
array["flip"],
)


def utc_now() -> datetime:
"""Get current datetime compatible with Py 3.8 to 3.12"""
if hasattr(datetime, "UTC"):
return datetime.now(datetime.UTC)
else:
return datetime.utcnow()

0 comments on commit 1ccb000

Please sign in to comment.