Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
opcua-asyncio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nikola Balog
opcua-asyncio
Commits
75ae5add
Commit
75ae5add
authored
Dec 08, 2015
by
ORD
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #63 from alkor/fix-datetime-conversion
Fix conversion from FILETIME to datetime
parents
56f45306
a243da5f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
9 deletions
+14
-9
opcua/uatypes.py
opcua/uatypes.py
+3
-9
tests/tests.py
tests/tests.py
+11
-0
No files found.
opcua/uatypes.py
View file @
75ae5add
...
...
@@ -23,6 +23,7 @@ UaTypes = ("Boolean", "SByte", "Byte", "Int8", "UInt8", "Int16", "UInt16", "Int3
EPOCH_AS_FILETIME
=
116444736000000000
# January 1, 1970 as MS file time
HUNDREDS_OF_NANOSECONDS
=
10000000
FILETIME_EPOCH_AS_DATETIME
=
datetime
(
1601
,
1
,
1
)
class
UTC
(
tzinfo
):
...
...
@@ -39,7 +40,7 @@ class UTC(tzinfo):
return
timedelta
(
0
)
# method
s copied from
David Buxton <david@gasmark6.com> sample code
# method
copied from
David Buxton <david@gasmark6.com> sample code
def
datetime_to_win_epoch
(
dt
):
if
(
dt
.
tzinfo
is
None
)
or
(
dt
.
tzinfo
.
utcoffset
(
dt
)
is
None
):
dt
=
dt
.
replace
(
tzinfo
=
UTC
())
...
...
@@ -48,14 +49,7 @@ def datetime_to_win_epoch(dt):
def
win_epoch_to_datetime
(
epch
):
(
s
,
ns100
)
=
divmod
(
epch
-
EPOCH_AS_FILETIME
,
HUNDREDS_OF_NANOSECONDS
)
try
:
dt
=
datetime
.
utcfromtimestamp
(
s
)
except
Exception
as
ex
:
#FIXME: find out what kind of exceptin is raised!!!
logger
.
debug
(
"Exception occurred during conversion within 'win_epoch_to_datetime'. %s"
,
ex
)
return
datetime
.
now
()
dt
=
dt
.
replace
(
microsecond
=
(
ns100
//
10
))
return
dt
return
FILETIME_EPOCH_AS_DATETIME
+
timedelta
(
microseconds
=
epch
//
10
)
def
uatype_to_fmt
(
uatype
):
...
...
tests/tests.py
View file @
75ae5add
...
...
@@ -152,11 +152,22 @@ class Unit(unittest.TestCase):
dt
=
ua
.
win_epoch_to_datetime
(
epch
)
self
.
assertEqual
(
now
,
dt
)
# python's datetime has a range from Jan 1, 0001 to the end of year 9999
# windows' filetime has a range from Jan 1, 1601 to approx. year 30828
# let's test an overlapping range [Jan 1, 1601 - Dec 31, 9999]
dt
=
datetime
(
1601
,
1
,
1
)
self
.
assertEqual
(
ua
.
win_epoch_to_datetime
(
ua
.
datetime_to_win_epoch
(
dt
)),
dt
)
dt
=
datetime
(
9999
,
12
,
31
,
23
,
59
,
59
)
self
.
assertEqual
(
ua
.
win_epoch_to_datetime
(
ua
.
datetime_to_win_epoch
(
dt
)),
dt
)
epch
=
128930364000001000
dt
=
ua
.
win_epoch_to_datetime
(
epch
)
epch2
=
ua
.
datetime_to_win_epoch
(
dt
)
self
.
assertEqual
(
epch
,
epch2
)
epch
=
0
self
.
assertEqual
(
ua
.
datetime_to_win_epoch
(
ua
.
win_epoch_to_datetime
(
epch
)),
epch
)
def
test_equal_nodeid
(
self
):
nid1
=
ua
.
NodeId
(
999
,
2
)
nid2
=
ua
.
NodeId
(
999
,
2
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment