需求:需要大数据测试考勤时间,生成一些范围内的秒级时间戳
思路:使用faker.random_int方法生成13位时间戳,然后返回一个数组
备注:本身是希望找到faker库中的方法,发现并没有找到
from faker import Faker
import time
from datetime import datetime
fake= Faker(locale='zh_CN')
def change_time(timestr):
datetime_obj = datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S.%f")
obj_stamp = int(time.mktime(datetime_obj.timetuple()) * 1000.0 + datetime_obj.microsecond / 1000.0)
return obj_stamp
def get_time(time1,time2,num):
time1=change_time(time1)
time2 = change_time(time2)
L=[]
i =0
while i<num:
f = fake.random_int(min=time1, max=time2)
l1=L.append(f)
i =i +1
return (L)
if __name__ == '__main__':
time1 ="2020-09-24 15:42:15.112"
time2 = "2020-10-24 15:42:15.112"
L= get_time(time1,time2,10)
print(L)