time模块

三种时间表示

在Python中,通常有这几种方式来表示时间:

  • 时间戳(timestamp) :         通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
  • 格式化的时间字符串
  • 元组(struct_time)   :         struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import time
 
# 1 time() :返回当前时间的时间戳
time.time()  #1473525444.037215
 
#----------------------------------------------------------
 
# 2 localtime([secs])
# 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
time.localtime() #time.struct_time(tm_year=2016, tm_mon=9, tm_mday=11, tm_hour=0,
# tm_min=38, tm_sec=39, tm_wday=6, tm_yday=255, tm_isdst=0)
time.localtime(1473525444.037215)
 
#----------------------------------------------------------
 
# 3 gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
 
#----------------------------------------------------------
 
# 4 mktime(t) : 将一个struct_time转化为时间戳。
print(time.mktime(time.localtime()))#1473525749.0
 
#----------------------------------------------------------
 
# 5 asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。
# 如果没有参数,将会将time.localtime()作为参数传入。
print(time.asctime())#Sun Sep 11 00:43:43 2016
 
#----------------------------------------------------------
 
# 6 ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
# None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
print(time.ctime())  # Sun Sep 11 00:46:38 2016
 
print(time.ctime(time.time()))  # Sun Sep 11 00:46:38 2016
 
# 7 strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和
# time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个
# 元素越界,ValueError的错误将会被抛出。
print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56
 
# 8 time.strptime(string[, format])
# 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
print(time.strptime('2011-05-05 16:37:06''%Y-%m-%d %X'))
 
#time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
#  tm_wday=3, tm_yday=125, tm_isdst=-1)
 
#在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。
 
 
# 9 sleep(secs)
# 线程推迟指定的时间运行,单位为秒。
 
# 10 clock()
# 这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间戳)。
# 而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行
# 时间,即两次时间差。

1
2
help(time)
help(time.asctime)

random模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import random
 
print(random.random())#(0,1)----float
 
print(random.randint(1,3))  #[1,3]
 
print(random.randrange(1,3)) #[1,3)
 
print(random.choice([1,'23',[4,5]]))#23
 
print(random.sample([1,'23',[4,5]],2))#[[4, 5], '23']
 
print(random.uniform(1,3))#1.927109612082716
 
 
item=[1,3,5,7,9]
random.shuffle(item)
print(item)
 验证码

最新文章

  1. POJ1703Find them, Catch them[种类并查集]
  2. windows系统下的第一个console程序
  3. C#执行存储过程的简化
  4. maven buid 导出项目依赖的jar包问题
  5. 【poj1741】 Tree
  6. MyEclipse------各种问题解决方法
  7. ava.lang.NullPointerException的一般解决方法
  8. coins_多重背包
  9. 使用JDBC向数据库中插入一条数据(第一次修改版)
  10. TikaEntityProcessor 各种示例
  11. robots.txt文件配置和使用方法详解
  12. OSStartHighRdy()一去不复返【worldsing笔记】
  13. light oj 1019【最短路模板】
  14. 关于JavaScript中计算精度丢失的问题
  15. 微信支付 - iOS
  16. Cobbler自动化部署最佳实践
  17. OpenCV分通道显示图片,灰度,融合,直方图,彩色直方图
  18. Python常用算法(一)
  19. C语言柔性数组讲解
  20. Zookeeper+Dubbo+SpringMVC环境搭建

热门文章

  1. 【树剖求LCA】树剖知识点
  2. How to: Set Properties of Web Application Projects
  3. python 循环高级用法 [expression for x in X [if condition] for y in Y [if condition] ... for n in N [if condition] ]按照从左至右的顺序,分别是外层循环到内层循环
  4. 谈谈windows下克隆的坑
  5. Ruby 遍历多个数组
  6. 确定比赛名次(toposort)
  7. 关于二分查找 使用 lower_bound
  8. ACM_蛋糕小王子铁头娃
  9. web api初学
  10. 通过HTTP协议实时获取微信聊天记录