Commit 0525da39347a5282dbb8545d03faf3fcca124db1

Authored by Paulo Henrique Junqueira Amorim
1 parent ef8fe454

ENH: Date and Time Formated

Showing 1 changed file with 29 additions and 16 deletions   Show diff stats
invesalius/reader/dicom.py
1 1 #---------------------------------------------------------------------
2 2 # Software: InVesalius Software de Reconstrucao 3D de Imagens Medicas
3   -
4 3 # Copyright: (c) 2001 Centro de Pesquisas Renato Archer
5 4 # Homepage: http://www.softwarepublico.gov.br
6 5 # Contact: invesalius@cenpra.gov.br
... ... @@ -70,6 +69,7 @@ INFO_KEYS = \
70 69  
71 70 import vtkgdcm
72 71 import gdcm
  72 +import time
73 73  
74 74 class Parser():
75 75 """
... ... @@ -90,7 +90,7 @@ class Parser():
90 90 def __init__(self):
91 91 self.filename = ""
92 92 self.vtkgdcm_reader = vtkgdcm.vtkGDCMImageReader()
93   -
  93 +
94 94 def GetAcquisitionDate(self):
95 95 """
96 96 Return string containing the acquisition date using the
... ... @@ -103,10 +103,7 @@ class Parser():
103 103 date = self.vtkgdcm_reader.GetMedicalImageProperties()\
104 104 .GetAcquisitionDate()
105 105 if (date):
106   - year = date[0:4]
107   - month = date[4:6]
108   - day = date[6:8]
109   - return day + "/" + month + "/" + year
  106 + return self.__format_date(date)
110 107 return ""
111 108  
112 109 def GetAcquisitionNumber(self):
... ... @@ -149,10 +146,7 @@ class Parser():
149 146 time = self.vtkgdcm_reader.GetMedicalImageProperties()\
150 147 .GetAcquisitionTime()
151 148 if (time):
152   - hh = time[0:2]
153   - mm = time[2:4]
154   - ss = time[4:6]
155   - return hh + ":" + mm + ":" + ss
  149 + return self.__format_time(time)
156 150 return ""
157 151  
158 152 def GetPatientAdmittingDiagnosis(self):
... ... @@ -1055,10 +1049,7 @@ class Parser():
1055 1049 date = self.vtkgdcm_reader.GetMedicalImageProperties()\
1056 1050 .GetPatientBirthDate()
1057 1051 if (date):
1058   - year = date[0:4]
1059   - month = date[4:6]
1060   - day = date[6:8]
1061   - return day + "/" + month + "/" + year
  1052 + self.__format_date(date)
1062 1053 return ""
1063 1054  
1064 1055  
... ... @@ -1421,9 +1412,31 @@ class Parser():
1421 1412 if ds.FindDataElement(tag):
1422 1413 data = str(ds.GetDataElement(tag).GetValue())
1423 1414 if (data):
1424   - return data
  1415 + return self.__format_time(data)
1425 1416 return ""
1426 1417  
  1418 + def __format_time(self,value):
  1419 + if (len(value.split(".")) > 1):
  1420 + data = time.strptime(value, "%H.%M.%S")
  1421 + elif(len(value.split(":")) > 1):
  1422 + data = time.strptime(value, "%H:%M:%S")
  1423 + else:
  1424 + data = time.strptime(value, "%H%M%S")
  1425 + return time.strftime("%H:%M:%S",data)
  1426 +
  1427 + def __format_date(self, value):
  1428 +
  1429 + if (len(value.split(".")) > 1):
  1430 + data = time.strptime(value, "%D.%M.%Y")
  1431 + elif(len(value.split("//")) > 1):
  1432 + data = time.strptime(value, "%D/%M/%Y")
  1433 + else:
  1434 + data = time.strptime(value, "%Y%M%d")
  1435 +
  1436 + print time.strftime("%d/%M/%Y",data)
  1437 +
  1438 + return time.strftime("%d/%M/%Y",data)
  1439 +
1427 1440 def GetAcquisitionTime(self):
1428 1441 """
1429 1442 Return the acquisition time.
... ... @@ -1434,7 +1447,7 @@ class Parser():
1434 1447 if ds.FindDataElement(tag):
1435 1448 data = str(ds.GetDataElement(tag).GetValue())
1436 1449 if (data):
1437   - return data
  1450 + return self.__format_time(data)
1438 1451 return ""
1439 1452  
1440 1453  
... ...