随机生成某年365/366天日期,格式要求为YYYYMMDD形式;
这里通过python实现:
MacBook-Pro:learning_python ora$ vi date.py
#!/usr/bin/env python
#coding=utf-8
import sys, time, random
from time import localtime
from datetime import datetime
def time_to_second(time_str):
time_format = datetime.strptime(time_str, '%Y-%m-%d')
return int(time.mktime(time_format.timetuple()))
def second_to_time(second_str):
return time.strftime('%Y-%m-%d', localtime(second_str))
def main():
if len(sys.argv) != 3:
print 'Usage: %s
\n' \
'Example: %s 2015-01-01 2015-11-11' % (sys.argv[0], sys.argv[0])
exit(-1)
start_time = sys.argv[1]
stop_time = sys.argv[2]
start_second = time_to_second(start_time)
stop_second = time_to_second(stop_time)
random_second = random.randrange(start_second, stop_second)
print second_to_time(random_second)
if __name__ == '__main__':
main()
给date.py文件赋权:
chmod 755 date.py
执行效果:
MacBook-Pro:learning_python ora$ ./date.py 2015-01-01 2015-11-11
2015-07-09
如果想生成一个列表呢:
for i in range(7):
random_second = random.randrange(start_second, stop_second)
print second_to_time(random_second)
|
在random_second前面加一个for循环就好了。
阅读(1485) | 评论(0) | 转发(0) |