💻
Code Snippet
  • Overview
  • General
    • Anaconda
  • GUI
    • PyQT
      • Qt Design
  • Pandas
    • Read Data
    • Replace
  • Articles
    • Python. PyQt
    • Offline Payment Wallet With Django
    • Documentation Encrypt File
    • Play With Pillow
  • Fontend
    • Snippet
    • Hugo
    • JavaScript
      • Form Validation
  • Finance
    • Library
      • yfinance
  • Notebook
    • Untitled
    • Snippet
  • Python
    • Download file
    • Date and Time
    • Snippet
    • Compile .exe
    • Overview
    • Google
      • Samples for Google Workspace
      • Drive
      • GoogleSheet
    • Virtual environment
    • Database
      • Pickle()
    • Datatypes
      • Excel
      • Dictionary
        • xmltodict()
    • File Handling
      • shutil()
      • Get the File Name
      • Get the Full Path
      • Check the File Size
      • Get File Creation Date
      • Find All File
        • Untitled
    • Dictionary
      • Convert Two Lists
  • Data Science
    • HTTP requests
  • Google Workspace
    • Overview
    • Apps Script
      • ์Note
      • Overview
      • Snippet
        • HTML Service
        • Fetch API
      • Quickstart
      • Google Sheets
        • Overview
          • Snippet
        • Fundamentals
          • Macros & Custom Functions
          • Spreadsheets, Sheets, and Ranges
          • Working with Data
          • Data Formatting
          • Chart and Present Data
        • Built-in Google Services
        • Fetch and format API data
        • Connected Sheets
  • Git
  • Mini Lab
    • Line
    • Python
  • Function
    • Python
      • Date&Time
  • Database
    • SQLite
      • Example
Powered by GitBook
On this page
  • Update System Clock

Was this helpful?

  1. Python

Date and Time

PreviousDownload fileNextSnippet

Last updated 3 years ago

Was this helpful?

import json
import re
from subprocess import Popen
from time import sleep
 
import requests
 
def set_current_time():
    """Set system clock to the current time if wrong."""
    try:
        res = requests.get("http://worldtimeapi.org/api/ip")
        data = res.text
        time_regex = r"\d+:\d+:\d+"
        datetime = json.loads(data)['datetime']
        date = re.search(time_regex,datetime).group()
        Popen(['time',date],shell = True)
        
        return date
 
    except requests.exceptions.ConnectionError:
        print('Connection error, please wait retrying,')
        sleep(3000)
dt = set_current_time()
dt

# '17:18:27'

Update System Clock