time&datetime&random模块

本文介绍了Python中使用time和datetime模块进行时间处理的方法,包括时间格式化、时间戳转换等,并展示了如何利用random模块生成随机数及应用场景,如验证码生成和游戏角色随机分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


import time
import datetime

'''Return the CPU time or real time since the start of the process or since
the first call to clock(). This has as much precision as the system
records.'''

print(time.clock())
print(time.process_time())#返回处理器时间
print(time.asctime())#返回时间格式Fri Apr 14 11:00:03 2017
print(time.asctime(time.localtime())) #返回时间格式,参数为struct time对象格式
print(time.localtime(time.time()))#返回本地时间 的struct time对象格式,参数是时间戳(秒)
print(time.localtime())#同上
print(time.ctime()) #Fri Apr 14 11:08:29 2017,参数为时间戳(秒)
print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式,参数是时间戳(秒)
print(time.gmtime(time.time() - 800000)) # 返回utc时间的struc时间对象格式,参数p_tuple


# 日期字符串 转成 时间戳 strptime
string_2_struct = time.strptime("2017/04/14","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
print(string_2_struct)#time.struct_time(tm_year=2017, tm_mon=4, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0,                                                  tm_wday=4, tm_yday=104, tm_isdst=-1)
struct_2_string=time.strftime("%x",string_2_struct) #将struct时间对象转换成日期格式 04/14/17
print(struct_2_string)#'04/14/17'

struct2_2_string=time.strftime("%Y-%m-%d %A %B",string_2_struct)
print(struct2_2_string)#'2017-04-14 Friday April'


#时间加减
print(datetime.datetime.now()) #返回 2017-08-19 12:47:03.941925
print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
print(datetime.datetime.now() )
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分

c_time  = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换
DirectiveMeaningNotes
%aLocale’s abbreviated weekday name. 
%ALocale’s full weekday name. 
%bLocale’s abbreviated month name. 
%BLocale’s full month name. 
%cLocale’s appropriate date and time representation. 
%dDay of the month as a decimal number [01,31]. 
%HHour (24-hour clock) as a decimal number [00,23]. 
%IHour (12-hour clock) as a decimal number [01,12]. 
%jDay of the year as a decimal number [001,366]. 
%mMonth as a decimal number [01,12]. 
%MMinute as a decimal number [00,59]. 
%pLocale’s equivalent of either AM or PM.(1)
%SSecond as a decimal number [00,61].(2)
%UWeek number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.(3)
%wWeekday as a decimal number [0(Sunday),6]. 
%WWeek number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.(3)
%xLocale’s appropriate date representation. 
%XLocale’s appropriate time representation. 
%yYear without century as a decimal number [00,99]. 
%YYear with century as a decimal number. 
%zTime zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. 
%ZTime zone name (no characters if no time zone exists). 
%%A literal '%' character.


####random模块


import random
print random.random()#产生随机数
print random.randint(1,10)#1-10之间的随机整数
print random.randrange(1,10)#1-10之间不包括(1和10)的随机整数
 
写个随机生成验证码的函数玩玩
##随机生成验证码
def get_code():
checkcode = ''#初始化验证码
strings=[] #验证码生成来源
for i in range(1,5): #生成四个验证码
current = random.randrange(0,5)
if current != i:
chr1 = chr(random.randint(65,90)) #将数字转换成大写字母
chr2= chr(random.randint(97,122)) #将数字转换成小写字母
strings.append(chr1)
strings.append(chr2)
else:
digit = random.randint(0,9)
strings.append(str(digit))
checkcode += random.choice(strings)+" "
print(checkcode)
if __name__=="__main__":
get_code()

###杀人游戏角色随机分配
def get_role(L_role): #接收一个角色的列表
#从A-Z选取玩杀人游戏的人数;游戏人数肯定不超过26个
gamers=(chr(i) for i in range(65,91))#A-Z的生成器
game_players=[]#玩游戏的人
roles=[]#分配的游戏角色
for i in range(len(L_role)): #根据游戏的角色的人数选择参与游戏的人
# chr1 = chr(random.randint(65, 90))
random.shuffle(L_role)#打乱角色的顺序;其实有没有都无所谓,只是为了练习
game_players.append(next(gamers)) #调用生成器,选择一个参与游戏的人选
temp=random.choice(L_role) #随机分配游戏角色
roles.append(temp) #已经分配好的角色
L_role.remove(temp) #删除已经被分配了的角色
for gamer,role in zip(game_players,roles): #遍历输出游戏选手对应的游戏角色
print(gamer+" ",role)
L_role=["警察","警察","警察","杀手","杀手","杀手","平民","平民","平民","平民","平民","平民","平民"]
if __name__=="__main__":
# get_code()
print("gamer---role")
get_role(L_role)
 

转载于:https://www.cnblogs.com/WhatTTEver/p/6706875.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值