Bottom Page

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

Function excerpt:

format = '%H:%M:%S.%f'
        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
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 -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Do a check so it work in both cases or more if needed.
from datetime import datetime

def date_check(d):
        if '.' in d:
            return datetime.strptime(d, '%H:%M:%S.%f')
            return datetime.strptime(d, '%H:%M:%S')
    except ValueError as error:
        # Can log error if want it to continue on error
        # Not ideal but <pass> will also continue
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)
И guess the problem (missing microseconds) is in lampaTV.AutoON - check you have microseconds there
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')
>>> 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 object --> 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

Life of Brian: Conjugate the verb, "to go" !
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.
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:
        zmiennaON = datetime.datetime.strptime(str(aktualnie), format) - datetime.datetime.strptime(lampaTV.AutoON, format)
    except ValueError as e:
        print('Time Error1:', e)
        timeON = datetime.datetime.strptime(str(aktual), format) - datetime.datetime.strptime(
This is just a workaround, not a solution, but I'll try.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Definition of .format? (Newbie question) Jan_97 4 234 Jul-13-2019, 12:49 PM
Last Post: Jan_97
  changing format to Int Scott 3 274 Jun-19-2019, 06:33 AM
Last Post: ODIS
  Date format issue the_dude 11 739 Jun-07-2019, 06:27 AM
Last Post: snippsat
  Changing Number Format moby 4 233 May-24-2019, 11:04 PM
Last Post: snippsat
  changing { and } in str.format() Skaperen 10 520 May-16-2019, 05:07 AM
Last Post: Skaperen
  Return in correct json format UtiliseIT 3 344 May-13-2019, 11:24 AM
Last Post: snippsat
  error: = "NPC{}".format(num_of_NPCs + 1) Exsul 1 151 May-03-2019, 08:56 PM
Last Post: ichabod801
  Why does time.sleep(wait) add time in python3 ? gerardg 4 505 Apr-28-2019, 06:26 PM
Last Post: gerardg
  Format for elif sunilramakumar 4 361 Apr-08-2019, 05:21 PM
Last Post: Yoriz
  Convert SAS Dates Format in a loop MohanReddy 2 284 Apr-02-2019, 10:31 AM
Last Post: scidam

Forum Jump:

Users browsing this thread: 1 Guest(s)