PDA

View Full Version : python time with milliseconds



zero-9376
August 29th, 2007, 09:29 AM
Hi, im trying to create a string which i will use to name files, im recording data and want to name the file based on the current time of day. the problem is that i need better than 1 second accuracy so this is what i have done...comments/suggestions please

import time
time_now_sec = time.mktime(time.localtime())
time_now_mic = time.time()
time_str = time.asctime()
milli = time_now_mic - time_now_sec
print time_str,".",milli

please not that i started learning python yesturday

thanks

zero-9376
August 29th, 2007, 10:11 AM
if someone can tell me whether this is correct, or if there is a better way it would be greatly appreciated

pmasiar
August 29th, 2007, 01:18 PM
google fond this: http://mail.python.org/pipermail/python-list/2003-November/235548.html

dwblas
August 30th, 2007, 04:30 AM
Python's datetime is good to milli-seconds on Windohs and micro-seconds on Linux. http://effbot.org/librarybook/datetime.htm
import time
import datetime
start_time = datetime.datetime.now()
time.sleep( 2 )
print "difference =", datetime.datetime.now() - start_time