0

I'm trying to convert an ordinal dateTime value into a value that can be accepted by os.utime()

My date has the format: 201642322295

(which is: year=2016, month=4, day=23, hour=22, minute=29, second=5)

However, the method won't accept this and I'm not sure how to/ what to convert it into. I've tried converting it into a datetime also but this does not work.

The code segment:

s = int(self.newTime)
date = datetime(year=s[0:4], month=s[4], day=s[5:2], hour=s[8:2], minute=s[10:2], second=s[12])
os.utime(self.fileName, (date,date))

(I had tried using just the ordinal format, which modifies the datetime of the file but is not at all correct)

edit: This is not the same as 'python converting datetime to be used in os.utime' because it's using a completely different format

Jonathan H
  • 141
  • 1
  • 12

1 Answers1

1

os.utime expects values in the form of integers representing a untix timestamp.

s = int(self.newTime)
date = datetime(year=s[0:4], month=s[4], day=s[5:2], hour=s[8:2], minute=s[10:2], second=s[12])
utime = time.mktime(date.timetuple())
os.utime(self.fileName, (utime, utime))
Community
  • 1
  • 1
Hamms
  • 5,016
  • 21
  • 28