day6 time和datetime模塊

来源:http://www.cnblogs.com/gengcx/archive/2017/05/29/6915855.html
-Advertisement-
Play Games

time模塊 time模塊提供各種操作時間的函數 #1、時間戳 1970年1月1日之後的秒 #2、元組 包含了:年、日、星期等... time.struct_time #3、格式化的字元串 2014-11-11 11:11 (1)asctime(p_tuple=None) def asctime(p ...


    time模塊

    time模塊提供各種操作時間的函數

    #1、時間戳    1970年1月1日之後的秒
  #2、元組 包含了:年、日、星期等... time.struct_time
  #3、格式化的字元串    2014-11-11 11:11

    (1)asctime(p_tuple=None)

    def asctime(p_tuple=None): # real signature unknown; restored from __doc__
    """
    asctime([tuple]) -> string

    Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
    When the time tuple is not present, current time as returned by localtime()
    is used.
    """
    return ""
    asctime()返回當前系統的時間,如下:

    >>> time.asctime()
  'Sun May 28 13:32:04 2017'

    (2)clock()

    def clock(): # real signature unknown; restored from __doc__
    """
    clock() -> floating point number

    Return the CPU time or real time since the start of the process or since
    the first call to clock(). This has as much precision as the system
    records.
    """
    return 0.0

    clock()返回CPU系統當前的時間,或者真實時間,從開始到第一次使用clock()。

    >>> time.clock()
  0.44741
    (3)clock_getres(clk_id)
    def clock_getres(clk_id): # real signature unknown; restored from __doc__
    """
    clock_getres(clk_id) -> floating point number

    Return the resolution (precision) of the specified clock clk_id.
    """
    return 0.0

    (4)clock_gettime(clk_id)

    def clock_gettime(clk_id): # real signature unknown; restored from __doc__
    """
    clock_gettime(clk_id) -> floating point number

    Return the time of the specified clock clk_id.
    """
    return 0.0

    (5)clock_settime(clk_id,time)

    def clock_settime(clk_id, time): # real signature unknown; restored from __doc__
    """
    clock_settime(clk_id, time)

    Set the time of the specified clock clk_id.
    """
    pass

    (6)ctime(seconds=None)

    def ctime(seconds=None): # known case of time.ctime
    """
    ctime(seconds) -> string

    Convert a time in seconds since the Epoch to a string in local time.
    This is equivalent to asctime(localtime(seconds)). When the time tuple is
    not present, current time as returned by localtime() is used.
    """
    return ""

    ctime()返回系統當前的時間:

    >>> time.ctime()
  'Sun May 28 13:55:39 2017'
    (7)get_clock_info(name)

    def get_clock_info(name): # real signature unknown; restored from __doc__
    """
    get_clock_info(name: str) -> dict

    Get information of the specified clock.
    """
    return {}

    (8)gmtime(seconds=None)

    def gmtime(seconds=None): # real signature unknown; restored from __doc__
    """
    gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,
    tm_sec, tm_wday, tm_yday, tm_isdst)

    Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.
    GMT). When 'seconds' is not passed in, convert the current time instead.

    If the platform supports the tm_gmtoff and tm_zone, they are available as
    attributes only.
    """
    pass

    gmtime(seconds=None)返回對應的時間格式,time.struct_time。

    >>> time.gmtime()
  time.struct_time(tm_year=2017, tm_mon=5, tm_mday=28, tm_hour=6, tm_min=1, tm_sec=28, tm_wday=6, tm_yday=148, tm_isdst=0)
    (9)localtime(seconds=None)    def localtime(seconds=None): # real signature unknown; restored from __doc__
    """
    localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
    tm_sec,tm_wday,tm_yday,tm_isdst)

    Convert seconds since the Epoch to a time tuple expressing local time.
    When 'seconds' is not passed in, convert the current time instead.
    """
    pass

    localtime()返回時間time.struct_time格式的日期。

    >>> time.localtime()
  time.struct_time(tm_year=2017, tm_mon=5, tm_mday=28, tm_hour=14, tm_min=3, tm_sec=58, tm_wday=6, tm_yday=148, tm_isdst=0)
    (10)mktime()

    def mktime(p_tuple): # real signature unknown; restored from __doc__
    """
    mktime(tuple) -> floating point number

    Convert a time tuple in local time to seconds since the Epoch.
    Note that mktime(gmtime(0)) will not generally return zero for most
    time zones; instead the returned value will either be equal to that
    of the timezone or altzone attributes on the time module.
    """
    return 0.0

    (11)monotonic()

    def monotonic(): # real signature unknown; restored from __doc__
    """
    monotonic() -> float

    Monotonic clock, cannot go backward.
    """
    return 0.0
 

    >>> time.monotonic()
  27333.713613735
    (12)perf_counter()
    def perf_counter(): # real signature unknown; restored from __doc__
    """
    perf_counter() -> float

    Performance counter for benchmarking.
    """
    return 0.0

    >>> time.perf_counter()
  27418.099319872
    (13)process_time()

    def process_time(): # real signature unknown; restored from __doc__
    """
    process_time() -> float

    Process time for profiling: sum of the kernel and user-space CPU time.
    """
    return 0.0

     >>> time.process_time()
  0.521978947

    (14)sleep(seconds)

  def sleep(seconds): # real signature unknown; restored from __doc__
    """
    sleep(seconds)

    Delay execution for a given number of seconds. The argument may be
    a floating point number for subsecond precision.
    """
    pass
    time.sleep(seconds)是程式停止運行一段時間。休眠。例如time.sleep(10)代表程式停止等待10秒鐘。

    (15)strftime(format,p_tuple=None)

    def strftime(format, p_tuple=None): # real signature unknown; restored from __doc__
    """
    strftime(format[, tuple]) -> string

    Convert a time tuple to a string according to a format specification.
    See the library reference manual for formatting codes. When the time tuple
    is not present, current time as returned by localtime() is used.

    Commonly used format codes:

    %Y Year with century as a decimal number.
    %m Month as a decimal number [01,12].
    %d Day of the month as a decimal number [01,31].
    %H Hour (24-hour clock) as a decimal number [00,23].
    %M Minute as a decimal number [00,59].
    %S Second as a decimal number [00,61].
    %z Time zone offset from UTC.
    %a Locale's abbreviated weekday name.
    %A Locale's full weekday name.
    %b Locale's abbreviated month name.
    %B Locale's full month name.
    %c Locale's appropriate date and time representation.
    %I Hour (12-hour clock) as a decimal number [01,12].
    %p Locale's equivalent of either AM or PM.

    Other codes may be available on your platform. See documentation for
    the C library strftime function.
    """
    return ""

    strftime(format,p_tuple)將時間進行格式轉換,只能轉換localtime()和gmtime()的struct_time格式時間情況;

    >>> time.strftime("%Y-%m-%d %p",time.localtime())
  '2017-05-28 PM'
  >>> time.strftime("%Y-%m-%d %I:%M:%S%p",time.localtime())
  '2017-05-28 02:23:21PM'
    日期格式的轉換,只能轉換struct_time的格式;

    >>> time.strftime("%a",time.localtime())    %a返回星期的簡寫
  'Sun'
    >>> time.strftime("%A",time.localtime())    %A返回日期的全拼
  'Sunday'

    (16)strptime(string,format)
    def strptime(string, format): # real signature unknown; restored from __doc__
    """
    strptime(string, format) -> struct_time

    Parse a string to a time tuple according to a format specification.
    See the library reference manual for formatting codes (same as
    strftime()).

    Commonly used format codes:

    %Y Year with century as a decimal number.
    %m Month as a decimal number [01,12].
    %d Day of the month as a decimal number [01,31].
    %H Hour (24-hour clock) as a decimal number [00,23].
    %M Minute as a decimal number [00,59].
    %S Second as a decimal number [00,61].
    %z Time zone offset from UTC.
    %a Locale's abbreviated weekday name.
    %A Locale's full weekday name.
    %b Locale's abbreviated month name.
    %B Locale's full month name.
    %c Locale's appropriate date and time representation.
    %I Hour (12-hour clock) as a decimal number [01,12].
    %p Locale's equivalent of either AM or PM.

    Other codes may be available on your platform. See documentation for
    the C library strftime function.
    """
    return struct_time

    strptime(string,format)將字元串的日期類型轉化為struct_time類型。

    >>> time.strptime("2017-5-18","%Y-%m-%d")
  time.struct_time(tm_year=2017, tm_mon=5, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=138, tm_isdst=-1)
    (17)time()

    def time(): # real signature unknown; restored from __doc__
    """
    time() -> floating point number

    Return the current time in seconds since the Epoch.
    Fractions of a second may be present if the system clock provides them.
    """
    return 0.0

    time()返回浮點數。

    >>> time.time()
  1495953536.3603268

    (18)tzset()

    def tzset(): # real signature unknown; restored from __doc__
    """
    tzset()

    Initialize, or reinitialize, the local timezone to the value stored in
    os.environ['TZ']. The TZ environment variable should be specified in
    standard Unix timezone format as documented in the tzset man page
    (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently
    fall back to UTC. If the TZ environment variable is not set, the local
    timezone is set to the systems best guess of wallclock time.
    Changing the TZ environment variable without calling tzset *may* change
    the local timezone used by methods such as localtime, but this behaviour
    should not be relied on.
    """
    pass

    datetime模塊   

    """

    import datetime

    datetime.date:表示日期的類。常用的屬性有year, month, day

    datetime.time:表示時間的類。常用的屬性有hour, minute, second, microsecond

    datetime.datetime:表示日期時間

    datetime.timedelta:表示時間間隔,即兩個時間點之間的長度

    timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

    strftime("%Y-%m-%d")

    """

    (1)datetime.date:表示日期的類。常用的屬性有year,month,day

    >>> datetime.date.today()
  datetime.date(2017, 5, 28)
    返回日期的格式情況,包含的屬性有year(年)、month(月)、日(day)。

    (2)datetime.time:表示時間的類。常用的屬性有hour,minute,second,microsecond

    >>> datetime.time(12,30,59,99)
  datetime.time(12, 30, 59, 99)

    返回日期時間的格式情況,如datetime.time()

    (3)datetime.datetime:表示日期時間

    >>> datetime.datetime(2016,5,12,7,59,59,99)
  datetime.datetime(2016, 5, 12, 7, 59, 59, 99)

    >>> datetime.datetime.now()
  datetime.datetime(2017, 5, 28, 15, 34, 1, 105235)

    >>> datetime.datetime.today()
  datetime.datetime(2017, 5, 28, 15, 35, 9, 384407)

    (4)datetime.timedelta:表示時間間隔,兩個時間點之間的長度

    (5)timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

    (6)strftime("%Y-%m-%d")

    實例:

    >>> now_date = datetime.datetime.now() + datetime.timedelta(days=10)      (1)比現在日期多十天
  >>> now_date
  datetime.datetime(2017, 6, 7, 15, 37, 7, 936368)

    >>> str_to_date = datetime.datetime.strptime("16/11/17 16:30","%d/%m/%y %H:%M")   (2)將字元串時間格式化轉化為時間
  >>> str_to_date
  datetime.datetime(2017, 11, 16, 16, 30)

    >>> new_date = datetime.datetime.now() + datetime.timedelta(hours=-10)     (3)比現在時間少10個小時
  >>> new_date
  datetime.datetime(2017, 5, 28, 20, 1, 11, 805686)


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 閱讀目錄 1. 介紹 2. 軟體準備 3. 建立SVN Server倉庫 4. 配置安裝PHP&IF.SVNadmin 5. 啟動服務 1.介紹 公司最近想把Windows server平臺的SVN遷移到Linux平臺;這邊經過測試成功,所以寫個隨筆記錄一下 今天寫的是CentOS7上搭建基於Apa ...
  • 1.Lazy<T>的使用 無意間看到一段代碼,在創建對象的時候使用了Lazy,顧名思義Lazy肯定是延遲載入,那麼它具體是如何創建對象,什麼時候創建對象了? 先看這段示列代碼: 使用非常簡單,把 OrderService 放到Lazy<T> 中,然後 _orderSrv.Value 的時候才真正創建 ...
  • 一般拿Timer和Quartz相比較的,簡直就是對Quartz的侮辱,兩者的功能根本就不在一個層級上,如本篇介紹的Quartz強大的集群機制,可以採用基於 sqlserver,mysql的集群方案,當然還可以在第三方插件的基礎上實現quartz序列化到熱炒的mongodb,redis,震撼力可想而知 ...
  • C# 7.0已經出來一段時間了,大家都知道新特性裡面有個對元組的優化:ValueTuple。這裡利用詳盡的例子詳解Tuple VS ValueTuple(元組類VS值元組),10分鐘讓你更瞭解ValueTuple的好處和用法。 如果您對Tuple足夠瞭解,可以直接跳過章節”回顧Tuple”,直達章節 ...
  • 最近需要做一個列印的功能,於是在網上找到了這麼一個方法。 以上就是全部代碼了,調用就很簡單了,方法如下: ...
  • 在基於“less rope to hang yourself with”思想下,.NET 框架沒有給開發提供很多太多的配置選項。但在大多數情況下,GC會跟你的硬體配置,及可用資源以及程式自己的行為做調整。當然也提供一些高級的配置使用,但這取決於你程式的類型。 ...
  • OS模塊 提供對操作系統進行調用的介面 (1)os.getcwd() 獲取當前工作目錄,即當前python腳本工作的目錄路徑 >>> os.getcwd() 獲取Python當前腳本工作的目錄路徑 '/home/zhuzhu' (2)os.chdir("dirname") 改變當前腳本工作目錄;相當 ...
  • random 我們經常看到網站的隨機驗證碼,這些都是由隨機數生成的,因此我們需要瞭解一下隨機數的模塊。如何生成隨機數。 random 生成隨機數 random.random() 生成0-1之間的小數 >>> import random >>> random.random() 0.7386445925 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...