zoukankan      html  css  js  c++  java
  • zz Python datetime / time conversions

    Python datetime / time conversions

    Date: 2008-11-12  |   Tags: datetimepython   |   3 Comments
    from datetime import datetime
    import time

    #-------------------------------------------------
    # conversions to strings
    #-------------------------------------------------
    # datetime object to string
    dt_obj = datetime(2008, 11, 10, 17, 53, 59)
    date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
    print date_str

    # time tuple to string
    time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
    date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
    print date_str

    #-------------------------------------------------
    # conversions to datetime objects
    #-------------------------------------------------
    # time tuple to datetime object
    time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
    dt_obj = datetime(*time_tuple[0:6])
    print repr(dt_obj)

    # date string to datetime object
    date_str = "2008-11-10 17:53:59"
    dt_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
    print repr(dt_obj)

    # timestamp to datetime object in local time
    timestamp = 1226527167.595983
    dt_obj = datetime.fromtimestamp(timestamp)
    print repr(dt_obj)

    # timestamp to datetime object in UTC
    timestamp = 1226527167.595983
    dt_obj = datetime.utcfromtimestamp(timestamp)
    print repr(dt_obj)

    #-------------------------------------------------
    # conversions to time tuples
    #-------------------------------------------------
    # datetime object to time tuple
    dt_obj = datetime(2008, 11, 10, 17, 53, 59)
    time_tuple = dt_obj.timetuple()
    print repr(time_tuple) # string to time tuple date_str = "2008-11-10 17:53:59" time_tuple = time.strptime(date_str, "%Y-%m-%d %H:%M:%S") print repr(time_tuple) # timestamp to time tuple in UTC timestamp = 1226527167.595983 time_tuple = time.gmtime(timestamp) print repr(time_tuple) # timestamp to time tuple in local time timestamp = 1226527167.595983 time_tuple = time.localtime(timestamp) print repr(time_tuple) #------------------------------------------------- # conversions to timestamps #------------------------------------------------- # time tuple in local time to timestamp time_tuple = (2008, 11, 12, 13, 59, 27, 2, 317, 0) timestamp = time.mktime(time_tuple) print repr(timestamp) #------------------------------------------------- # results #------------------------------------------------- # 2008-11-10 17:53:59 # 2008-11-12 13:51:18 # datetime.datetime(2008, 11, 12, 13, 51, 18) # datetime.datetime(2008, 11, 10, 17, 53, 59) # datetime.datetime(2008, 11, 12, 13, 59, 27, 595983) # datetime.datetime(2008, 11, 12, 21, 59, 27, 595983) # (2008, 11, 10, 17, 53, 59, 0, 315, -1) # (2008, 11, 10, 17, 53, 59, 0, 315, -1) # (2008, 11, 12, 21, 59, 27, 2, 317, 0) # (2008, 11, 12, 13, 59, 27, 2, 317, 0) # 1226527167.0
  • 相关阅读:
    2020牛客暑期多校训练营(第三场)C-Operation Love(计算几何)
    洛谷 P3376 【模板】网络最大流
    2020牛客暑假多校训练营(第二场)F-Fake Maxpooling(单调队列)
    Codeforces Round #655 (Div. 2)【ABCD】(题解)
    Codeforces Round #648 (Div. 2)【ABCDEF】(题解)
    Codeforces Round #647 (Div. 2)
    Codeforces Round #646 (Div. 2)【ABCDE】(题解)
    Educational Codeforces Round 88 (Rated for Div. 2)【ABCDE】(题解)
    [蓝帽杯2020]一个利用data伪协议和include,file_get_contents写shell的web题
    [网鼎杯2020朱雀场] misc部分题解
  • 原文地址:https://www.cnblogs.com/yeyong/p/4962965.html
Copyright © 2011-2022 走看看