zoukankan      html  css  js  c++  java
  • python之time模块和hashlib模块

    一、time模块

    import time
    print(time.strftime('%Y-%m-%d %H:%M:%S'))#获取当前的格式化时间,time.strftime(format)
    print(time.time())#获取当前的时间戳
    #第一种:将格式化的时间转换成时间戳
    #思路:先将格式化时间转成时间元组,然后再转成时间戳
    # time_tuple=time.strptime('2019-02-18 15:15:32','%Y-%m-%d %H:%M:%S')#将格式化时间转换成时间元组,time.strptime(string[,format]
    # print(time.mktime(time_tuple))
    #编写一个将格式化时间转换成时间戳的函数
    def str_to_timestamp(time_str=None,format='%Y-%m-%d %H:%M:%S'):
        if time_str:
            time_tuple=time.strptime(time_str,format)
            timestamp=time.mktime(time_tuple)
        else:
            timestamp=time.time()
        return int(timestamp)
    
    #第二种,将时间戳转换成格式化时间
    #思路,首先将时间戳转换成时间元组,然后再转成格式化时间
    # time_struct=time.gmtime(time.time())#将时间戳转换成时间元组,标准时区
    # res=time.localtime(time.time())#将时间戳转换成时间元组,当地时区
    # res2=time.strftime('%Y-%m-%d %H:%M:%S',time_struct)
    # print(res2)
    #编写一个将时间戳转成格式化时间的代码
    def timestamp_to_str(timestamp=None,format='%Y-%m-%d %H:%M:%S'):
        if timestamp:
            time_struct=time.gmtime(timestamp)
            res=time.strftime(format,time_struct)
        else:
            res=time.strftime(format)
        return res

    二、hashlib模块 

    该模块用于加密操作

    import hashlib
    password='lyh123456'
    e=password.encode()#md5加密,必须先转换成二进制才能加密
    m=hashlib.md5(e)
    m1=hashlib.sha1(password.encode())
    print(m)
    print(m.hexdigest())#加密后的16进制结果
    print(m1.hexdigest())
  • 相关阅读:
    access将一个表中的记录插入到另一个表中
    在Windows Azure中使用CQRS
    Hazelcast 2.0发布,推出堆外存储和分布式备份
    Telefónica与Mozilla携手开拓首个开放互联网设备
    拥有完整硬件访问权限的本地Silverlight
    Mozilla BrowserQuest
    Doclist压缩方法简介
    CodePlex提供Git支持
    主流浏览器版本发布历史
    access截取字符
  • 原文地址:https://www.cnblogs.com/balllyh/p/10396249.html
Copyright © 2011-2022 走看看