user1467267
user1467267

Reputation:

Python get micro time from certain date

I was browsing the Python guide and some search machines for a few hours now, but I can't really find an answer to my question.

I am writing a switch where only certain files are chosen to be in the a file_list (list[]) when they are modified after a given date.

In my loop I do the following code to get its micro time:

file_time = os.path.getmtime(path + file_name)

This returns me a nice micro time, like this: 1342715246.0

Now I want to compare if that time is after a certain date-time I give up. So for testing purposes, I used 1790:01:01 00:00:00.

# Preset (outside the class/object)
start_time = '1970-01-01 00:00:00'
start_time = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
start_time = start_time.strftime("%Y-%m-%d %H:%M:%S")

# In my Method this check makes sure I only append files to the list
#   that are modified after the given date
if file_time > self.start_time:
    file_list.append(file_name)

Of course this does not work, haha :P. What I'm aiming for is to make a micro time format from a custom date. I can only find ClassMethods online that make micro time formats from the current date.

Upvotes: 0

Views: 824

Answers (2)

jfocht
jfocht

Reputation: 1754

Take a look at Python datetime to microtime. You need the following snippet:

def microtime(dt):
    time.mktime(dt.timetuple()) + dt.microsecond / 1000000.0 

Upvotes: 2

smont
smont

Reputation: 1348

You can compare datetime.datetime objects by themselves. If you keep start_time as a datetime.datetime object and make file_time a datetime.datetime object, you can successfully do file_time > self.start_time

Take a look at the supported operators in the documentation

Upvotes: 0

Related Questions