get timestamp in python
November 28th, 2007 by Lawrence David
to insert a timestamp into your python code:
import time
….
print time.localtime()

November 28th, 2007 by Lawrence David
to insert a timestamp into your python code:
import time
….
print time.localtime()
To get the timestamp from python you should do this morom.
import time
print time.time()
Wow thanks douchenozzle. time.time() gives a /super/ useful return value…. wait, no. At least time.localtime() gives somewhat useful values.
How is a unix timestamp not useful?
Real coders just get a hint where to search. And not follow blindly what they find on the net.
Wow, you all are a bunch of socially inept pricks- Grow up, or don’t even bother replying with your worthless remarks..
As for the answer to this question, the following is probably the best solution- Especially if you’re planning on storing the timestamp possibly to be interacted with by other services or languages:
# Output: 1288221999.43
import time
print time.time()
Otherwise, here are some other solutions as well:
# Output: 2010-10-27 19:29:48.401560
import datetime
print datetime.datetime.now()
# Output: 2010-10-27
import datetime
print datetime.date.today()
Thanks for the helpful remarks Joshua.
Okay, so, why is it if I do something like, say
import time
timenow = time.time().tm_hour, time.time().tm_min, time.time().tm_sec
print timenow
I get
(11, 45, 31)
for instance,
and a minute later, when I do
print timenow
I get
(11, 45, 31)
as if the clock has stopped.
These datetime, and time functions in python are striking me as nearly useless…Maybe I’m spoiled, but in bash, tcl, or php, when I tell a script to give the time, I can get them to give the time at the moment when I ask it to, not the time 5 minutes ago when it was first asked.
They will all also print the time in a human readable fashion with relatively little effort.
(bash – date -d %I:%M:%S
php – date(h.i.s), etc.)
I don’t know wny ESR says python is a good, easy place to start. Tcl was a lot simpler.
Ugh… Tony, if you store something in a variable, the variable isn’t going to change by itself. time() returns the time as it is the moment the function is called. You need to call the time() function again to get the current time value. In PHP, if you store date(“H:i:s”); return value in $date and print it out as many times as you’d like, the value printed will be the same, too.
You probably didn’t bother to look at the python’s time or datetime module docs, since you would’ve most likely noticed that there’s an equivalent for “date -d %I:%M:%S” and date(“H:i:s”) there: time.strftime(“%H:%M:%S”). Similar method exists for the datetime object, if you wanted to use it instead.
Also, remember that in python you can always decide the name under which something is imported into the local namespace. So you could do something like the example below, if remembering and/or typing out strftime seems awkward to you.
from time import strftime as date
print date(‘%Y-%m-%d %H:%M:%S’)
Tony, i hope you were kidding. time.time() is returning a float, not a time Object reference. this is a base concept for every oop language.
This thread is absolutely HILARIOUS!
#Output is “hilarious”
import python
print(“hilarious”)()()()()()
Timestamps predate objects, Tony was correct. All of these entries are technically timestamps.
Jordan, you have nothing better to say, so just keep your quips to yourself.
sudo apt-get install “IM SO EDGY HURR DURR I FIT IN”
Just stop