- #
- A
- B
- C
- D
- E
- F
- G
- I
- L
- M
- N
- R
- S
- T
- U
-
- usec,
- utc,
- utc?,
- utc_offset
類別公開方法
civil_from_format(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0) 連結
傳回 DateTime
,其中包含給定年份的當地偏移量,如果格式為當地,否則偏移量為零。
DateTime.civil_from_format :local, 2012
# => Sun, 01 Jan 2012 00:00:00 +0300
DateTime.civil_from_format :local, 2012, 12, 17
# => Mon, 17 Dec 2012 00:00:00 +0000
來源: 顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 73 def self.civil_from_format(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0) if utc_or_local.to_sym == :local offset = ::Time.local(year, month, day).utc_offset.to_r / 86400 else offset = 0 end civil(year, month, day, hour, min, sec, offset) end
current() 連結
當設定 Time.zone
或 config.time_zone
時,傳回 Time.zone.now.to_datetime
,否則傳回 Time.now.to_datetime
。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 10 def current ::Time.zone ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime end
執行個體公開方法
<=>(other) 連結
在 DateTime#<=>
上分層其他行為,以便 Time
和 ActiveSupport::TimeWithZone
執行個體可以與 DateTime
進行比較。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 208 def <=>(other) if other.respond_to? :to_datetime super other.to_datetime rescue nil else super end end
acts_like_date?() 連結
將鴨子類型化為類似的日期類別。請參閱 Object#acts_like?
。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/acts_like.rb, line 8 def acts_like_date? true end
acts_like_time?() 連結
將鴨子類型化為類似的時間類別。請參閱 Object#acts_like?
。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/acts_like.rb, line 13 def acts_like_time? true end
advance(options) 連結
使用 Date
為年、月和日提供精確的 Time
計算。options
參數採用包含下列任一金鑰的雜湊::years
、:months
、:weeks
、:days
、:hours
、:minutes
、:seconds
。
就像 Date#advance
,增量會依時間單位從最大到最小順序套用。此順序可能會影響月底附近的結果。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 82 def advance(options) unless options[:weeks].nil? options[:weeks], partial_weeks = options[:weeks].divmod(1) options[:days] = options.fetch(:days, 0) + 7 * partial_weeks end unless options[:days].nil? options[:days], partial_days = options[:days].divmod(1) options[:hours] = options.fetch(:hours, 0) + 24 * partial_days end d = to_date.advance(options) datetime_advanced_by_date = change(year: d.year, month: d.month, day: d.day) seconds_to_advance = \ options.fetch(:seconds, 0) + options.fetch(:minutes, 0) * 60 + options.fetch(:hours, 0) * 3600 if seconds_to_advance.zero? datetime_advanced_by_date else datetime_advanced_by_date.since(seconds_to_advance) end end
ago(seconds) 連結
傳回表示若干秒前的時間的新 DateTime
。請勿將此方法與 x.months 合併使用,請改用 months_ago!
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 109 def ago(seconds) since(-seconds) end
beginning_of_day() 連結
傳回一個新的 DateTime
,表示一天的開始 (0:00)。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 122 def beginning_of_day change(hour: 0) end
beginning_of_hour() 連結
傳回一個新的 DateTime
,表示一小時的開始 (hh:00:00)。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 146 def beginning_of_hour change(min: 0) end
beginning_of_minute() 連結
傳回一個新的 DateTime
,表示一分鐘的開始 (hh:mm:00)。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 158 def beginning_of_minute change(sec: 0) end
change(選項) 連結
傳回一個新的 DateTime
,其中一個或多個元素已根據 選項
參數進行變更。時間選項 (:hour
、:min
、:sec
) 會遞迴重置,因此如果只傳遞小時,則會將分鐘和秒設為 0。如果傳遞小時和分鐘,則會將秒設為 0。選項
參數採用雜湊,其中包含下列任一金鑰::year
、:month
、:day
、:hour
、:min
、:sec
、:offset
、:start
。
DateTime.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => DateTime.new(2012, 8, 1, 22, 35, 0)
DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => DateTime.new(1981, 8, 1, 22, 35, 0)
DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => DateTime.new(1981, 8, 29, 0, 0, 0)
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 51 def change(options) if new_nsec = options[:nsec] raise ArgumentError, "Can't change both :nsec and :usec at the same time: #{options.inspect}" if options[:usec] new_fraction = Rational(new_nsec, 1000000000) else new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000)) new_fraction = Rational(new_usec, 1000000) end raise ArgumentError, "argument out of range" if new_fraction >= 1 ::DateTime.civil( options.fetch(:year, year), options.fetch(:month, month), options.fetch(:day, day), options.fetch(:hour, hour), options.fetch(:min, options[:hour] ? 0 : min), options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec) + new_fraction, options.fetch(:offset, offset), options.fetch(:start, start) ) end
end_of_day() 連結
傳回一個新的 DateTime
,表示一天的結束 (23:59:59)。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 140 def end_of_day change(hour: 23, min: 59, sec: 59, usec: Rational(999999999, 1000)) end
end_of_hour() 連結
傳回一個新的 DateTime
,表示一小時的結束 (hh:59:59)。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 152 def end_of_hour change(min: 59, sec: 59, usec: Rational(999999999, 1000)) end
end_of_minute() 連結
傳回一個新的 DateTime
,表示一分鐘的結束 (hh:mm:59)。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 164 def end_of_minute change(sec: 59, usec: Rational(999999999, 1000)) end
formatted_offset(冒號 = true,替代 UTC 字串 = nil) 連結
傳回一個格式化字串的 UTC 偏移量,或如果時區已經是 UTC,則傳回一個替代字串。
datetime = DateTime.civil(2000, 1, 1, 0, 0, 0, Rational(-6, 24))
datetime.formatted_offset # => "-06:00"
datetime.formatted_offset(false) # => "-0600"
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 55 def formatted_offset(colon = true, alternate_utc_string = nil) utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon) end
localtime(utc_offset = nil) 連結
傳回系統時區中同時時間的 Time
實例。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 170 def localtime(utc_offset = nil) utc = new_offset(0) Time.utc( utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec + utc.sec_fraction ).getlocal(utc_offset) end
middle_of_day() 連結
傳回一個新的 DateTime
,代表一天的中午 (12:00)
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 130 def middle_of_day change(hour: 12) end
nsec() 連結
傳回以奈秒為單位的秒數小數
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 98 def nsec (sec_fraction * 1_000_000_000).to_i end
readable_inspect() 連結
以人類可讀的方式覆寫預設的檢查方法,例如「星期一,2005 年 2 月 21 日下午 2:30:00 +0000」。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 60 def readable_inspect to_fs(:rfc822) end
seconds_since_midnight() 連結
傳回自 00:00:00 以來的秒數。
DateTime.new(2012, 8, 29, 0, 0, 0).seconds_since_midnight # => 0
DateTime.new(2012, 8, 29, 12, 34, 56).seconds_since_midnight # => 45296
DateTime.new(2012, 8, 29, 23, 59, 59).seconds_since_midnight # => 86399
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 20 def seconds_since_midnight sec + (min * 60) + (hour * 3600) end
seconds_until_end_of_day() 連結
傳回至 23:59:59 的秒數。
DateTime.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399
DateTime.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103
DateTime.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 29 def seconds_until_end_of_day end_of_day.to_i - to_i end
since(seconds) 連結
傳回一個新的 DateTime
,表示自執行個體時間以來經過的秒數。請勿將此方法與 x.months 搭配使用,請改用 months_since!
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 116 def since(seconds) self + Rational(seconds, 86400) end
subsec() 連結
傳回秒數小數部分,型態為 Rational
DateTime.new(2012, 8, 29, 0, 0, 0.5).subsec # => (1/2)
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 36 def subsec sec_fraction end
to_f() 連結
將 self
轉換為浮點數秒數,包含 Unix 紀元以來的微秒小數部分。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 83 def to_f seconds_since_unix_epoch.to_f + sec_fraction end
to_fs(format = :default) 連結
轉換為格式化字串。請參閱 Time::DATE_FORMATS 以取得預先定義的格式。
此方法的別名為 to_formatted_s
。
範例
datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
datetime.to_fs(:db) # => "2007-12-04 00:00:00"
datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
datetime.to_fs(:number) # => "20071204000000"
datetime.to_fs(:short) # => "04 Dec 00:00"
datetime.to_fs(:long) # => "December 04, 2007 00:00"
datetime.to_fs(:long_ordinal) # => "December 4th, 2007 00:00"
datetime.to_fs(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000"
datetime.to_fs(:iso8601) # => "2007-12-04T00:00:00+00:00"
將您自己的日期時間格式新增到 to_fs
DateTime
格式與 Time
共用。您可以將自己的格式新增到 Time::DATE_FORMATS hash。將格式名稱用作 hash 鍵,並將 strftime 字串或 Proc 實例(採用時間或日期時間引數)用作值。
# config/initializers/time_formats.rb
Time::DATE_FORMATS[:month_and_year] = '%B %Y'
Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 35 def to_fs(format = :default) if formatter = ::Time::DATE_FORMATS[format] formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter) else to_s end end
to_i() 連結
將 self
轉換為 Unix 紀元以來的整數秒數。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 88 def to_i seconds_since_unix_epoch.to_i end
to_time() 連結
傳回一個 Time
實例,其 UTC 位移與 self
相同,或傳回一個 Time
實例,表示當地系統時區中的相同時間,具體取決於 ActiveSupport.to_time_preserves_timezone
設定的設定。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/compatibility.rb, line 15 def to_time preserve_timezone ? getlocal(utc_offset) : getlocal end
usec() 連結
傳回秒數的微秒小數部分
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/conversions.rb, line 93 def usec (sec_fraction * 1_000_000).to_i end
utc() 連結
傳回 UTC 時區中同時時間的 Time
實例。
DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)) # => Mon, 21 Feb 2005 10:11:12 -0600
DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc # => Mon, 21 Feb 2005 16:11:12 UTC
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 184 def utc utc = new_offset(0) Time.utc( utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec + utc.sec_fraction ) end
utc?() 連結
如果 offset == 0
,則傳回 true
。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 197 def utc? offset == 0 end
utc_offset() 連結
傳回秒數中的位移值。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 202 def utc_offset (offset * 86400).to_i end