vailixi Posted October 16, 2018 Posted October 16, 2018 (edited) Can anyone suggest a command line tool to convert any calendar date to an epoch time? I don't want current time as epoch. I want to be able to input a calender date and get epoch. I noticed there are a lot of Javascript tools that do this but I was wonder if there is a command line tool. I'll code it if I have to, but time would be better spent elsewhere if it already exists. I noticed date time groups are easiest to work with as epoch time. At least for doing comparison operators. Here's a python snippet that essentially does what I'm looking for. Is there a native Linux application that does this? #!/usr/bin/python import datetime import calendar aprilFirst=datetime.datetime(2012, 11, 12, 0, 0) print(calendar.timegm(aprilFirst.timetuple())) This works for now. #!/usr/bin/python import sys from sys import argv import datetime import calendar year = int(argv[1]) month = int(argv[2]) day = int(argv[3]) minute = int(argv[4]) second = int(argv[5]) aprilFirst=datetime.datetime(year, month, day, minute, second) #usage py_epoch.py year month day minute second print(calendar.timegm(aprilFirst.timetuple())) Edited October 16, 2018 by vailixi Quote
Just_a_User Posted October 16, 2018 Posted October 16, 2018 (edited) 4 hours ago, vailixi said: Can anyone suggest a command line tool to convert any calendar date to an epoch time? I only found Python also #!/usr/bin/python import time import datetime import sys # # script calculates the time sind epoch (1970-01-01 00:00am UTC) # if len(sys.argv) < 2: print "Usage: %s 2011-10-25 [19:30:00]" % (sys.argv[0]) sys.exit(1) elif len(sys.argv) !=3: u_time = "00:00:00" else: u_time = sys.argv[2] u_date = sys.argv[1] u_input = "%s %s" % (u_date, u_time) print "epoch for given time: \t %s" % (int(time.mktime(time.strptime(u_input, '%Y-%m-%d %H:%M:%S')))) now = int( time.time() ) print "epoch timezone: \t %s" % (now) Edited October 16, 2018 by Just_a_User Quote
vailixi Posted October 16, 2018 Author Posted October 16, 2018 13 hours ago, Just_a_User said: I only found Python also #!/usr/bin/python import time import datetime import sys # # script calculates the time sind epoch (1970-01-01 00:00am UTC) # if len(sys.argv) < 2: print "Usage: %s 2011-10-25 [19:30:00]" % (sys.argv[0]) sys.exit(1) elif len(sys.argv) !=3: u_time = "00:00:00" else: u_time = sys.argv[2] u_date = sys.argv[1] u_input = "%s %s" % (u_date, u_time) print "epoch for given time: \t %s" % (int(time.mktime(time.strptime(u_input, '%Y-%m-%d %H:%M:%S')))) now = int( time.time() ) print "epoch timezone: \t %s" % (now) Thanks, This is a time saver. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.