Sinatra 處理時間和日期

2023-12-20 15:43 更新

sinatra 提供一個 ?time_for? 的輔助方法,用從給定的值中來生成 Time 對象。其也可用來轉(zhuǎn)換 DateTime,Date 以及相似的類:

get '/' do
  pass if Time.now > time_for('Dec 23, 2012')
  "still time"
end

該方法在?expires? ,?last_modified? 和 ?akin? 內(nèi)部使用。通過覆蓋? time_for? 方法,可在應用成很容易的擴展這些方法的行為:

helpers do
  def time_for(value)
    case value
    when :yesterday then Time.now - 24*60*60
    when :tomorrow  then Time.now + 24*60*60
    else super
    end
  end
end

get '/' do
  last_modified :yesterday
  expires :tomorrow
  "hello"
end


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號