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/8fd85d8bf5a56d9b76bfd25249cd370c to your computer and use it in GitHub Desktop.
Save CarlosDomingues/8fd85d8bf5a56d9b76bfd25249cd370c to your computer and use it in GitHub Desktop.
Getting the number of days since 1970/01/01, as a string.
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