Menu

#16 ValueError, "can't format dates this early"

v1.0 (example)
open
nobody
None
5
2018-03-30
2018-03-30
No

On some OPC-Servers (in my case FlowChief OPC) if using a group in the read() request the first read ends with a ValueError "can't format dates this early". A second read() on the same group is then fine and returns useful values.

Not that I used the 1.2.0 code-base but also looked up the situation in 1.3.1 code (see later comments on that)

The ValueError exception comes from the following statement in iread()
timestamp = str(tag_time[tag]) --- line 560

I debugged that in ipython and found that the reason for that is that the reported tag_time has the following value <pytime:01 01="" 1601="" 00:00:00="" am=""> (probably just an non initialized timestamp default of that OPC-server)
When the str() method is applied on that PyTime object the exception is raised.
You can look this up in https://github.com/kovidgoyal/pywin32/blob/master/win32/src/PyTime.cpp (line 174).
I am not an expert in PyTime cannot tell why this kind of exception is raised when the year is < 1900.</pytime:01>

In the 1.3.1 code-base this ValueError exception is captured and a None is returned as timestamp.
This is somehow OK an I can on application level then handle that situation as a special case.

I found an other way that allows the system to handle the situation in a more normal way:

  • PyTime object has attributes like year, month, day, ... to access the integer values of the date items.
  • using an own string conversion will just return me a correct formated date that I can use later.
    "{0.month:02}/{0.day:02}/{0.year:02} {0.hour:02}:{0.minute:02}:{0.second:02}".format(tag_time[tag])
    returns "01/01/1601 00:00:00" as a string based on that PyTime object.

See the attached patch file that proposes a patch for the 1.3.1 code-base where also variable values of type pywintypes.TimeType could now be using dates earlier than year 1900.

Best
Georges

1 Attachments

Discussion


Log in to post a comment.

MongoDB Logo MongoDB