本文簡要介紹ruby語言中 DateTime類
的用法。
DateTime
Date
的子類,可以輕鬆處理日期、小時、分鍾、秒和偏移量。
DateTime
不考慮任何閏秒,不跟蹤任何夏令時規則。
DateTime
對象是用 DateTime::new
, DateTime::jd
, DateTime::ordinal
, DateTime::commercial
, DateTime::parse
, DateTime::strptime
, DateTime::now
, Time#to_datetime
等創建的。
require 'date'
DateTime.new(2001,2,3,4,5,6)
#=> #<DateTime: 2001-02-03T04:05:06+00:00 ...>
日、小時、分鍾或秒的最後一個元素可以是小數。假設小數的精度最多為納秒。
DateTime.new(2001,2,3.5)
#=> #<DateTime: 2001-02-03T12:00:00+00:00 ...>
可選參數 offset 表示本地時間和 UTC 之間的差異。例如,Rational(3,24)
表示提前 3 小時 UTC,Rational(-5,24)
表示落後 5 小時 UTC。偏移量應為 -1 到 +1,並且假定其精度最多為秒。默認值為零(等於 UTC)。
DateTime.new(2001,2,3,4,5,6,Rational(3,24))
#=> #<DateTime: 2001-02-03T04:05:06+03:00 ...>
偏移量也接受字符串形式:
DateTime.new(2001,2,3,4,5,6,'+03:00')
#=> #<DateTime: 2001-02-03T04:05:06+03:00 ...>
可選參數,日曆改革日 (start
),表示儒略日數,應為 2298874 到 2426355 或負/正無窮大。默認值為Date::ITALY
(2299161=1582-10-15)。
DateTime
對象有多種方法。請參閱每個參考。
d = DateTime.parse('3rd Feb 2001 04:05:06+03:30')
#=> #<DateTime: 2001-02-03T04:05:06+03:30 ...>
d.hour #=> 4
d.min #=> 5
d.sec #=> 6
d.offset #=> (7/48)
d.zone #=> "+03:30"
d += Rational('1.5')
#=> #<DateTime: 2001-02-04%16:05:06+03:30 ...>
d = d.new_offset('+09:00')
#=> #<DateTime: 2001-02-04%21:35:06+09:00 ...>
d.strftime('%I:%M:%S %p')
#=> "09:35:06 PM"
d > DateTime.new(1999)
#=> true
什麽時候應該使用 DateTime
,什麽時候應該使用 Time
?
曆史上 William Shakespeare 和 Miguel de Cervantes 死於同一天是一個普遍的誤解,以至於聯合國教科文組織將 4 月 23 日命名為 World Book Day because of this fact。但是,由於英格蘭尚未采用 Gregorian Calendar Reform(直到 1752 才采用),他們的死亡實際上相隔 10 天。由於 Ruby 的 Time
類實現了 proleptic Gregorian calendar 並且沒有日曆改革的概念,因此無法用 Time
對象來表達這一點。這是 DateTime
介入的地方:
shakespeare = DateTime.iso8601('1616-04-23', Date::ENGLAND)
#=> Tue, 23 Apr 1616 00:00:00 +0000
cervantes = DateTime.iso8601('1616-04-23', Date::ITALY)
#=> Sat, 23 Apr 1616 00:00:00 +0000
您已經可以看到一些奇怪的事情 - 一周中的日子不同。更進一步:
cervantes == shakespeare
#=> false
(shakespeare - cervantes).to_i
#=> 10
這表明他們實際上相隔 10 天去世(實際上距離塞萬提斯前一天去世但在 23 日被埋葬的時間已經過去了 11 天)。我們可以通過使用 gregorian
方法轉換它來看到莎士比亞的實際去世日期:
shakespeare.gregorian
#=> Tue, 03 May 1616 00:00:00 +0000
因此有一種說法認為,所有在 4 月 23 日 Stratford-upon-Avon 舉行的慶祝活動實際上都是錯誤的日期,因為英格蘭現在使用的是公曆。當我們跨越改革日期邊界時,您可以看到為什麽:
# start off with the anniversary of Shakespeare's birth in 1751
shakespeare = DateTime.iso8601('1751-04-23', Date::ENGLAND)
#=> Tue, 23 Apr 1751 00:00:00 +0000
# add 366 days since 1752 is a leap year and April 23 is after February 29
shakespeare + 366
#=> Thu, 23 Apr 1752 00:00:00 +0000
# add another 365 days to take us to the anniversary in 1753
shakespeare + 366 + 365
#=> Fri, 04 May 1753 00:00:00 +0000
如您所見,如果我們準確跟蹤自莎士比亞生日以來的solar years 的數量,那麽正確的周年紀念日應該是 5 月 4 日,而不是 4 月 23 日。
那麽什麽時候應該在Ruby中使用 DateTime
,什麽時候應該使用 Time
?幾乎可以肯定,您會想要使用 Time
,因為您的應用程序可能正在處理當前日期和時間。但是,如果您需要在曆史背景下處理日期和時間,您將需要使用 DateTime
以避免犯與 UNESCO 相同的錯誤。如果您還必須處理時區,那麽祝您好運 - 請記住,您可能會處理 local solar times ,因為直到 19 世紀鐵路的引入才需要 Standard Time 以及最終的時區。
相關用法
- Ruby DateTime jisx0301()用法及代碼示例
- Ruby DateTime.hour用法及代碼示例
- Ruby DateTime.jd用法及代碼示例
- Ruby DateTime.zone用法及代碼示例
- Ruby DateTime ordinal()用法及代碼示例
- Ruby DateTime.second用法及代碼示例
- Ruby DateTime to_datetime()用法及代碼示例
- Ruby DateTime civil()用法及代碼示例
- Ruby DateTime.second_fraction用法及代碼示例
- Ruby DateTime second_fraction()用法及代碼示例
- Ruby DateTime httpdate()用法及代碼示例
- Ruby DateTime zone()用法及代碼示例
- Ruby DateTime iso8601()用法及代碼示例
- Ruby DateTime to_s()用法及代碼示例
- Ruby DateTime.sec用法及代碼示例
- Ruby DateTime sec_fraction()用法及代碼示例
- Ruby DateTime rfc2822()用法及代碼示例
- Ruby DateTime offset()用法及代碼示例
- Ruby DateTime xmlschema()用法及代碼示例
- Ruby DateTime.xmlschema用法及代碼示例
- Ruby DateTime.iso8601用法及代碼示例
- Ruby DateTime.now用法及代碼示例
- Ruby DateTime.ordinal用法及代碼示例
- Ruby DateTime.rfc3339用法及代碼示例
- Ruby DateTime minute()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 DateTime類。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。