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
51c2c510
Commit
51c2c510
authored
Dec 22, 2022
by
Alexander Schrode
Committed by
oroulet
Dec 29, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
truncate datetime
parent
5bf2a3ff
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
6 deletions
+25
-6
asyncua/ua/uatypes.py
asyncua/ua/uatypes.py
+22
-6
tests/test_unit.py
tests/test_unit.py
+3
-0
No files found.
asyncua/ua/uatypes.py
View file @
51c2c510
...
...
@@ -43,6 +43,8 @@ EPOCH_AS_FILETIME = 116444736000000000 # January 1, 1970 as MS file time
HUNDREDS_OF_NANOSECONDS
=
10000000
FILETIME_EPOCH_AS_DATETIME
=
datetime
(
1601
,
1
,
1
)
FILETIME_EPOCH_AS_UTC_DATETIME
=
FILETIME_EPOCH_AS_DATETIME
.
replace
(
tzinfo
=
timezone
.
utc
)
MAX_FILETIME_EPOCH_DATETIME
=
datetime
(
9999
,
12
,
31
,
23
,
59
,
59
)
MAX_FILETIME_EPOCH_AS_UTC_DATETIME
=
MAX_FILETIME_EPOCH_DATETIME
.
replace
(
tzinfo
=
timezone
.
utc
)
def
type_is_union
(
uatype
):
...
...
@@ -165,7 +167,21 @@ _microsecond = timedelta(microseconds=1)
def
datetime_to_win_epoch
(
dt
:
datetime
):
if
dt
.
tzinfo
is
None
:
ref
=
FILETIME_EPOCH_AS_DATETIME
max_ep
=
MAX_FILETIME_EPOCH_DATETIME
else
:
ref
=
FILETIME_EPOCH_AS_UTC_DATETIME
max_ep
=
MAX_FILETIME_EPOCH_AS_UTC_DATETIME
ref
=
FILETIME_EPOCH_AS_DATETIME
if
dt
.
tzinfo
is
None
else
FILETIME_EPOCH_AS_UTC_DATETIME
# Python datetime starts from year 1, opc ua only support dates starting 1601-01-01 12:00AM UTC
# So we need to trunc the value to zero
if
ref
>=
dt
:
return
0
# A date/time is encoded as the maximum value for an Int64 if either
# The value is equal to or greater than 9999-12-31 11:59:59PM UTC,
if
dt
>=
max_ep
:
return
9223372036854775807
return
10
*
((
dt
-
ref
)
//
_microsecond
)
...
...
@@ -174,12 +190,12 @@ def get_win_epoch():
def
win_epoch_to_datetime
(
epch
):
try
:
return
FILETIME_EPOCH_AS_DATETIME
+
timedelta
(
microseconds
=
epch
//
10
)
except
OverflowError
:
# FILETIMEs after 31 Dec 9999 can't be converted to datetime
logger
.
warning
(
"datetime overflow: %s"
,
epch
)
return
datetime
(
MAXYEAR
,
12
,
31
,
23
,
59
,
59
,
999999
)
if
epch
>=
2650467743989999999
:
# FILETIMEs after 31 Dec 9999 are truncated to max value
return
MAX_FILETIME_EPOCH_DATETIME
if
epch
<
0
:
return
0
return
FILETIME_EPOCH_AS_DATETIME
+
timedelta
(
microseconds
=
epch
//
10
)
FROZEN
:
bool
=
False
...
...
tests/test_unit.py
View file @
51c2c510
...
...
@@ -544,6 +544,9 @@ def test_datetime():
assert
epch
==
epch2
epch
=
0
assert
ua
.
datetime_to_win_epoch
(
ua
.
win_epoch_to_datetime
(
epch
))
==
epch
# Test if values that are out of range are either min or max
assert
ua
.
datetime_to_win_epoch
(
datetime
.
min
)
==
0
assert
ua
.
datetime_to_win_epoch
(
datetime
.
max
)
==
(
2
**
63
)
-
1
def
test_equal_nodeid
():
...
...
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