模块,用一砣代码实现了某个功能的代码集合。 

类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。

如:os 是系统相关的模块;file是文件操作相关的模块

模块分为三种:

  • 自定义模块
  • 内置标准模块(又称标准库)
  • 开源模块

一、time & datetime模块

  1 #!/usr/bin/env python
  2 # _*_ coding:utf-8 _*_
  3 
  4 import time
  5 
  6 
  7 # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
  8 # print(time.altzone/60/60)  #返回与utc时间的时间差,以秒计算\
  9 # print(time.asctime()) #返回时间格式"Fri Aug 19 11:14:16 2016",
 10 # print(time.localtime()) #返回本地时间 的struct time对象格式
 11 # print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
 12 #
 13 # print(time.asctime(time.localtime())) #返回时间格式"Fri Aug 19 11:14:16 2016",
 14 # print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上
 15 
 16 
 17 
 18 # 日期字符串 转成  时间戳
 19 # string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
 20 # print(string_2_struct)
 21 # #
 22 # struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳
 23 # print(struct_2_stamp)
 24 
 25 
 26 
 27 #将时间戳转为字符串格式
 28 # print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
 29 # print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
 30 
 31 
 32 
 33 
 34 
 35 #时间加减
 36 import datetime
 37 
 38 # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
 39 #print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
 40 # print(datetime.datetime.now() )
 41 # print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
 42 # print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
 43 # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
 44 # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
 45 
 46 
 47 #
 48 # c_time  = datetime.datetime.now()
 49 # print(c_time.replace(minute=3,hour=2)) #时间替
时间模块

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-28
  • 2022-12-23
  • 2021-12-23
  • 2021-09-13
  • 2021-06-05
  • 2021-05-23
猜你喜欢
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2021-06-26
  • 2021-09-10
  • 2022-02-24
  • 2021-10-25
相关资源
相似解决方案