zoukankan      html  css  js  c++  java
  • Python 时间整理

    在平常的代码中,我们常常需要与时间打交道。在Python中,与时间处理有关的模块就包括:time,datetime以及calendar。这篇文章,主要讲解time模块。

    在开始之前,首先要说明这几点:

    1. 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同。
    2. UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。
    3. 时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。返回时间戳方式的函数主要有time(),clock()等。
    4. 元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。下面列出这种方式元组中的几个元素

    import time

    time.time()单位为秒

    import datetime

    datetime.datetime.now() 为当前日期

    d1 = datetime.datetime.now()
    time.sleep(3)
    d2 = datetime.datetime.now()
    d = d2-d1 

         ### 产生的是 datetime.timedelta 对象

    d.days 天
    d.max 最大
    d.microseconds 微秒
    d.min 最小
    d.resolution
    d.seconds 秒

    注: d1, d2 还可以格式化成正常的日期时间。
    e.g:
    t_now = str(d1)  ### t_now = '2012-02-17 12:49:04.828000'  精确到毫秒
    t_now[:19]  ## 秒级别 '2012-02-17 12:49:04'
    t_now[:10]  ## 日期级别 '2012-02-17'
  • 相关阅读:
    tableView
    ios设计模式 设计一个应用程序 笔记
    Touching the Background to close the Keyboard
    fdfd
    fdffafadf
    Declaring the Action Method
    网易公开课IOS笔记 第二课
    getters and setters
    objective c
    Google编码规范 C++ Style Guide, JavaScript Style Guide, ObjectiveC Style Guide, and Python Style Guide
  • 原文地址:https://www.cnblogs.com/zmlctt/p/4315794.html
Copyright © 2011-2022 走看看