Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

python - ValueError: time data '6.9141387939453125e-06' does not match format '%H/%M/%S'

I have a function

start_time = time.time()

That is producing some wild numbers i.e.

start time:  1611368981.2445016

That is causing the following error

ValueError: time data '6.9141387939453125e-06' does not match format '%H/%M/%S'

Why is time.time() producing such wild times and how to get it into normal format?

lines of code in question:

if plant_warning_flag == 0:
        start_time = time.time() #start time flag for plant temperature warning
        plant_warning_flag = 1

elapsed_time = time.time() - start_time
print("start time: ", start_time)
print('my elapsed time: ', elapsed_time)
newelaptime = time.strptime(str(elapsed_time), "%H/%M/%S")
newmthactime = pd.to_datetime(maxtime_heatac.strip(), format='%H:%M:%S')
if newcread > plant_warning + critical_threshold_ac:
        session = requests.Session()
        session.post(acserver_url,headers=headers,data=payload)

elif newlaptime > newmthactime:
        payload = {'on1':'4000'}
        session = requests.Session()
        print('peek-a-boo')

how do I retrieve time.time() is regular date format?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your error seem to be coming from this line:

newelaptime = time.strptime(str(elapsed_time), "%H/%M/%S")

because you trying to convert Unix time (number of secs of the Unix Epoch - counted from 1.01.1970) which is number - into string - and then using strptime trying to convert this into time object. ...which goes into error - because strptime converts time from human-readable-string into time-object.

Unix Time is great for operators and calculating time ranges but not good for reading by human :) To get human readable value - try something like this:

datetime.datetime.utcfromtimestamp(YOUR_UNIX_TIMESTAMP).strftime('%Y-%m-%dT%H:%M:%SZ')

it will give you a string 'YYYY-mm-dd H:M:Secs' ...shorter version like this:

datetime.datetime.utcfromtimestamp(YOUR_UNIX_TIMESTAMP)

...will give you a time object - which is great when you want to operate with (less mathematic / more calendar) units like months years etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...