Skip to content

Instantly share code, notes, and snippets.

@kde713
Last active January 6, 2019 11:21

Revisions

  1. kde713 revised this gist Jan 6, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion time.py
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ def sync(self):
    win32api.SetSystemTime(server_time.year, server_time.month, server_time.isocalendar()[2], server_time.day, server_time.hour, server_time.minute, server_time.second, 0)

    else:
    raise SyncError("Now supported platform: " + str(sys.platform))
    raise SyncError("Not supported platform: " + str(sys.platform))


    if __name__ == "__main__":
  2. kde713 revised this gist Jan 6, 2019. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    pywin32==224
    requests==2.21.0
    fire==0.1.3
  3. kde713 renamed this gist Jan 6, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions app.py → time.py
    Original file line number Diff line number Diff line change
    @@ -27,13 +27,13 @@ def get_api_datetime(self, tz: str) -> datetime:


    class TimeSyncApplication:
    def view(self, tz: str):
    def view(self):
    client = WorldTimeApiClient()
    sys.stdout.write(client.get_api_datetime(tz).isoformat())
    sys.stdout.write(client.get_api_datetime("UTC").isoformat())

    def sync(self, tz: str):
    def sync(self):
    client = WorldTimeApiClient()
    server_time = client.get_api_datetime(tz)
    server_time = client.get_api_datetime("UTC")

    if sys.platform == "win32":
    from win32 import win32api
  4. kde713 created this gist Jan 6, 2019.
    47 changes: 47 additions & 0 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    import sys
    from datetime import datetime

    import fire
    import requests


    class SyncError(Exception):
    def __init__(self, message):
    self.message = message

    def __str__(self):
    return self.message

    def __repr__(self):
    return f"SyncError('{self.message}')"


    class WorldTimeApiClient:
    def get_api_datetime(self, tz: str) -> datetime:
    response = requests.get("http://worldtimeapi.org/api/timezone/" + tz)
    data = response.json()
    if data.get("error"):
    raise SyncError("API Error: " + response.json()['error'])

    return datetime.fromisoformat(response.json()['datetime'])


    class TimeSyncApplication:
    def view(self, tz: str):
    client = WorldTimeApiClient()
    sys.stdout.write(client.get_api_datetime(tz).isoformat())

    def sync(self, tz: str):
    client = WorldTimeApiClient()
    server_time = client.get_api_datetime(tz)

    if sys.platform == "win32":
    from win32 import win32api
    win32api.SetSystemTime(server_time.year, server_time.month, server_time.isocalendar()[2], server_time.day, server_time.hour, server_time.minute, server_time.second, 0)

    else:
    raise SyncError("Now supported platform: " + str(sys.platform))


    if __name__ == "__main__":
    fire.Fire(TimeSyncApplication)
    3 changes: 3 additions & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    pywin32==224
    requests==2.21.0
    fire==0.1.3