Contents
Ruby – 日付と時刻
Rubyで日付と時刻を表します。1970年以前または2038年以降の日付を表すことができない場合があります。
この章では、最も必要と思われる日付と時間の概念を理解しています。
現在の日付と時刻の取得
以下は、現在の日付と時刻を取得する簡単な例です。
#!/usr/bin/ruby -w time1 = Time.new puts "Current Time : " + time1.inspect # Time.now is a synonym: time2 = Time.now puts "Current Time : " + time2.inspect
このようになります
Current Time : Mon Jun 02 12:02:39 -0700 2008 Current Time : Mon Jun 02 12:02:39 -0700 2008
日付と時刻のコンポーネントの取得
Timeオブジェクトを使用して、日付と時刻のさまざまなコンポーネントを取得できます。以下は同じものを示す例です –
#!/usr/bin/ruby -w time = Time.new # Components of a Time puts "Current Time : " + time.inspect puts time.year # => Year of the date puts time.month # => Month of the date (1 to 12) puts time.day # => Day of the date (1 to 31 ) puts time.wday # => 0: Day of week: 0 is Sunday puts time.yday # => 365: Day of year puts time.hour # => 23: 24-hour clock puts time.min # => 59 puts time.sec # => 59 puts time.usec # => 999999: microseconds puts time.zone # => "UTC": timezone name
コンパイルすると
Current Time : Mon Jun 02 12:03:08 -0700 2008 2008 6 2 1 154 12 3 8 247476 UTC
Time.utc、Time.gm、およびTime.local関数
これらの2つの関数を使用して、次のように標準形式で日付をフォーマットすることができます。
# July 8, 2008 Time.local(2008, 7, 8) # July 8, 2008, 09:10am, local time Time.local(2008, 7, 8, 9, 10) # July 8, 2008, 09:10 UTC Time.utc(2008, 7, 8, 9, 10) # July 8, 2008, 09:10:11 GMT (same as 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_a p 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_a puts Time.utc(*values)
これにより、次の結果が生成されます。
Mon Jun 02 12:15:36 UTC 2008
以下は、(プラットフォームに依存する)エポックタイムから内部的に秒単位で表される時間を得る方法です。
# Returns number of seconds since epoch time = Time.now.to_i # Convert number of seconds into Time object. Time.at(time) # Returns second since epoch which includes microseconds time = Time.now.to_f
タイムゾーンとサマータイム
Timeオブジェクトを使用して、次のようにタイムゾーンおよび夏時間に関するすべての情報を取得できます。
time = Time.new time.zone # => "UTC": return the timezone time.utc_offset # => 0: UTC is 0 seconds offset from UTC time.zone # => "PST" (or whatever your timezone is) time.isdst # => false: If UTC does not have DST. time.utc? # => true: if t is in UTC time zone time.localtime # Convert to local timezone. time.gmtime # Convert back to UTC. time.getlocal # Return a new Time object in local zone time.getutc # Return a new Time object in UTC
時間と日付の書式設定
日付と時刻の書式を設定する方法はさまざまです。ここでは、
#!/usr/bin/ruby -w time = Time.new puts time.to_s puts time.ctime puts time.localtime puts time.strftime("%Y-%m-%d %H:%M:%S")
これにより、次の結果が生成されます。
Mon Jun 02 12:35:19 -0700 2008 Mon Jun 2 12:35:19 2008 Mon Jun 02 12:35:19 -0700 2008 2008-06-02 12:35:19
時刻書式ディレクティブ
次の表のこれらのディレクティブは、Time.strftimeメソッドで使用されま
次のように時間とともに簡単な算術演算を実行できます。
now = Time.now # いまの時間 puts now past = now - 10 # 今から10秒前 puts past future = now + 10 # 今から10秒後 puts future diff = future - now # みらいの時間から今を引く puts diff
これにより、次の結果が生成されます。
Thu Aug 01 20:57:05 -0700 2013 Thu Aug 01 20:56:55 -0700 2013 Thu Aug 01 20:57:15 -0700 2013 10.0