Dusty Boshoff
Dusty Boshoff

Reputation: 1061

Python timestamps with time()

I have a few timestamps in a database that I would like to trim down to only about 10 timestamps, from most latest to oldest. All the timestamps in the DB were made using time() in python.

Create timestamp:

_timestamp = time.time()

Timestamps:

1435652632.92778
1435652633.01
1435652633.07
1435652633.13
1435652642.71371
1435652642.77
1435652675.13
1435652675.22
1435652717.74
1435652735.11
1435652735.16
1435652735.24

How can I ask python to remove the oldest timestamp?

I currently convert the timestamp to readable format using the below string in my script.

print datetime.datetime.fromtimestamp(1434615010.27858)
>>2015-06-18 08:10:10.278580

I have no code yet to remove the oldest timestamps, however I would appreciate some help with this please.

Upvotes: 1

Views: 213

Answers (2)

Arturs Vancans
Arturs Vancans

Reputation: 4640

This is an epoch timestamp which is simply seconds passed since 1970-01-01T00:00:00Z.

In you case, sort the timestmaps and take the 10 top elements.

timestmaps = [1435652632.92778, 1435652633.01, 1435652633.07]
timestmaps.sort(reverse=True)
latestTimestamps = timestmaps[:10]

Upvotes: 4

Raphael Amoedo
Raphael Amoedo

Reputation: 4475

Not sure if it's what you want, but you can do this:

timestamps = [1435652632.92778,1435652633.01,1435652633.07,1435652633.13,1435652642.71371,1435652642.77,1435652675.13,1435652675.22,1435652717.74,1435652735.11,1435652735.16,1435652735.24]
timestamps.remove(min(timestamps)) #Remove the minimum value from the list

Upvotes: 0

Related Questions