Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlosDomingues/31f37516b8f1b850b84a369e70d5f41e to your computer and use it in GitHub Desktop.
Save CarlosDomingues/31f37516b8f1b850b84a369e70d5f41e to your computer and use it in GitHub Desktop.
Python - Getting correct time, date and timezone
import datetime
import time
import tzlocal
def get_formated_datetime():
"""Returns the current date and time, as a formated string with the
following formating:
'Wed Jan 31 2018 16:02:27 GMT-0200 (E. South America Daylight Time)'
Parameters
----------
Returns
-------
str
Returns the current date and time, as a formated string with the
following formating:
'Wed Jan 31 2018 16:02:27 GMT-0200 (E. South America Daylight Time)'
"""
local_tz = tzlocal.get_localzone()
current_datetime = datetime.datetime.now(tz=local_tz)
formated_datetime = current_datetime.strftime("%a %b %d %Y %H:%M:%S GMT%z ")
formated_datetime = formated_datetime + "(" + time.localtime().tm_zone + ")"
return formated_datetime
def is_dst():
"""Checks wheter user local time zone is under daylight saving time or not.
Parameters
----------
Returns
-------
boolean
Returns True if the local time zone on the user´s machine is under daylight
saving regime and False if it is not.
"""
return bool(time.localtime().tm_isdst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment