Created
June 6, 2018 03:54
-
-
Save CarlosDomingues/8fd85d8bf5a56d9b76bfd25249cd370c to your computer and use it in GitHub Desktop.
Getting the number of days since 1970/01/01, as a string.
This file contains hidden or 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
def datetime_to_unixtime(date): | |
"""Returns the number of days between 'date' and 1970/01/01, as a string. | |
Parameters | |
---------- | |
date : datetime.datetime | |
A date object of the datetime.datetime class. | |
Returns | |
------- | |
str | |
Returns the number of days since 1970/01/01, as a string. | |
""" | |
if isinstance(date, datetime) is False: | |
raise TypeError("Date type should be datetime.datetime.") | |
if date > datetime(1970,1,1): | |
epoch_day = date - datetime.utcfromtimestamp(0) | |
epoch_day = str(epoch_day)[:-14] | |
return epoch_day | |
else: | |
raise ValueError("Date should be more recent than 1970-01-01.") | |
def get_current_date(): | |
"""Returns the current day as a string, in the unix timestamp format. | |
---------- | |
Returns | |
------- | |
str | |
The current day as a string, in the unix timestamp format. | |
""" | |
current_date = datetime(datetime.today().year, | |
datetime.today().month, | |
datetime.today().day) | |
return datetime_to_unixtime(current_date) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment