Skip to content

Commit 2ac182e

Browse files
committed
feat: replace datetime.utcnow and datetime.utcfromtimestamp with Dirac own implementation
1 parent 40386a5 commit 2ac182e

103 files changed

Lines changed: 307 additions & 203 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dirac-common/src/DIRACCommon/Core/Utilities/TimeUtilities.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import sys
2424
import time
2525

26+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
27+
2628
# Some useful constants for time operations
2729
microsecond = datetime.timedelta(microseconds=1)
2830
second = datetime.timedelta(seconds=1)
@@ -111,7 +113,7 @@ def fromEpoch(epoch):
111113
epoch /= 1000**2
112114
elif epoch > 10**11: # milliseconds
113115
epoch /= 1000
114-
return datetime.datetime.utcfromtimestamp(epoch)
116+
return DiracTime.utcfromtimestamp(epoch)
115117

116118

117119
def toString(myDate=None):

src/DIRAC/AccountingSystem/Agent/NetworkAgent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
:caption: NetworkAgent options
99
1010
"""
11+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
1112
from datetime import datetime
1213

1314
from DIRAC import S_OK, S_ERROR, gLogger
@@ -165,7 +166,7 @@ def processMessage(self, headers, body):
165166
timestamps = sorted(body["datapoints"])
166167
for timestamp in timestamps:
167168
try:
168-
date = datetime.utcfromtimestamp(float(timestamp))
169+
date = DiracTime.utcfromtimestamp(float(timestamp))
169170

170171
# create a key that allows to join packet-loss-rate and one-way-delay
171172
# metrics in one network accounting record

src/DIRAC/AccountingSystem/Client/Types/BaseAccountingType.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" Within this module is defined the class from which all other accounting types are defined
22
"""
33

4+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
45
import datetime
56

67
from DIRAC import S_OK, S_ERROR
@@ -55,7 +56,7 @@ def setStartTime(self, startTime=False):
5556
By default use now
5657
"""
5758
if not startTime:
58-
self.startTime = datetime.datetime.utcnow()
59+
self.startTime = DiracTime.utcnow()
5960
else:
6061
self.startTime = startTime
6162

@@ -65,15 +66,15 @@ def setEndTime(self, endTime=False):
6566
By default use now
6667
"""
6768
if not endTime:
68-
self.endTime = datetime.datetime.utcnow()
69+
self.endTime = DiracTime.utcnow()
6970
else:
7071
self.endTime = endTime
7172

7273
def setNowAsStartAndEndTime(self):
7374
"""
7475
Set current time as start and end time of the report
7576
"""
76-
self.startTime = datetime.datetime.utcnow()
77+
self.startTime = DiracTime.utcnow()
7778
self.endTime = self.startTime
7879

7980
def setValueByKey(self, key, value):

src/DIRAC/AccountingSystem/DB/AccountingDB.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
12
"""Frontend to MySQL DB AccountingDB"""
23

34
import datetime
@@ -50,7 +51,7 @@ def __init__(self, name="Accounting/AccountingDB", readOnly=False, parentLogger=
5051
minute=random.randint(0, 59), # nosec B311
5152
second=random.randint(0, 59), # nosec B311
5253
)
53-
lcd = datetime.datetime.utcnow()
54+
lcd = DiracTime.utcnow()
5455
lcd.replace(hour=self.__compactTime.hour + 1, minute=0, second=0)
5556
self.__lastCompactionEpoch = TimeUtilities.toEpoch(lcd)
5657
self.__registerTypes()
@@ -69,7 +70,7 @@ def autoCompactDB(self):
6970

7071
def __periodicAutoCompactDB(self):
7172
while self.autoCompact:
72-
nct = datetime.datetime.utcnow()
73+
nct = DiracTime.utcnow()
7374
if nct.hour >= self.__compactTime.hour:
7475
nct = nct + datetime.timedelta(days=1)
7576
nct = nct.replace(

src/DIRAC/AccountingSystem/Service/ReportGeneratorHandler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
:dedent: 2
77
:caption: ReportGenerator options
88
"""
9+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
910
import os
1011
import datetime
1112

@@ -76,13 +77,13 @@ def __checkPlotRequest(self, reportRequest):
7677
return S_ERROR("Value Error")
7778
if lastSeconds < 3600:
7879
return S_ERROR("lastSeconds must be more than 3600")
79-
now = datetime.datetime.utcnow()
80+
now = DiracTime.utcnow()
8081
reportRequest["endTime"] = now
8182
reportRequest["startTime"] = now - datetime.timedelta(seconds=lastSeconds)
8283
else:
8384
# if enddate is not there, just set it to now
8485
if not reportRequest.get("endTime", False):
85-
reportRequest["endTime"] = datetime.datetime.utcnow()
86+
reportRequest["endTime"] = DiracTime.utcnow()
8687
# Check keys
8788
for key, keyType in self.__reportRequestDict.items():
8889
if key not in reportRequest:

src/DIRAC/ConfigurationSystem/Client/CSAPI.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Most of these functions can only be done by administrators
44
"""
55

6+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
67
import datetime
78

89
from DIRAC import S_ERROR, S_OK, gConfig, gLogger
@@ -104,7 +105,7 @@ def initialize(self):
104105
self.__csMod = Modificator(
105106
self.__rpcClient,
106107
"%s - %s - %s"
107-
% (self.__userGroup, self.__userDN, datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")),
108+
% (self.__userGroup, self.__userDN, DiracTime.utcnow().strftime("%Y-%m-%d %H:%M:%S")),
108109
)
109110
retVal = self.downloadCSData()
110111
if not retVal["OK"]:

src/DIRAC/ConfigurationSystem/private/ConfigurationData.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" ConfigurationData module is the base for cfg files management
22
"""
33

4+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
45
import os.path
56
import zlib
67
import zipfile
@@ -201,7 +202,7 @@ def deleteOptionInCFG(self, path, cfg=False):
201202
self.sync()
202203

203204
def generateNewVersion(self):
204-
self.setVersion(str(datetime.datetime.utcnow()))
205+
self.setVersion(str(DiracTime.utcnow()))
205206
self.sync()
206207
gLogger.info(f"Generated new version {self.getVersion()}")
207208

@@ -326,7 +327,7 @@ def dumpRemoteCFGToFile(self, fileName):
326327
def __backupCurrentConfiguration(self, backupName):
327328
configurationFilename = f"{self.getName()}.cfg"
328329
configurationFile = os.path.join(DIRAC.rootPath, "etc", configurationFilename)
329-
today = datetime.datetime.utcnow().date()
330+
today = DiracTime.utcnow().date()
330331
backupPath = os.path.join(self.getBackupDir(), str(today.year), "%02d" % today.month)
331332
mkDir(backupPath)
332333
backupFile = os.path.join(backupPath, configurationFilename.replace(".cfg", f".{backupName}.zip"))

src/DIRAC/ConfigurationSystem/private/Modificator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" This is the guy that actually modifies the content of the CS
22
"""
3+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
34
import datetime
45
import difflib
56
import zlib
@@ -29,7 +30,7 @@ def loadCredentials(self):
2930
self.commiterId = "{}@{} - {}".format(
3031
credDict["username"],
3132
credDict["group"],
32-
datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
33+
DiracTime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
3334
)
3435
return retVal
3536
return retVal

src/DIRAC/ConfigurationSystem/scripts/dirac_admin_sort_cs_sites.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# File : dirac-admin-sort-cs-sites
44
# Author : Matvey Sapunov
55
########################################################################
6+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
67
"""
78
Sort site names at CS in "/Resources" section. Sort can be alphabetic or by country postfix in a site name.
89
Alphabetic sort is default (i.e. LCG.IHEP.cn, LCG.IHEP.su, LCG.IN2P3.fr)
@@ -112,7 +113,7 @@ def main():
112113
gLogger.notice("Nothing to do, site names are already sorted")
113114
DIRACExit(0)
114115

115-
timestamp = str(datetime.utcnow())
116+
timestamp = str(DiracTime.utcnow())
116117
stamp = f"Site names are sorted by {Script.scriptName} script at {timestamp}"
117118
cs.setOptionComment("/Resources/Sites", stamp)
118119

src/DIRAC/Core/Base/AgentModule.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Base class for all agent modules
33
"""
4+
from DIRAC.Core.Utilities.TimeUtilities import DiracTime
45
import datetime
56
import importlib.metadata
67
import inspect
@@ -233,7 +234,7 @@ def am_checkStopAgentFile(self):
233234
def am_createStopAgentFile(self):
234235
try:
235236
with open(self.am_getStopAgentFile(), "w") as fd:
236-
fd.write(f"Dirac site agent Stopped at {str(datetime.datetime.utcnow())}")
237+
fd.write(f"Dirac site agent Stopped at {str(DiracTime.utcnow())}")
237238
except Exception as err:
238239
self.log.info(f"Failed to write stop file: {str(err)}")
239240

0 commit comments

Comments
 (0)