Import TimePrint(Time.time ())#1468639205.841486Print(Time.ctime ())#Sat Jul 11:20:05Print(Time.ctime (Time.time ()-86400))#Fri Jul 11:20:05Print(Time.gmtime ())#time.struct_time (tm_year=2016, tm_mon=7, tm_mday=16, tm_hour=3, tm_min=20, tm_sec=5, tm_wday=5, tm_yday=198, tm_ isdst=0)Print("{Year}-{month}-{day}". Format (Year=time.gmtime (). Tm_year,month=time.gmtime (). tm_mon,day=time.gmtime (). Tm_mday))#2016-7-16Print("{0}-{1}-{2}". Format (*(Time.gmtime (). Tm_year,time.gmtime (). Tm_mon,time.gmtime ( ). Tm_mday)))#2016-7-16Print("{Year}-{month}-{day}". Format (**{' Year': Time.gmtime (). Tm_year,'Month': Time.gmtime (). Tm_mon,' Day': Time.gmtime (). Tm_mday}))#2016-7-16Print(Time.localtime ())#time.struct_time (tm_year=2016, tm_mon=7, tm_mday=16, tm_hour=11, tm_min=43, Tm_sec=1, tm_wday=5, tm_yday=198, tm_ isdst=0)Print(Time.mktime (Time.localtime ()))#1468640581.0Time.sleep (4)Print("---"*)Print(Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ()))#2016-07-16 03:43:01Print(Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()))#2016-07-16 11:43:01Print(Time.strptime ("2016-07-16 11:36","%y-%m-%d%h:%m"))#time.struct_time (tm_year=2016, tm_mon=7, tm_mday=16, tm_hour=11, tm_min=36, tm_sec=0, tm_wday=5, tm_yday=198, tm_ Isdst=-1)#convert the time of a string to a timestamp: first convert the string to Struct_time and then use Mktime () to convertret = Time.strptime ("20160716150308","%y%m%d%h%m%s")Print(ret)#time.struct_time (tm_year=2016, tm_mon=7, tm_mday=16, tm_hour=15, tm_min=3, Tm_sec=8, tm_wday=5, tm_yday=198, tm_ Isdst=-1)Print(Time.mktime (ret))#1468652588.0
ImportdatetimePrint(Datetime.date.today ())#2016-07-16Print(Datetime.date.fromtimestamp (Time.time ()))#2016-07-16Print(Datetime.datetime.now ())#2016-07-16 11:54:20.143985Print(Datetime.datetime.now (). Timetuple ())#time.struct_time (tm_year=2016, tm_mon=7, tm_mday=16, tm_hour=11, tm_min=54, tm_sec=20, tm_wday=5, tm_yday=198, tm_ Isdst=-1)#time is often used to take timestamps#datetime is often used to take timenew_date1= Datetime.datetime.now () + Datetime.timedelta (days=10)Print(new_date1)#2016-07-26 12:07:53.078482New_date2= Datetime.datetime.now () + Datetime.timedelta (hours=10)Print(NEW_DATE2)#2016-07-16 22:07:53.078482
Logging
import logging Logging.debug ( Span style= "color: #800000;" > ' ' logging.warning ( " warning message " ) Logging.error ( " error message ' ' logging.critical ( " critical message )
Output:
WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message
Python defaults to printing information above the warn level to standard output
ImportLoggingImportLogginglogging.basicconfig ( level=logging. DEBUG, Format='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s', Datefmt='%a,%d%b%Y%h:%m:%s', filename='Log_module.log', FileMode='W') Logging.debug ('Debug Message') Logging.info ('Info Message') logging.warning ('warning Message') Logging.error ('error Message') logging.critical ('Critical Message')
The Logging.basicconfig () function can be seen to change the default behavior of the logging module through specific parameters, with the parameters available
FileName: Creates a filedhandler with the specified file name (the concept of handler is explained in the back) so that the log is stored in the specified file.
FileMode: File is opened by using this parameter when filename is specified, and the default value is "a" and can be specified as "W".
Format: Specifies the log display format used by handler.
DATEFMT: Specifies the date time format.
Level: Set the log levels for Rootlogger (which will explain the concepts behind)
Stream: Creates a streamhandler with the specified stream. You can specify the output to Sys.stderr,sys.stdout or to a file, and the default is Sys.stderr. If you list both the filename and stream two parameters, the stream parameter is ignored.
formatting strings that may be used in the format parameter:
% (name) s logger name
% (Levelno) s log level in digital form
% (levelname) s log level in text form
% (pathname) s calls the full pathname of the module of the log output function and may not have
% (filename) s The file name of the module that called the log output function
% (module) s call the module name of the log output function
% (FuncName) s Call the function name of the log output function
% (Lineno) d The line of code where the statement of the log output function is called
% (created) F current time, represented by the UNIX standard floating-point number representing the time
% (relativecreated) d when the log information is output, the number of milliseconds since logger was created
% (asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds
% (thread) d thread ID. Probably not.
% (threadname) s thread name. Probably not.
% (process) d process ID. Probably not.
% (message) s user-output message
Python Time and logging modules