zoukankan      html  css  js  c++  java
  • Python内置模块-time && datatime

    Python提供两种时间表示方式,一种是时间戳,从1970年1月1日 0时开始。一种是struct_time数组格式,共有9个元素。
    import time
    print(time.time()) #返回时间戳,从1970年1月1日00:00:00开始。返回值为float。
    print(time.ctime()) #输出当前系统时间。
    print(time.ctime(time.time()-86400))#当前时间减去一天,返回字符串格式。
    print(time.gmtime()) #返回数组形式的struct_time,共九个元素。为UTC时间
    # tm_year, tm_mon, tm_mday, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst
    now = time.gmtime()
    print(now.tm_year)
    print(time.localtime()) #返回数组形式的struct_time,共九个元素。为本地时间
    print(time.mktime(time.localtime()))  #将struct_time转换成字符串格式的时间戳
    print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))  #将Struct_time格式转换为指定格式输出。UTC时间。
    print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))  #将Struct_time格式转换为指定格式输出。本地时间
    
    time.sleep(4) #程序休眠4s
     
    import datetime
    print(datetime.date.today()) #返回格式:2016-06-07
    print(datetime.date.fromtimestamp(time.time()))
    print(datetime.datetime.now()) #返回当前时间2016-06-07 15:34:24.228779
    print(datetime.datetime.now().timetuple()) #返回struct_time格式的时间
    new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
    new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
    new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
    new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
    

    print(new_date)
  • 相关阅读:
    自定义 ClassLoader
    HashCode 解析
    Unsafe与CAS
    ReentrantLock实现原理深入探究
    javaNIO:选择器--实践 Selector
    javaNIO:选择器--理论 Selector
    javaNIO:Socket通道
    CentOs 7 kong 2.3.X oss 自定义插件
    CentOs 7 kong 2.3.X oss 部署安装
    CentOS7 yum安装、配置PostgreSQL 9.6
  • 原文地址:https://www.cnblogs.com/ernest-zhang/p/5569379.html
Copyright © 2011-2022 走看看