Skip to content

Commit

Permalink
RF: Centralize to have a helper utcnow()
Browse files Browse the repository at this point in the history
  • Loading branch information
yarikoptic authored and effigies committed Oct 6, 2024
1 parent f7a37ef commit cd4bd5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
7 changes: 4 additions & 3 deletions nipype/interfaces/base/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
from copy import deepcopy
from textwrap import wrap
import re
from datetime import datetime as dt, UTC
from datetime import datetime as dt
from dateutil.parser import parse as parseutc
import platform

from ... import logging, config
from ...utils.datetime import utcnow
from ...utils.misc import is_container, rgetcwd
from ...utils.filemanip import md5, hash_infile

Expand Down Expand Up @@ -72,15 +73,15 @@ def __enter__(self):
if self._runtime.redirect_x:
self._runtime.environ["DISPLAY"] = config.get_display()

self._runtime.startTime = dt.isoformat(dt.now(UTC))
self._runtime.startTime = dt.isoformat(utcnow())
self._resmon.start()
# TODO: Perhaps clean-up path and ensure it exists?
os.chdir(self._runtime.cwd)
return self._runtime

def __exit__(self, exc_type, exc_value, exc_tb):
"""Tear-down interface execution."""
self._runtime.endTime = dt.isoformat(dt.now(UTC))
self._runtime.endTime = dt.isoformat(utcnow())
timediff = parseutc(self._runtime.endTime) - parseutc(self._runtime.startTime)
self._runtime.duration = (
timediff.days * 86400 + timediff.seconds + timediff.microseconds / 1e6
Expand Down
4 changes: 2 additions & 2 deletions nipype/pipeline/engine/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import os
import os.path as op
import sys
from datetime import datetime, UTC
from copy import deepcopy
import pickle
import shutil

import numpy as np

from ... import config, logging
from ...utils.datetime import utcnow
from ...utils.misc import str2bool
from ...utils.functions import getsource, create_function_from_source

Expand Down Expand Up @@ -627,7 +627,7 @@ def run(self, plugin=None, plugin_args=None, updatehash=False):
if str2bool(self.config["execution"]["create_report"]):
self._write_report_info(self.base_dir, self.name, execgraph)
runner.run(execgraph, updatehash=updatehash, config=self.config)
datestr = datetime.now(UTC).strftime("%Y%m%dT%H%M%S")
datestr = utcnow().strftime("%Y%m%dT%H%M%S")
if str2bool(self.config["execution"]["write_provenance"]):
prov_base = op.join(self.base_dir, "workflow_provenance_%s" % datestr)
logger.info("Provenance file prefix: %s" % prov_base)
Expand Down
19 changes: 19 additions & 0 deletions nipype/utils/datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Utilities for dates and time
"""

from datetime import datetime as dt
import sys

if sys.version_info >= (3, 11):
from datetime import UTC

def utcnow():
"""Adapter since 3.12 prior utcnow is deprecated,
but not EOLed 3.8 does not have datetime.UTC"""
return dt.now(UTC)

else:
utcnow = dt.utcnow

0 comments on commit cd4bd5b

Please sign in to comment.