-
-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re
is already imported at line 88. This line is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some smaller style things in my opinion.
A question I would have is, what happens if sb. sets a bigger/falsetime_string
greater 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You approach is more elegant. Thank you.
|
||
Examples: | ||
>>> data = bytearray(4) | ||
>>> snap7.util.set_dint(data, 0, '-22:3:57:28.192') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo. Wrong method.
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?