zoukankan      html  css  js  c++  java
  • 【python基础】time模块(库)方法汇总

    一、time

      python自带的管理时间的模块

    二、表示

      1、时间戳:某个时间距离1970-01-01 00:00:00的秒数*(计算机用来记录时间)
         时间戳 ——> 浮点数 基准时间:1970-01-01 00:00:00
      2、时间元组:(在程序中操作时间)
      3、时间字符串: '2019-08-15', '2019年8月15号', '08/15/2019'(方便人看的)

    三、方法

      1、time.sleep() : 睡眠功能, 让程序等待多少秒以后再执行
      2、time.time() : 时间戳
      3、time.localtime() : 获取当前时间的时间元组

      时间元组 VS 时间字符串
      1、time.strptime(string, format) 将时间字符串 --> 时间元组 参数1: 时间字符串 参数2: 字符串对应的格式

      2、time.strftime(fromat, t) 将时间元组 --> 时间字符串 参数1: 要转换的格式 参数2: 时间元组

      时间戳 --> 时间元组
      1、time.gmtime(secs) 将时间戳 ——> UTC时间元组
      2、time.localtime(secs) 将时间戳 ——> 当地时间-时间元组

      结构化时间
      1、time.asctime(t) 将时间元组结构化
      2、time.ctime(secs) 将时间戳结构化

     1 import time
     2 
     3 # print("肉丝打卤面")
     4 # time.sleep(3)  # 睡 3 s
     5 # print("鸡蛋西红柿打卤面")
     6 
     7 print(time.time())  # 1577465720.948886
     8 print(time.localtime())  # time.struct_time(tm_year=2019, tm_mon=12, tm_mday=28, tm_hour=0, tm_min=55, tm_sec=20, tm_wday=5, tm_yday=362, tm_isdst=0)
     9 
    10 
    11 # time.strptime(string, format)  时间字符串 ---> 时间元组
    12 s = "08-15-2019"
    13 t1 = time.strptime(s, "%m-%d-%Y")
    14 print(t1)  # time.struct_time(tm_year=2019, tm_mon=8, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=227, tm_isdst=-1)
    15 
    16 # time.strftime(format, t)  时间元组 ---> 时间字符串
    17 # 将时间元组t1 转换成 时间字符串 "19年08月15日"
    18 s1 = time.strftime("%y-%m-%d", t1)  # 19-08-15
    19 print(s1)  # 19-08-15
    20 
    21 # 时间戳 -- 时间元组之间的转换
    22 t = time.time()
    23 res1 = time.gmtime(t)  # 获取时间戳所对应的时间元组 UTC -- 格林尼治时间
    24 res2 = time.localtime(t)  # 获取时间戳所对应的时间元组
    25 print(res1)  # time.struct_time(tm_year=2019, tm_mon=12, tm_mday=27, tm_hour=16, tm_min=55, tm_sec=20, tm_wday=4, tm_yday=361, tm_isdst=0)
    26 print(res2)  # time.struct_time(tm_year=2019, tm_mon=12, tm_mday=28, tm_hour=0, tm_min=55, tm_sec=20, tm_wday=5, tm_yday=362, tm_isdst=0)
    27 
    28 # 结构化时间
    29 res3 = time.asctime(t1)
    30 res4 = time.ctime(t)
    31 print(res3)  # Thu Aug 15 00:00:00 2019
    32 print(res4)  # Sat Dec 28 00:55:20 2019
  • 相关阅读:
    (30)导入时如何定制spring-boot依赖项的版本【转载】【从零开始学Spring Boot】
    (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
    (28)SpringBoot启动时的Banner设置【从零开始学Spring Boot】
    POSIX 消息队列相关问题
    linux系统的7种运行级别
    如何判断是否开启超线程
    (26)改变自动扫描的包【从零开始学Spring Boot】
    (24)Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】
    (25)Spring Boot使用自定义的properties【从零开始学Spring Boot】
    《将博客搬至CSDN》
  • 原文地址:https://www.cnblogs.com/Tree0108/p/12110225.html
Copyright © 2011-2022 走看看