Skip to content

Instantly share code, notes, and snippets.

@stantonk
Last active October 5, 2015 15:47
Show Gist options
  • Save stantonk/2829544 to your computer and use it in GitHub Desktop.
Save stantonk/2829544 to your computer and use it in GitHub Desktop.
date2epoch
#!/usr/bin/env python
# Notes
# One can easily get the current epoch using the date command, like so:
#
# in your timezone:
#
# $ date -j +"%s"
# 1363104249
#
# in UTC:
#
# $ date -uj +"%s"
# 1363104259
#
import datetime
import calendar
import sys
def usage(args):
return 'usage: %s [year] [month] [day] [hour] [minute] [second]' % (args[0])
def fill_defaults(int_args):
""" fill in "minimum" default time values for any values omitted """
min_vals = [2000, 1, 1, 0, 0, 0]
for i in range(6):
if not int_args[i:i+1]:
int_args.append(min_vals[i])
if (__name__ == '__main__'):
try:
arg_count = len(sys.argv)
int_args = [int(a) for a in sys.argv[1:]]
fill_defaults(int_args)
if arg_count == 1:
dt = datetime.datetime.utcnow()
elif arg_count > 1:
dt = datetime.datetime(*int_args)
print calendar.timegm(dt.utctimetuple())
except Exception as e:
print 'Error: %s' % (e.message)
print '\n' + usage(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment