Python standard modules (time, datetime, and Hashlib modules)

Source: Internet
Author: User
Tags string format

One, Time,datetime module

Time-related operations

1 Import Time2Time.sleep (5)#==> Pause how many seconds3 Print(Time.time ())#==> Returns the timestamp, seconds after January 1, 1970, Seconds4 Print(Time.ctime ())#==> Returns the current time of the current time zone as a string5 Print(Time.ctime (Time.time ()-86400))#==> time conversion, current system time in advance one day6 Print(Time.gmtime (Time.time ()))#==> displays the current UTC time zone (0 o'clock Zone) in struct_time format.7obj =Time.gmtime (Time.time ())8 Print(obj.tm_year)#==> can obtain their tm_year in struct_time format time respectively, Tm_mon equivalent9 Print(Time.gmtime ())#==> If you do not fill in the seconds parameter, the default is Time.time ()Ten Print(Time.localtime ())#==> Displays the current time zone times in struct_time format, in second if the arguments are passed One Print(Time.mktime (Time.localtime ()))#==>, in contrast to the time.localtime () function, is to convert the Struct_time format to a timestamp format (seconds) A Print(Time.strftime ('%y-%m-%d', Time.localtime ()))#==> Formats the Struct_time as a specified string, passing in a parameter ① as a format, ② as a time object, Struct--time format - Print(Time.strptime ('2016-7-3 22:52','%y-%m-%d%h:%m'))#==> Convert string format to struct_time format -  the Importdatetime - Print(Datetime.date.today ())#==> Current Date - Print(Datetime.date.fromtimestamp (86400))#==> converting timestamps to date formats -Current = Datetime.datetime.now ()#Current Time + Print(current)#==> Print the current time string format - Print(Current.timetuple ())#==> return to struct_time format + Print(Current.replace (year=1988,month=11,day=27,hour=22))#==> Returns the current time, but the specified value is replaced A Print(Current.replace (1988,5,11)) at  - Print(Datetime.datetime.strptime ('2016/07/03','%y/%m/%d'))#==> Converting a string to a date format - #time plus minus - Print(Datetime.datetime.now () + Datetime.timedelta (days=10)) - #available '-', days=-10 also OK - #parameter days seconds weeks minutes hours microseconds milliseconds

Second, Hashlib module

Used for cryptographic related operations, in lieu of the MD5 module and the SHA module, mainly providing MD5 (), SHA1 (), sha224 (), sha256 (), sha384 (), sha512 () algorithm

The main methods are. Update (ARG); Digest (); Hexdigest (); Copy ()

1 ImportHashlib2 3obj =hashlib.md5 ()4Obj.update (Bytes ('What a fucking day', encoding='Utf-8'))#==> obj.update (b ' What a fucking day ')5 Print(Obj.digest ())6 Print(Obj.hexdigest ())#16 binary form display7 8 #Results:9B'2\x1d\x86\xc6\x17\xb5\xa2]\x81\x02ec\xc4\xf5\xb2t'Ten 321d86c617b5a25d81024543c4f5b254 One #Although the above algorithm is very powerful, but the time has the flaw, namely: through the collision library can reverse the solution. Therefore, it is necessary to add a custom key to the encryption algorithm to do encryption.  A ImportHashlib -m = Hashlib.md5 (b'cbcllsn478') -M.update (b'What a fucking day') the Print(M.digest ()) - Print(M.hexdigest ()) - #Results -B'[\XD5E\XDF\XB8NCS\X9F9Y\X13\XF9\XCE\X17\XFB' +5bd545dfb84e43539f397913f9ce17fb

Python standard modules (time, datetime, and Hashlib 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.