Time module and log module

Source: Internet
Author: User
Tags timedelta

Tag: Port-level deb Mat str imp color file alt

Import time

Print (Time.time ()) printing timestamp from UNIX release time
Print (Time.ctime ()) printing Current time
Print (Time.ctime (Time.time () -86400)) prints the current time minus one day

Time_obj = Time.gmtime ()
Print (Time_obj.tm_year,time_obj.tm_mon) Specifies that only the printed date

Print ("{a}-{b}". Format (A=time_obj.tm_year,b=time_obj.tm_mon))
Print (Time.localtime ()) printing Current time

Convert Time to Timestamp
Print (Time.mktime (time_obj))
Tm2= time.strptime ("2016-12-13", "%y-%m-%d")
Print (Time.mktime (TM2))

Time.sleep (3) pause for three seconds
Print ("111111111")

Specify format printing
TM = Time.strftime ("%y%m%d%h:%m:%s", Time.gmtime ())
TM1 = Time.strftime ("%y%m%d%h:%m:%s", Time.localtime ())
Tm2= time.strptime ("2016-12-13", "%y-%m-%d")
Print (Time.mktime (TM2))

Import datetime

Print (Datetime.date.today ()) printing results 2016-12-13
Print (Datetime.date.fromtimestamp (Time.time ()))

Print Current time
Current_time = Datetime.datetime.now ()
Print (Current_time)
Print (Current_time.timetuple ())

Print (Datetime.datetime.now () + Datetime.timedelta (days=-1)) prints the current time minus one day or one extra day
Print (Datetime.datetime.now () + Datetime.timedelta (hours=10)) minus one hour

Specify a date to print
A= Datetime.datetime.now ()
Print (A.replace (2016,12,10))

Specify format printing
Print (Datetime.datetime.strptime ("2016-12-13 19:23", "%y-%m-%d%h:%m"))

Judging size
Current_time = Datetime.datetime.now ()
t = current_time.replace (2015,5)
Print (current_time>t)

Import logging

Simple printing

Logging.basicconfig (filename= ' Example.log ', level=logging.info, defining file and INFO levels
format= '% (asctime) s% (message) s ', datefmt= '%y-%m-%d%i:%s%p ') print Date
Logging.debug (' This message shouid go to then log file ')
Logging.info (' S should this ')
Logging.warning (' and this too ')

Complex
Import logging

Logger = Logging.getlogger (' test-log ') defines the module name or user name
Logger.setlevel (logging.info) set Local level

ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG) set global level

FH = logging. Filehandler ("Access.log") Append file name
Fh.setlevel (logging. WARNING) level
Fh_err = logging. Filehandler ("Error.log") file
Fh_err.setlevel (logging. ERROR) level


Formatter = logging. Formatter ('% (asctime) s-% (filename) s-% (levelname) s-% (message) s ') time format
Formatter_for_file = logging. Formatter ('% (asctime) s-% (levelname) s-% (message) s ') time format

Ch.setformatter (Formatter) specify format
Fh.setformatter (formatter_for_file) Specify format
Fh_err.setformatter (Formatter) specify format

Logger.addhandler (CH) Registration
Logger.addhandler (FH) Registration
Logger.addhandler (fh_err) registration

Set Print content
Logger.debug (' Debug message ')
Logger.info (' info message ')
Logger.warn (' Warn message ')
Logger.error (' Error message ')
Logger.critical (' critical message ')






Time module and log module

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.