ThinkBonobo
ThinkBonobo

Reputation: 16525

How do you change Dropbox client_mtime to unix time (epoch timestamp) in Python

I want to compare the client_mtime ('modified' does not work for my application) from a file's metadata to to the time obtained using

os.path.getctime(myFilePath)

The above gives me a unix timestamp like 1400687774.48, while the client_mtime gives me a time stamp formatted like so: 'Wed, 21 May 2014 15:59:25 +0000'

I looked around and found that the Dropbox format for time is as follows:

"EEE, dd MMM yyyy HH:mm:ss Z"

What's the simplest way to either convert the dropbox client_mtime to a unixtime stamp or directly compare the two and see which one is earlier?

Edit: I am using python 2.6

Upvotes: 0

Views: 281

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180461

Convert timestamp to datetime object and do the same with the dropbox time:

Using Python 2 %z does not seem to work so you can include the +0000 when formatting.

In [6]: from datetime import datetime

In [7]: t = datetime.fromtimestamp(1400687774.48)

In [8]: t1 =datetime.strptime('Wed, 21 May 2014 15:59:25 +0000', '%a, %d %b %Y %H:%M:%S +0000')

Out[4]: datetime.datetime(2014, 5, 21, 16, 56, 14, 480000)

In [5]: t1
Out[5]: datetime.datetime(2014, 5, 21, 15, 59, 2)

In [6]: t
Out[6]: datetime.datetime(2014, 5, 21, 16, 56, 14, 480000)    

In [7]: max(t,t1)
Out[7]: datetime.datetime(2014, 5, 21, 16, 56, 14, 480000)

In [7]: min(t,t1)
Out[7]: datetime.datetime(2014, 5, 21, 15, 59, 25)

If you are using python 3 %z works.

In [4]: t1
Out[4]: datetime.datetime(2014, 5, 21, 15, 59, 25, tzinfo=datetime.timezone.utc)

Upvotes: 1

Related Questions