Some important function of time describes the Python library

This introduction of some commonly used functions, and application methods python-time inside, easy to learn and use everybody.

1. time.time () - returns the current timestamp (floating type)

#! /usr/bin/python

import time

time_stamp = time.time()
#1548826080.93

2. time.localtime () - returns a tuple structure of the present time

#! /usr/bin/python

import time

time_tuple = time.localtime()
#time_tuple = time.localtime(time.time())
#time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=13, tm_min=38, tm_sec=46, tm_wday=2, tm_yday=30, tm_isdst=0)
#数据可以直接用元组方法使用

3.time.strftime () - Returns the form of a specified time (string type)

#! /usr/bin/python

import time

time_strf = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
#2019-01-30 14:00:33

4. time.strptime () - returns a tuple structure of time data time.localtime () is similar to

#! /usr/bin/python

import time

time_strp = time.strptime("2019-01-30","%Y-%m-%d")
#time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=30, tm_isdst=-1)

5.time.gmtime () - returns zero zone structured time tuple

#! /usr/bin/python

import time

time_gm = time.gmtime()
#time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=6, tm_min=6, tm_sec=26, tm_wday=2, tm_yday=30, tm_isdst=0)
#比北京时间晚八个小时的元组数据

6.time.mktime () - Returns the structure of a tuple timestamp (floating-point type)

#! /usr/bin/python

import time

time_mk = time.mktime(time.strptime("11 Jan 2019","%d %b %Y"))
#1547136000.0
#例如:用此方法求出11 Jan 2019的时间戳

7.time.ctime () - Returns the current time in the form of time

#! /usr/bin/python

import time

time_ct = time.ctime()
#Wed Jan 30 14:32:06 2019

Learn these methods, so the question is:
now there is a need to get the day 0:00 time stamp, which is how to program?


Ideas: The time.strftime first use () is obtained in the form of time the time of day, using the time.strptime () into tuple data, then time.mktime () the data into a time stamp tuple 0:00.


#! /usr/bin/python

import time

get_time_strf = time.strftime("%Y-%m-%d",time.localtime())
get_time_strp = time.strptime(get_time_strf,"%Y-%m-%d")
get_time_mk = time.mktime(get_time_strp)
#1548777600.0
#当然有能力也可以直接就写出来 *_*
#get_time_mk = time.mktime(time.strptime(time.strftime("%Y-%m-%d",time.localtime()),"%Y-%m-%d"))

Guess you like

Origin blog.csdn.net/weixin_42883530/article/details/86701845