我写了这个python程序。每当我使用参数运行脚本时
python script.py -t 它以 unixtime 形式返回当前时间。
但是每当我尝试传递一个论点时
python script.py -c 1325058720 它说LMT没有定义。所以我从
LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
然后它只是跳过我的参数并返回本地时间的当前时间。
有人可以帮我在 LMT 中传递一个参数并将其转换为可读时间格式。我需要向它传递一个参数并以本地时间可读格式查看输出
import optparse
import re
import time
GMT = int(time.time())
AMT = 123456789
LMT = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(LMT))
VERBOSE=False
def report(output,cmdtype="UNIX COMMAND:"):
#Notice the global statement allows input from outside of function
if VERBOSE:
print "%s: %s" % (cmdtype, output)
else:
print output
#Function to control option parsing in Python
def controller():
global VERBOSE
p = optparse.OptionParser()
p.add_option('--time', '-t', action="store_true", help='gets current time in epoch')
p.add_option('--nums', '-n', action="store_true", help='gets the some random number')
p.add_option('--conv', '-c', action="store_true", help='convert epoch to readable')
p.add_option('--verbose', '-v',
action = 'store_true',
help='prints verbosely',
default=False)
#Option Handling passes correct parameter to runBash
options, arguments = p.parse_args()
if options.verbose:
VERBOSE=True
if options.time:
value = GMT
report(value, "GMT")
elif options.nums:
value = AMT
report(value, "AMT")
elif options.conv:
value = LMT
report(value, "LMT")
else:
p.print_help()