Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Incorrect time format
#1
Hello,
I have a problem with the time format.

Function excerpt:

format = '%H:%M:%S.%f'
aktual=datetime.datetime.now().time()
 
try:
        timeON = datetime.datetime.strptime(str(aktual), format) - datetime.datetime.strptime(lampaTV.AutoON, format)
except ValueError as e:
        print('Time error1:', e)
This function is called about 4 times per second and works well but after 2-4 days it throws an error e.g.
Quote:ValueError("time data '20:42:11' does not match format '%H:%M:%S.%f'",))

I can't deal with it for a long time Confused
Reply
#2
Well, it would appear that occasionally the microseconds are not being provided. It that case use a try/except block (which you seem to have already set up). In the case of an error, try the same thing but without the '.%f'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Do a check so it work in both cases or more if needed.
Example:
from datetime import datetime

def date_check(d):
    try:
        if '.' in d:
            return datetime.strptime(d, '%H:%M:%S.%f')
        else:
            return datetime.strptime(d, '%H:%M:%S')
    except ValueError as error:
        print(error)
        # Can log error if want it to continue on error
        # Not ideal but <pass> will also continue
 
Test:
d = '20:42:11.00567' 
>>> date_check(d)
datetime.datetime(1900, 1, 1, 20, 42, 11, 5670)
>>> 
>>> d = '20:42:11'
>>> date_check(d)
datetime.datetime(1900, 1, 1, 20, 42, 11)
Reply
#4
И guess the problem (missing microseconds) is in lampaTV.AutoON - check you have microseconds there
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Do not take it too seriously: 'format' is built-in function name and by overwriting it you live in state of sin and deserve whatever happens to you.

In more serious note:

>>> format(42, 'b')
101010
>>> format = '%H:%M:%S.%f'                                                
>>> format(42, 'b')
/.../
TypeError: 'str' object is not callable
It's late evening I am probably not the brightest, but I observe:

datetime.datetime.now() --> datetime.datetime object
datetime.datetime.now().time --> datetime.time object

From datetime.datetime object you create datetime.time object, then convert this object into string and then parse time from string and get again datetime.datetime object. As at the end you get same object type you started with I have a question - are all these conversion necessary?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
This is something like a timer. Unfortunately, I understand date / time variable in Python. Actually, I can only download a variable that records the entire date and time together - I haven't found another solution.

Naturally, my variables are saved in the correct format.
AutoON='21:00:00.0000'
The problem is that Python gets the current time in the wrong format.

Getting time without .%F is pointless, because I can't compare it with my variables in this format anyway.

Yesterday I came up with such (stupid?) idea:
    aktual=datetime.datetime.now().time()
    try:
        zmiennaON = datetime.datetime.strptime(str(aktualnie), format) - datetime.datetime.strptime(lampaTV.AutoON, format)
    except ValueError as e:
        print('Time Error1:', e)
        aktual=datetime.datetime.now().time()
        timeON = datetime.datetime.strptime(str(aktual), format) - datetime.datetime.strptime(
This is just a workaround, not a solution, but I'll try.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python date format changes to date & time 1418 4 518 Jan-20-2024, 04:45 AM
Last Post: 1418
  Code is returning the incorrect values. syntax error 007sonic 6 1,137 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  What time format is this? wrybread 3 2,003 Jun-15-2022, 02:46 PM
Last Post: snippsat
  error 1102 (42000) incorrect database name 's' Anldra12 4 1,666 Jun-08-2022, 09:00 AM
Last Post: Anldra12
  openpyxl incorrect delete rows VladislavM 6 4,032 Jul-19-2021, 08:54 AM
Last Post: VladislavM
  Incorrect Type Error milkycow 4 2,829 Jun-25-2021, 06:04 AM
Last Post: milkycow
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,204 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  Why is 0.1 * 0.2 arithmetically incorrect? Pedroski55 2 2,180 Nov-25-2020, 12:01 AM
Last Post: snippsat
  ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' rajesh3383 4 14,202 Sep-03-2020, 08:22 PM
Last Post: buran
  how to retain time format in df.to_csv Mekala 2 3,072 Aug-07-2020, 07:04 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020