The time.time() function returns the number of seconds since the epoch, as a float. Note that “the epoch” is defined as the start of January 1st, 1970 in UTC. So the epoch is defined in terms of UTC and establishes a global moment in time. No matter where on Earth you are, “seconds past epoch” (time.time()) returns the same value at the same moment.
Here is some sample output I ran on my computer, converting it to a string as well.
>>> import time
>>> ts = time.time()
>>> ts
1355563265.81
>>> import datetime
>>> datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
'2012-12-15 01:21:05'
>>>
The ts variable is the time returned in seconds. I then converted it to a human-readable string using the datetime library.
Videos
The time.time() function returns the number of seconds since the epoch, as a float. Note that “the epoch” is defined as the start of January 1st, 1970 in UTC. So the epoch is defined in terms of UTC and establishes a global moment in time. No matter where on Earth you are, “seconds past epoch” (time.time()) returns the same value at the same moment.
Here is some sample output I ran on my computer, converting it to a string as well.
>>> import time
>>> ts = time.time()
>>> ts
1355563265.81
>>> import datetime
>>> datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
'2012-12-15 01:21:05'
>>>
The ts variable is the time returned in seconds. I then converted it to a human-readable string using the datetime library.
This is for the text form of a timestamp that can be used in your text files. (The title of the question was different in the past, so the introduction to this answer was changed to clarify how it could be interpreted as the time. [updated 2016-01-14])
You can get the timestamp as a string using the .now() or .utcnow() of the datetime.datetime:
>>> import datetime
>>> print datetime.datetime.utcnow()
2012-12-15 10:14:51.898000
The now differs from utcnow as expected -- otherwise they work the same way:
>>> print datetime.datetime.now()
2012-12-15 11:15:09.205000
You can render the timestamp to the string explicitly:
>>> str(datetime.datetime.now())
'2012-12-15 11:15:24.984000'
Or you can be even more explicit to format the timestamp the way you like:
>>> datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
'Saturday, 15. December 2012 11:19AM'
If you want the ISO format, use the .isoformat() method of the object:
>>> datetime.datetime.now().isoformat()
'2013-11-18T08:18:31.809000'
You can use these in variables for calculations and printing without conversions.
>>> ts = datetime.datetime.now()
>>> tf = datetime.datetime.now()
>>> te = tf - ts
>>> print ts
2015-04-21 12:02:19.209915
>>> print tf
2015-04-21 12:02:30.449895
>>> print te
0:00:11.239980
The -0600 is the offset from Greenwich Mean Time (GMT). As another SO question already says, time.strptime cannot read timezone offsets, though datetime.strftime can generate them.
As explained at the start of the datetime module's documentation, there are two ways of approaching "time" in python, naive or aware. When all you care about is time inside your system, dealing with naive time/datetime objects is fine (in which case you can strip out the offset as alan suggested). When you need to compare the values inside your system with the real world's notion of time, you have to start dealing with that offset.
The easy way to deal with this is just to use python-dateutil. It has a parse function that will do its best to fuzzily match the date string you pass in to multiple formats and return a workable datetime instance that represents its best guess as to what you meant.
>>> from dateutil.parser import parse
>>> parse('17/Dec/2011:09:48:49 -0600', fuzzy=True)
datetime.datetime(2011, 12, 17, 9, 48, 49, tzinfo=tzoffset(None, -21600))
Normally, having software give its "best guess" is a Bad Thing. In this case, it seems justified if your input formats are stable. Dealing with time in software development is hard, just go shopping.
strptime simply matches the modifiers (%d,%b, ...) with the corresponding segments of the string and then converts that matched piece of string to an integer. So in your case, the -0600 only makes it so that your format string matches the input string.
If you want to adjust the time by a specified offset, I would recommend using a datetime object.
>>>s = '17/Dec/2011:09:48:49 -0600'
>>>from datetime import datetime,timedelta
>>>mytime = datetime.strptime(s,"%d/%b/%Y:%H:%M:%S -0600")
>>>dt = timedelta(minutes=6*60) #6 hours
>>>mytime-=dt
>>>print mytime
2011-12-17 03:48:49
>>>print mytime.hour
3
Also note that since str is a builtin, it is generally not advisable to reassign it.