import datetime
import time
from faker import Faker
fake = Faker(locale='zh_CN')
# 随机生成手机号
def rdm_phone_number():
return fake.phone_number()
# 生成当前日期
def cur_date(): # 2023-09-24
return fake.date_between_dates()
# 生成当前日期,带时分秒
def cur_date_time(): # 2023-09-24 11:34:59
return fake.date_time_between_dates()
# 随机生成日期
def rdm_date(pattern='%Y-%m-%d'):
return fake.date(pattern=pattern)
# 随机生成日期带时分秒
def rdm_date_time():
return fake.date_time()
# 生成当前时间戳
def cur_timestamp(level='s'):
if level == 's':
return int(time.time()) # 10位时间戳,精确到秒
elif level=='ms':
return int(time.time()*1000) # 13位时间戳,精确到毫秒
else:
raise Exception(f'{level}不支持')
# 生成某一个时间段的时间戳
def gen_timestamp(start_date='+0d',end_date='+1d'):
date_time = fake.date_time_between(start_date=start_date,end_date=end_date)
print(date_time)
return int(time.mktime(date_time.timetuple()))
# 计算两个时间戳之间相差了多少天
def get_days_difference(timestamp1,timestamp2):
# 转换成datetime对象
date1 = datetime.datetime.fromtimestamp(timestamp1)
date2 = datetime.datetime.fromtimestamp(timestamp2)
# 计算差值
diff = date2-date1
# 提取差值的天数部分
days = diff.days
return days
def company():
return fake.company()
if __name__ == '__main__':
print(company())
print(rdm_phone_number())
print(cur_date())
print(cur_timestamp())
# print(cur_timestamp('ms'))
# print(gen_timestamp(start_date='+10d', end_date='+20d'))
# print(gen_timestamp(start_date='-10d',end_date='-5d'))
python随机参数
最新推荐文章于 2024-09-09 00:00:00 发布