pycomm3 icon indicating copy to clipboard operation
pycomm3 copied to clipboard

[BUG] - get_plc_time does not account for timezones when converting to datetime

Open ronytigo opened this issue 2 years ago • 4 comments

Pre-checks

  • [x] running latest version
  • [x] enabled logging
  • [x] consulted the docs

Description time received ignores the timezone of the plc. For example, the plc timezone is GMT + 3 and the time is set to 8pm, the code will receive 8pm instead of 5pm which is the gmt time. If I go to https://www.epochconverter.com and enter the microseconds field I get from plc.get_plc_time().value['microseconds'] it shows 8pm in GMT and 11pm in local (the correct timestamp should show 5pm in GMT and 8pm in local timezone).

Target PLC Rockwell Automation/Allen-Bradley Model: 1756-L83E/B Firmware Revision: 33.11

Code Sample Minimal reproduceable code sample

LogixDriver(url, init_tags=False, init_program_tags=False) as plc:
    plc.get_plc_time().value['microseconds']

Additional context

  • if reading/writing a tag, describe it's type/structure/etc
  • attach any relevant L5X or data files
  • paste/attach logging output

ronytigo avatar May 01 '23 13:05 ronytigo

You're right, this method reads attribute 11 from the WallClockTime object, which is the unix local time. Attribute 6 is the utc unix time, which I think should fix the issue. I don't do a lot with datetime objects in Python, but I think passing the utc timezone when constructing the datetime object should also be done.

If you want to create a PR for this issue, you may and I can help with it, else I'll try to get it fixed soon.

ottowayi avatar May 16 '23 13:05 ottowayi

I implemented the fix for this, was simple modification of the get_plc_time method, passing in the time zone like you suggested and using attribute 6. Do you prefer I perform the fix from the main branch? I am new to GitHub but will attempt to do the PR and commit push.

jbolognini avatar Oct 14 '23 01:10 jbolognini

sorry for the late response, targeting master is fine. I may change it into a dev branch before merging, but don't worry about that. The basic steps would be to fork my repo (the Fork button at the top of the page), then create a branch for this fix, check that branch out in your IDE, make the code changes, commit and push those changes to your new branch, then in github create a new pull request back to my repo targeting the master branch.

ottowayi avatar Nov 03 '23 18:11 ottowayi

ok i did as you instructed, thanks for the help. pull request pending.

jbolognini avatar Nov 22 '23 04:11 jbolognini