1

I edited and saved a text file, "fullname" on my Windows 7 computer. I ran the following two lines of code immediately after saving the edits to "fullname", and I expected both of the following lines of code to return almost the same number of seconds since the epoch:

print str(os.path.getmtime(fullname))
print str(time.mktime(t.timetuple()))

The second line of code was borrowed from How to convert a Python datetime object to seconds

The results were not even close:

"1494082110.0"

"1319180400.0"

I would like to know why the results were not close.

My ultimate goal is that I want to know how to generate a float date, matching a calendar date of my choosing, for use in the context of: win32file.SetFileTime(handle, CreatedTime , AccessTime , WrittenTime )

Any help in understanding these issues would be much appreciated.

Community
  • 1
  • 1
Marc B. Hankin
  • 771
  • 3
  • 15
  • 31

1 Answers1

0

You need to compare the current time with the time at which you saved the file. In this code I save a file, then I get the current time in t and display it, then I get the modification time for the file and display that. You may note that the two times differ by less than a half a second.

>>> import datetime
>>> import time
>>> import os
>>> fullname = 'temp.txt'
>>> open('temp.txt', 'w').write('something')
9
>>> t = datetime.datetime.now()
>>> time.mktime(t.timetuple())
1502039202.0
>>> os.path.getmtime(fullname)
1502039187.4629886

I notice too that,

>>> datetime.datetime.fromtimestamp(1319180400)
datetime.datetime(2011, 10, 21, 3, 0)

In other words, that second number in your question yields a date that came before you put your question.

Bill Bell
  • 21,021
  • 5
  • 43
  • 58