Python common modules-time modules

Source: Internet
Author: User
Tags local time timedelta

Python common modules-time modules

Yun Zhengjie

Copyright Notice: Original works, declined reprint! Otherwise, the legal liability will be investigated.

I. Initial TIME module

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 Import Time8 9 """"Ten categories of modules: One Module Essence is a "*.py" file, broadly divided into the following three categories: A 1>: A built-in module that refers to a module that exists inside a Python interpreter, such as a time module; - 2>> third-party module, refers to the installation of Python after the Lib folder module; - 3>. Custom module, refers to the Python program you write yourself; the """ -  - Print(Time.time ())#returns the current timestamp, representing the number of seconds from January 1, 1971 "00:00:00" to the time node of the moment.  -  +s = Time.localtime ()#creates a time object, or, in the structured time object, returns the Struct_time object format for the local time.  - Print(s) + Print(s.tm_year)#gets the year.  A Print(S.tm_mon)#gets the month.  at  -S2 = Time.gmtime ()#returns the Struc Time Object format for UTC time.  - Print(S2) -  -  -  in  - #The result of the above code execution is as follows: to1520176127.9244497 +Time.struct_time (tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=23, Tm_min=8, tm_sec=47, tm_wday=6, tm_yday=63, tm_isdst=0) -2018 the3 *Time.struct_time (tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=15, Tm_min=8, tm_sec=47, tm_wday=6, tm_yday=63, tm_isdst=0 )

Two. Conversion of time modules to each other

1. Converting a mnemonic chart

2. Case Show

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 Import Time8 9s = time.localtime (31245244545)#a structured time object that converts a timestamp to a structured time. Ten Print(s) One  AS2 = Time.mktime (Time.localtime ())#converts a structured time into a timestamp.  - Print(S2) -  theS3 = Time.strftime ("%y-%m-%d", Time.localtime ())#converts a structured time into a string time.  - Print(S3) -  -S4 = Time.strptime ("1993:05:19","%y:%m:%d")#converts a string time into a structured time.  + Print(S4) -  +  A  at #The result of the above code execution is as follows: -Time.struct_time (tm_year=2960, tm_mon=2, tm_mday=15, tm_hour=2, tm_min=35, tm_sec=45, tm_wday=4, tm_yday=46, tm_isdst=0) -1520176194.0 -2018-03-04 -Time.struct_time (tm_year=1993, tm_mon=5, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=139, tm_isdst=- 1)

Three. Time expansion

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 8 Import Time9 Print(Time.asctime (Time.localtime ()))#converts a structured time into a string time. Ten Print(Time.ctime (565656446))#converts a timestamp into a string time.  One  ATime.sleep (2)#lets the program pause for 2 seconds to simulate I/O blocking without consuming CPU resources.  -  -  the  -  - #The result of the above code execution is as follows: -Sun Mar 4 23:11:16 2018 +Sat Dec 5 06:47:26 1987

Four. Time module small test sledgehammer

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 Import Time8 9s ="2018-03-04"Ten  One defChangetime (string,number,format="%y-%m-%d"): AStructtime =time.strptime (String,format) -     Print(structtime) -Stamptime =time.mktime (structtime) the     Print(stamptime) -Newstamp = stamptime + 3600 * 24 * Number -Newstringtime =time.strftime (format, Time.localtime (Newstamp)) -     Print(newstringtime) +  -Changetime (s,3)#Estimated time after 3rd +  A  at  -  - #The result of the above code execution is as follows: -Time.struct_time (tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=63, Tm_isdst=-1) -1520092800.0 -2018-03-07

Five. DateTime INTRODUCTION

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 ImportTime,datetime7 8 Print(Datetime.datetime.now ())#Print current system time9 Ten Print(Datetime.date.fromtimestamp (Time.time ()))#The timestamp is converted directly into a date format such as: 2018-03-04 One  A Print(Datetime.datetime.now () + Datetime.timedelta (3))#Current time + 3 days -  - Print(Datetime.datetime.now () + Datetime.timedelta (-3))#Current Time-3 days the  - Print(Datetime.datetime.now () + Datetime.timedelta (hours=3))#Current time + 3 hours -  - Print(Datetime.datetime.now () + Datetime.timedelta (minutes=30))#current time +30 minutes +  -C_time =Datetime.datetime.now () + Print(C_time.replace (minute=3,hour=2))##时间替换 A  at  -  -  - #The result of the above code execution is as follows: -2018-03-04 23:12:13.477416 -2018-03-04 in2018-03-07 23:12:13.477416 -2018-03-01 23:12:13.477416 to2018-03-05 02:12:13.477416 +2018-03-04 23:42:13.477416 -2018-03-04 02:03:13.477416

Python common modules-time modules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.