Created
June 6, 2018 04:02
-
-
Save CarlosDomingues/31f37516b8f1b850b84a369e70d5f41e to your computer and use it in GitHub Desktop.
Python - Getting correct time, date and timezone
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
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