add get_time and set_time in util#308
Conversation
|
first impression is that it looks good, thanks. there is only an issue with code style, as you can see in the github actions build: |
|
Hello @gijzelaerr, |
swamper123
left a comment
There was a problem hiding this comment.
Some smaller style things in my opinion.
A question I would have is, what happens if sb. sets a bigger/false time_string greater than 4 Bytes. Would it break succesfuly?
| >>> data | ||
| bytearray(b'\x8d\xda\xaf\x00') | ||
| """ | ||
| import re |
There was a problem hiding this comment.
re is already imported at line 88. This line is not needed.
There was a problem hiding this comment.
Some smaller style things in my opinion.
A question I would have is, what happens if sb. sets a bigger/falsetime_stringgreater than 4 Bytes. Would it break succesfuly?
Good catch. I didn't notice that issue. I will modify this and make a new pull request.
| minutes = seconds // 60 | ||
| hours = minutes // 60 | ||
| days = hours // 24 | ||
| time_str = str(days * sign) + ":" + str(hours % 24) + ":" + str(minutes % 60) + ":" + str(seconds % 60) + "." + str(milli_seconds) |
There was a problem hiding this comment.
This string concatination style still works, but is a bit outdated (but not wrong). An f-string equivalent would be sth. like this:
time_str = f"{days * sign!s}):{hours % 24!s}:{minutes % 60}:{seconds % 60!s}.{milli_seconds!s}")
|
regarding the pending CI build, you can ignore that, it just hangs sometimes. |
| formatstring = '{:0%ib}' % bits | ||
| byte_hex = hex(int(formatstring.format(time_int), 2)).split('x')[1] | ||
| bytes_array = bytes.fromhex(byte_hex) |
There was a problem hiding this comment.
Are these conversions necessary? Int has a method .to_bytes() that does the same. Or it might be better to use a struct.
There was a problem hiding this comment.
You approach is more elegant. Thank you.
|
|
||
| Examples: | ||
| >>> data = bytearray(4) | ||
| >>> snap7.util.set_dint(data, 0, '-22:3:57:28.192') |
|
This version has one more problem. If the time_string is '-0:0:0:0.1', the result bytearray is not 0xffffffff, but 0x00000001. |
Hello,
I have added the new functions in util. Could someone take a look at it?