Skip to content

Instantly share code, notes, and snippets.

@jjmalina
Last active August 29, 2015 14:08
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 jjmalina/3e896cf26486ebf416fb to your computer and use it in GitHub Desktop.
Save jjmalina/3e896cf26486ebf416fb to your computer and use it in GitHub Desktop.
Because I keep forgetting this...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
datetime_to_epoch
~~~~~~~~~~~~~~~~~
Takes a (naive) UTC datetime and converts to epoch
"""
from calendar import timegm
def to_epoch(dt):
"""Returns the UTC epoch given a datetime
"""
return timegm(dt.utctimetuple())
if __name__ == '__main__':
import sys
from dateutil.parser import parse
if len(sys.argv) > 1:
for date in sys.argv[1:]:
print to_epoch(parse(date))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment