Date and Time calculation simplified with Python

When looking at java garbage collection logs you only get logs with a time stamp in seconds from the start of the JVM. After a few days of running the JVM it is difficult to remember when it started and when is the event your are looking at from. I was looking for a quick way to calculate the time. Python has some nice facilities built-in to help with that task. Here is what I do from the command line to figure out the last time it was started:

[bash]$ python
>>> from datetime import datetime
>>> from datetime import timedelta
>>> datetime(2009,2,9,10,48) – timedelta(seconds=159565)
datetime.datetime(2009, 2, 7, 14, 28, 35)

The datetime object is initialized with the date I want. In this case it was February 9, 2009 at 10:48. The timedelta object is initialized with a number of seconds but it could be days or min.

Very quick and convenient.

Advertisements

Published by

m5c

Java developper that loves photography and good coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s