python time模块函数

# -*-coding=utf-8 -*-
__author__ = ‘piay‘
import time

def get_struct_time():
    ‘‘‘
    可以使用list或者字符串格式化
     tm_year-->年
     tm_mon-->月
     tm_mday-->日
     tm_hour-->时
     tm_min-->分
     tm_sec-->秒
     tm_wday--> 0 - 6(0表示周1)
     tm_yday-->一年中的第几天(1-366)
     tm_isdst-->是否是夏令时,默认为-1
    :return:-->time.struct_time(tm_year=2016, tm_mon=6, tm_mday=16,
    tm_hour=23, tm_min=29, tm_sec=13, tm_wday=3, tm_yday=168, tm_isdst=0)
    ‘‘‘
    return time.localtime()

# time.localtime([sec]) 将一个时间戳转换为当前时区的struct)_time,sec参数不提供的话,则以当前时间为标准
print time.localtime()
# ----->time.struct_time(tm_year=2016, tm_mon=6, tm_mday=19, tm_hour=12, tm_min=57, tm_sec=17, tm_wday=6, tm_yday=171, tm_isdst=0)

#time.gtime([sec]) 同localtime类似只是转换为utc时区的时间
print time.gmtime()
#----->time.struct_time(tm_year=2016, tm_mon=6, tm_mday=19, tm_hour=4, tm_min=57, tm_sec=17, tm_wday=6, tm_yday=171, tm_isdst=0)

#返回当前时间的时间戳
print time.time()
#----->1466312237.8

# time.mktime 将struct_time转换为时间戳
print time.mktime(time.localtime())
#----->1466312237.0

#将一个struct_time(默认为当时时间),转换成字符串
print time.asctime()
#---->Sun Jun 19 12:57:17 2016

#time.clock() 函数有两个功能:第一次调用的时候返回的是程序运行的实际时间,第一次调用以后的调用,返回的是自第一次调用后,到
#此次调用的时间间隔
time.sleep(1)
print ‘clock1:%s‘ % time.clock()
time.sleep(1)
print ‘clock2:%s‘ % time.clock()
time.sleep(1)
print ‘clock3:%s‘ % time.clock()
# 输出:clock1:2.93333370582e-06 --->输出程序运行时间
#     clock2:1.00065301596 -->输出与第一个clock的时间间隔
#     clock3:2.00075212073 -->输出与第一个clock的时间间隔

# 线程推辞时间运行,参数单位为秒,比如sleep1秒
# 休眠的时间并非十分精确,有一个浮动值,参数可以是一个浮点数,可以保证精确到0.1s
time.sleep(1.5)

# time.strftime()
‘‘‘
strftime(format[, tuple]) -> string
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
  %y 两位数的年份表示(00-99)
  %Y 四位数的年份表示(000-9999)
  %m 月份(01-12)
  %d 月内中的一天(0-31)
  %H 24小时制小时数(0-23)
  %I 12小时制小时数(01-12)
  %M 分钟数(00=59)
  %S 秒(00-59)

  %a 本地简化星期名称
  %A 本地完整星期名称
  %b 本地简化的月份名称
  %B 本地完整的月份名称
  %c 本地相应的日期表示和时间表示
  %j 年内的一天(001-366)
  %p 本地A.M.或P.M.的等价符
  %U 一年中的星期数(00-53)星期天为星期的开始
  %w 星期(0-6),星期天为星期的开始
  %W 一年中的星期数(00-53)星期一为星期的开始
  %x 本地相应的日期表示
  %X 本地相应的时间表示
  %Z 当前时区的名称
  %% %号本身
‘‘‘
print time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime())
# --->2016-06-19 14:26:18

# time.strptime()
‘‘‘
strptime(string, format) -> struct_time
将时间字符串根据指定的格式化符转换成数组形式的时间
‘‘‘
print time.strptime(‘2016 9 25 10:22:32‘,‘%Y %m %d %H:%M:%S‘)
# -->time.struct_time(tm_year=2016, tm_mon=9, tm_mday=25, tm_hour=10, tm_min=22, tm_sec=32, tm_wday=6, tm_yday=269, tm_isdst=-1)
时间: 2024-07-30 08:20:00

python time模块函数的相关文章

Python中re模块函数使用介绍

Python中通过re模块实现了正则表达式的功能.re模块提供了一些根据正则表达式进行查找.替换.分隔字符串的函数.本文主要介绍re模块中常用的函数和函数常用场景. re模块常用函数 1.match(pattern,string,flags=0) 根据pattern从string的头部开始匹配字符串,只返回第1次匹配成功的对象,否则,返回None.flags表示规则选项. >>> import re >>> Str='Python:Java:C' >>>

python re 模块 findall 函数用法简述

python re 模块 findall 函数用法简述 代码示例: 1 >>> import re 2 >>> s = "adfad asdfasdf asdfas asdfawef asd adsfas " 3 4 >>> reObj1 = re.compile('((\w+)\s+\w+)') 5 >>> reObj1.findall(s) 6 [('adfad asdfasdf', 'adfad'), ('a

Python 基础学习之: Python math 模块、cmath 模块 区别是 cmath 模块运算的是复数,math 模块运算的是数学运算 Python数学函数列表及解释 Python math 模块提供了许多对浮点数的数学运算函数。 Python cmath 模块包含了一些用于复数运算的函数

Python math 模块.cmath 模块 Python 中数学运算常用的函数基本都在 math 模块.cmath 模块中. Python math 模块提供了许多对浮点数的数学运算函数. Python cmath 模块包含了一些用于复数运算的函数. cmath 模块的函数跟 math 模块函数基本一致,区别是 cmath 模块运算的是复数,math 模块运算的是数学运算. 要使用 math 或 cmath 函数必须先导入: import math 查看 math 查看包中的内容: impo

python之模块ctypes

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ctypes import ctypes #ctypes是python的一个外部库,它提供了C兼容的数据类型,并允许调用函数C DLL. #注意事项: #就我个人目前而言,了解该库是提供与C语言数据类型兼容的接口作用即可,不需要深入了解.

如何理解Python的Main函数?

作者:蟒蛇帝国(ID:Pythondg) 难度:初级 演示环境:OS:ubuntu 16.04Python:3.6 编写 Python 代码的时候我们经常看到下面这条语句.貌似是 Python 的 Main 函数.那它具体是什么意思呢. if __name__ == '__main__':     print('hello world') 首先 Python 里有两个概念, 源码文件: ~/code_house/py_dev$ tree . ├── file1.py ├── file2.py └

python——常用模块

time.asctime(time.localtime(1234324422)) python--常用模块 1 什么是模块: 模块就是py文件 2 import time #导入时间模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: (1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行"type(time.time())",返回的是float类型.

python 集合、函数

*集合: set:持有一系列元素,但是set元素没有重复,并且无序 如何创建:set()并传入一个list,list的元素将作为set 的元素. s=set(['a','b','c']) print(s)    //set(['a', 'c', 'b']) print(len(s))  //3 如何访问:用in操作符判断有没有 =([,,,]) ( )//True set的特点: 1.内部结构和字典很像,唯一区别是不存储value =([,,,,,,]) x1=x2=x1 :     () (x

[转]python pickle模块

持久性就是指保持对象,甚至在多次执行同一程序之间也保持对象.通过本文,您会对 Python对象的各种持久性机制(从关系数据库到 Python 的 pickle以及其它机制)有一个总体认识.另外,还会让您更深一步地了解Python 的对象序列化能力. 什么是持久性? 持久性的基本思想很简单.假定有一个 Python 程序,它可能是一个管理日常待办事项的程序,您希望在多次执行这个程序之间可以保存应用程序对象(待办事项).换句话说,您希望将对象存储在磁盘上,便于以后检索.这就是持久性.要达到这个目的,

python之模块ftplib(FTP协议的客户端)

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ftplib(FTP协议的客户端) #需求:快速进行ftp上传 ,下载,查询文件 from ftplib import FTP ftp = FTP() #设置变量 timeout = 30 port = 21 ftp.connect('192.168.1.188',port,timeout) # 连接FTP服务器 ftp.login('UserName','888888') # 登录 p