-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: Add system time manipulations.
- Loading branch information
Showing
4 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from datetime import datetime | ||
|
||
from rpcclient.darwin.structs import timeval | ||
from rpcclient.exceptions import MissingLibraryError | ||
from rpcclient.structs.consts import RTLD_NOW | ||
|
||
|
||
class Time: | ||
def __init__(self, client): | ||
""" | ||
:param rpcclient.darwin.client.DarwinClient client: | ||
""" | ||
self._client = client | ||
self._load_core_time_framework() | ||
|
||
def _load_core_time_framework(self): | ||
if self._client.dlopen('/System/Library/PrivateFrameworks/CoreTime.framework/CoreTime', RTLD_NOW): | ||
return | ||
raise MissingLibraryError('failed to load CoreTime') | ||
|
||
def now(self): | ||
with self._client.safe_calloc(timeval.sizeof()) as current: | ||
self._client.symbols.gettimeofday(current, 0) | ||
time_of_day = timeval.parse(current.peek(timeval.sizeof())) | ||
return datetime.fromtimestamp(time_of_day.tv_sec + (time_of_day.tv_usec / (10 ** 6))) | ||
|
||
def set_current(self, new_time: datetime): | ||
self._client.symbols.TMSetAutomaticTimeZoneEnabled(0) | ||
with self._client.safe_calloc(timeval.sizeof()) as current: | ||
current.poke(timeval.build({'tv_sec': int(new_time.timestamp()), 'tv_usec': new_time.microsecond})) | ||
self._client.symbols.settimeofday(current, 0) | ||
|
||
def set_auto(self): | ||
self._client.symbols.TMSetAutomaticTimeZoneEnabled(1) | ||
|
||
@property | ||
def is_set_automatically(self): | ||
return bool(self._client.symbols.TMIsAutomaticTimeZoneEnabled()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from datetime import datetime | ||
|
||
|
||
def test_now(client): | ||
""" | ||
:param rpcclient.client.Client client: | ||
""" | ||
assert client.time.now() > datetime.fromtimestamp(0) | ||
|
||
|
||
def test_set_current(client): | ||
""" | ||
:param rpcclient.client.Client client: | ||
""" | ||
try: | ||
client.time.set_current(datetime(year=2020, month=1, day=1)) | ||
assert client.time.now() < datetime(year=2020, month=1, day=1, minute=1) | ||
finally: | ||
client.time.set_auto() | ||
|
||
|
||
def test_is_set_automatically(client): | ||
""" | ||
:param rpcclient.client.Client client: | ||
""" | ||
client.time.set_auto() | ||
assert client.time.is_set_automatically |