在Ruby中處理日期和時間的教程_ruby專題

來源:互聯網
上載者:User

Time 類在 Ruby 中用於表示日期和時間。它是基於作業系統提供的系統日期和時間之上。該類可能無法表示 1970 年之前或者 2038 年之後的日期。

本教程將讓您熟悉日期和時間的所有重要的概念。
建立當前的日期和時間

下面是擷取當前的日期和時間的簡單一實例:

#!/usr/bin/ruby -w time1 = Time.new puts "Current Time : " + time1.inspect # Time.now 是一個同義字time2 = Time.nowputs "Current Time : " + time2.inspect

這將產生以下結果:

Current Time : Mon Jun 02 12:02:39 -0700 2008Current Time : Mon Jun 02 12:02:39 -0700 2008

擷取 Date & Time 組件

我們可以使用 Time 對象來擷取各種日期和時間的組件。請看下面的執行個體:

#!/usr/bin/ruby -w time = Time.new # Time 的組件puts "Current Time : " + time.inspectputs time.year  # => 日期的年份puts time.month  # => 日期的月份(1 到 12)puts time.day   # => 一個月中的第幾天(1 到 31)puts time.wday  # => 一周中的星期幾(0 是星期日)puts time.yday  # => 365:一年中的第幾天puts time.hour  # => 23:24 小時制puts time.min   # => 59puts time.sec   # => 59puts time.usec  # => 999999:微秒puts time.zone  # => "UTC":時區名稱

這將產生以下結果:

Current Time : Mon Jun 02 12:03:08 -0700 200820086211541238247476UTC

Time.utc、Time.gm 和 Time.local 函數

這些函數可用于格式化標準格式的日期,如下所示:

# July 8, 2008Time.local(2008, 7, 8) # July 8, 2008, 09:10am,本地時間Time.local(2008, 7, 8, 9, 10) # July 8, 2008, 09:10 UTCTime.utc(2008, 7, 8, 9, 10) # July 8, 2008, 09:10:11 GMT (與 UTC 相同)Time.gm(2008, 7, 8, 9, 10, 11) 

下面的執行個體在數組中擷取所有的組件:
[sec,min,hour,day,month,year,wday,yday,isdst,zone]

嘗試下面的執行個體:

#!/usr/bin/ruby -w time = Time.new values = time.to_ap values

這將產生以下結果:

[26, 10, 12, 2, 6, 2008, 1, 154, false, "MST"]

該數組可被傳到 Time.utc 或 Time.local 函數來擷取日期的不同格式,如下所示:

#!/usr/bin/ruby -w time = Time.new values = time.to_aputs Time.utc(*values)

這將產生以下結果:

Mon Jun 02 12:15:36 UTC 2008

下面是擷取時間的方式,從紀元以來的秒數(平台相關):

# 返回從紀元以來的秒數time = Time.now.to_i  # 把秒數轉換為 Time 對象Time.at(time) # 返回從紀元以來的秒數,包含微妙time = Time.now.to_f

時區和夏令時

您可以使用 Time 對象來擷取與時區和夏令時有關的所有資訊,如下所示:

time = Time.new # 這裡是解釋time.zone    # => "UTC":返回時區time.utc_offset # => 0:UTC 是相對於 UTC 的 0 秒位移time.zone    # => "PST"(或其他時區)time.isdst   # => false:如果 UTC 沒有 DST(夏令時)time.utc?    # => true:如果在 UTC 時區time.localtime # 轉換為本地時區time.gmtime   # 轉換回 UTCtime.getlocal  # 返回本地區中的一個新的 Time 對象time.getutc   # 返回 UTC 中的一個新的 Time 對象

格式化時間和日期

有多種方式格式化日期和時間。下面的執行個體示範了其中一部分:

#!/usr/bin/ruby -wtime = Time.new puts time.to_sputs time.ctimeputs time.localtimeputs time.strftime("%Y-%m-%d %H:%M:%S")

這將產生以下結果:

Mon Jun 02 12:35:19 -0700 2008Mon Jun 2 12:35:19 2008Mon Jun 02 12:35:19 -0700 20082008-06-02 12:35:19

時間格式化指令

下表所列出的指令與方法 Time.strftime 一起使用。

時間演算法

您可以用時間做一些簡單的算術,如下所示:

now = Time.now      # 目前時間puts now past = now - 10     # 10 秒之前。Time - number => Timeputs past future = now + 10    # 從現在開始 10 秒之後。Time + number => Timeputs future diff = future - now   # => 10 Time - Time => 秒數puts diff

這將產生以下結果:

Thu Aug 01 20:57:05 -0700 2013Thu Aug 01 20:56:55 -0700 2013Thu Aug 01 20:57:15 -0700 201310.0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.