python time module (13)

 

 

 

    python time module provides a variety of functions related mainly containing the date and time functions. time module provides both the date and time function formatted as a string, but also provides a resume date and time from the feature string.

 

Tell you a little secret

 

I. Introduction

    Module provided within a time many functions, many time.struct_time function returns a class that represents a time object that mainly contains nine attributes, each attribute information as shown:

time

 

.Time two common function module Introduction

    time.gmtime ([secs]) - The time is representative of the number of seconds into a struct_time object. If you do not pass parameters, the current time;

    time.asctime ([t]) - tuple or struct_time time is converted to a time string. If no parameter t, the current default conversion time;

    time.localtime ([secs]) - The time is represented by the number of seconds representing the current object converted struct_time time. If you do not pass parameters, the current time;

    time.sleep (secs) - pause secs seconds, do not do anything;

    time.strftime (format [, t]) - struct_time time tuples or objects formatted string for the specified time format. If no parameter t, the current default conversion time;

    time.strptime (string [, format]) - The format of the time string struct_time parsed into objects;

    time.time () - returns from at 0:00 on January 1, 1970 up to now the whole the number of seconds;

    time.tzname - name Returns the local area;

# 获取本地时区的名字
local_time = time.tzname[0]
# 如果不设置编码格式,输出是乱码
print(local_time.encode('latin-1').decode('gbk'))

 

输出:

 

This operation also

 

三.time模块使用

1.获取时间

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com
 
@File:python_time.py
@Time:2019/11/07 21:25
 
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
 
# 导入时间time模块
import time
 
# 获取当前时间,默认格式
print(time.asctime())
 
# 返回 time.struct_time 类型对象
print(time.gmtime())
 
# 返回 time.struct_time 类型对象
print(time.localtime())
 
# 返回从 1970 年 1 月 1 日 0 点整到现在过了多少秒
print(time.time())

 输出结果:

 

 

2.自定义时间格式

    可以通过 time.strftime() 函数 将 struct_time 对象转为自定义时间格式 或者 将字符串转为 struct_time 对象,在转换过程中可能需要用到以下格式参数:

    %a 星期几的简写;

    %A 星期几的全称;

    %b 月分的简写;

    %B 月份的全称;

    %c 标准的日期的时间串;

    %C 年份的后两位数字;

    %d 十进制表示的每月的第几天;

    %D 月/天/年;

    %e 在两字符域中,十进制表示的每月的第几天;

    %F 年-月-日;

    %g 年份的后两位数字,使用基于周的年;

    %G 年分,使用基于周的年;

    %h 简写的月份名;

    %H 24小时制的小时;

    %I 12小时制的小时;

    %j 十进制表示的每年的第几天;

    %m 十进制表示的月份;

    %M 十时制表示的分钟数;

    %n 新行符;

    %p 本地的AM或PM的等价显示;

    %r 12小时的时间;

    %R 显示小时和分钟:hh:mm;

    %S 十进制的秒数;

    %t 水平制表符;

    %T 显示时分秒:hh:mm:ss;

    %u 每周的第几天,星期一为第一天 (值从0到6,星期一为0);

    %U 第年的第几周,把星期日做为第一天(值从0到53);

    %V 每年的第几周,使用基于周的年;

    %w 十进制表示的星期几(值从0到6,星期天为0);

    %W 每年的第几周,把星期一做为第一天(值从0到53);

    %x 标准的日期串;

    %X 标准的时间串;

    %y 不带世纪的十进制年份(值从0到99);

    %Y 带世纪部分的十制年份;

    %z,%Z 时区名称,如果不能得到时区名称则返回空字符;

    %% 百分号;

 

    将 struct_time 对象转为自定义时间格式,示例代码如下:

# 导入时间time模块
import time
 
 
str_time = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())#再由中间格式转为字符串(str)
print(str_time)
 
str_time = time.strftime('%Y/%m/%d %H:%M:%S', time.localtime())#再由中间格式转为字符串(str)
print(str_time)
 
str_time = The time.strftime ( ' % the Y /% m /% H-D%%% M-S ' , time.localtime ()) # and then from the intermediate format into a string (STR) 
Print (str_time)

 

    Output:

 

asphyxia

 

3. Review the code execution time

    In python project development process, we tend to optimize the code, find out what the code is relatively time-consuming, how much concrete time-consuming, then the same can also be done through the time module,

    Code analysis: get the current time by the time.time (), and then subtracting the two times, the code can be obtained naturally takes time, unit: seconds following sample code:

    If one day you go Fudge your project with this code experience or boss, when you interrupted Please do not mention my leg!

deny

 

 

you may also like:

    1.python sys module

    2.python random module

    3.python thread creation

    4.python thread mutex Lock

 

    Reproduced please specify: ape say Python  »  Python Time Module

 

Technical exchanges, business cooperation please contact bloggers
Scan code or search: ape say python
No public python tutorial
Ape say python
No. sweep the micro-channel public concern

Guess you like

Origin www.cnblogs.com/shuopython/p/12075161.html