Jump to content

Recommended Posts

Posted (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 by vailixi
Posted (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 by Just_a_User
Posted
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.

 

 

 

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...