@mytime = Time.zone.now.in_time_zone("London")
# Creates a TimeWithZone instance (instead of just a Time instance)
@mytime.period.local_start = 2010-03-28T02:00:00+00:00 Local Standard Time
# This is when daylight savings time starts
@mytime.period.local_end = 2010-10-31T02:00:00+00:00 Local DST
# This is when dst ends
----------------------------------
@sometime = Time.parse('2010-3-27 01:00:00').in_time_zone("London")
# 2010-03-26 16:00:00 +0000
@sometime.period.std_offset
# 0
@mytime.period.local_after_start?(@sometime)
# false
@mytime.period.local_before_end?(@sometime)
# true
----------------------------------
@sometime = Time.parse('2010-3-29 05:00:00').in_time_zone("London")
# 2010-03-28 21:00:00 +0100
@sometime.period.std_offset
# 3600 << Note that std_offset now holds the offset time for daylight savings in seconds
@mytime.period.local_after_start?(@sometime)
# true
@mytime.period.local_before_end?(@sometime)
# true
----------------------------------
@sometime = Time.parse('2010-12-29 05:00:00').in_time_zone("London")
# 2010-12-28 20:00:00 +0000
@sometime.period.std_offset
# 0
@mytime.period.local_after_start?(@sometime)
# true
@mytime.period.local_before_end?(@sometime)
# false
2010/07/29
Find out when Daylight Savings Time (DST) starts and ends using Ruby (on Rails)
Recently encountered a requirement to find out whether a particular date falls within daylight savings time. After banging my head on the wall for a few hours trying to figure it out, I found that the ruby TimeWithZone object makes this quite simple.
2010/05/05
Rails ブラウザからタイムゾーンをゲット
javascript でブラウザからタイムゾーンをゲットする方法。
適当に .js ファイルを作り、ブラウザのオフセット時間をクッキーに入れる。例えば、set_tzoffset.js
document.cookie = 'tzoffset='+ (new Date()).getTimezoneOffset();全てのページで読み込みたく無い場合は、おなじみ content_for でヘッダーに js ファイルを追加
<% content_for :head do %> <%= javascript_include_tag 'set_tzoffset' %> <% end %>コントローラーで呼び出し
if cookies[:tzoffset] gmt_offset = ActiveSupport::TimeZone[-cookies[:tzoffset].to_i/60] # オフセットは「差」が「分」で取得されるため、マイナス掛けて60で割る。 end問題は、同じオフセットで複数のタイムゾーンがあるため、時間はあっていても、必ずしも正しいロケーションのものが取得できないこと。例えば、同じ gmt + 9 のオフセットでは、Tokyo, Osaka 等がある。うーん、こればかりは。。。。
2010/01/15
Rails datetime 設定
Unix timestamp を rails datetime に変更
@somedate = Time.at(1260519945).utc
DB風に表示
@date.to_s(:db) # 2009-12-11 08:25:45
strftime 備忘録
example: object.strftime("%Y/%m/%d %H:%M")
%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character
2010/01/07
Rails Timezone の設定色々
困ったときのRails timezone api
オフセット、またはタイムゾーン文字列からタイムゾーンを返す
※特定のタイムゾーンがRails に存在するかどうかチェックするのに便利。Cafetalkでは、旧システムからデータを移行する際に使用しました。
タイムゾーンの名前だけを表示
http://marklunds.com/articles/one/402
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#M001513
Timezone 基本設定
config/environments/production.rb 等、各環境後とに・・・config.time_zone = "Tokyo"ApplicationController でのタイムゾーン設定
class ApplicationController < ActionController::Base
before_filter :set_timezone
def set_timezone
Time.zone = session[:tz] if session[:tz]
end
end
そして、ログインに使うコントローラーで session[:tz] を設定
class User::LoginController < UserRootController
def index
if request.post?
if session[:user_id] = user.authenticate(params[:user][:email], params[:user][:password])
user = User.find(session[:user_id])
session[:tz] = user.time_zone if user.time_zone
flash[:notice] = t('logged_in')
if session[:return_to_path]
redirect_to session[:return_to_path]
else
redirect_to user_root_path
end
else
flash[:error] = t('login.failed')
session[:user_id] = nil
end
end
end
end
その他色々便利なタイムゾーン系メソッド
特定のタイムゾーンで表示する
sometime.in_time_zone("Hawaii")
# @user.time_zone にユーザーのタイムゾーンが入っている場合
sometime.in_time_zone(@user.time_zone)
オフセット、またはタイムゾーン文字列からタイムゾーンを返す
※特定のタイムゾーンがRails に存在するかどうかチェックするのに便利。Cafetalkでは、旧システムからデータを移行する際に使用しました。
ActiveSupport::TimeZone['Tokyo'] > (GMT+09:00) Tokyo ActiveSupport::TimeZone[8] ActiveSupport::TimeZone[+8] > (GMT+09:00) Osaka
タイムゾーンの名前だけを表示
ActiveSupport::TimeZone[+8].name > Osakaオフセットを表示
Time.zone.utc_offset/3600 > -5 など。。。フォームでタイムゾーンのプルダウンリストを表示
<%= f.time_zone_select( :time_zone, nil, :include_blank => true) %>http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/
http://marklunds.com/articles/one/402
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#M001513
登録:
投稿 (Atom)
