require 'date' require 'when_exe' include When
コア拡張を利用する場合はコア拡張を参照
sdate = Date.new(1582,10,15) p sdate #=> #<Date: 1582-10-15 ((2299161j,0s,0n),+0s,2299161j)> gdate = when?(sdate) p gdate #=> 1582-10-15 p gdate.class #=> When::TM::CalDate p gdate.frame.iri #=> "http://hosi.org/When/CalendarTypes/Civil?reform_jdn=2299161" sdate = Date.new(1582,10,4) p sdate #=> #<Date: 1582-10-04 ((2299160j,0s,0n),+0s,2299161j)> gdate = when?(sdate) p gdate #=> 1582-10-04 p gdate.frame.iri #=> "http://hosi.org/When/CalendarTypes/Civil?reform_jdn=2299161" jdate = when?(sdate, :frame=>'Gregorian') p jdate #=> 1582-10-14 p jdate.frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian"
stime = Time.utc(2013, 5 ,30, 16, 20, 45) p stime #=> 2013-05-30 16:20:45 UTC gtime = when?(stime) p gtime #=> 2013-05-30T16:20:45.00Z p gtime.class #=> When::TM::DateAndTime p gtime.frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian" p gtime.clock.iri #=> "http://hosi.org/When/CalendarTypes/UTC"
stime = Time.local(2013, 5 ,30, 16, 20, 45) p stime #=> 2013-05-30 16:20:45 +0900 gtime = when?(stime) p gtime #=> 2013-05-30T16:20:45.00+09:00 p gtime.class #=> When::TM::DateAndTime p gtime.frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian" p gtime.clock.iri #=> "http://hosi.org/When/TM/Clock?label=+09:00"
sdate = DateTime.new(2013, 5 ,30, 16, 20, 45) p sdate #=> #<DateTime: 2013-05-30T16:20:45+00:00 ((2456443j,58845s,0n),+0s,2299161j)> gdate = when?(sdate) p gdate #=> 2013-05-30T16:20:45.00Z p gdate.class #=> When::TM::DateAndTime p gdate.frame.iri #=> "http://hosi.org/When/CalendarTypes/Civil?reform_jdn=2299161" p gdate.clock.iri #=> "http://hosi.org/When/CalendarTypes/UTC"
sdate = DateTime.new(2013, 5 ,30, 16, 20, 45, 0.375) p sdate #=> #<DateTime: 2013-05-30T16:20:45+09:00 ((2456443j,26445s,0n),+32400s,2299161j)> gdate = when?(sdate) p gdate #=> 2013-05-30T16:20:45.00+09:00 p gdate.class #=> When::TM::DateAndTime p gdate.frame.iri #=> "http://hosi.org/When/CalendarTypes/Civil?reform_jdn=2299161" p gdate.clock.iri #=> "http://hosi.org/When/TM/Clock?label=+09:00"
gdate = when?('1582-10-15') p gdate #=> 1582-10-15 p gdate.class #=> When::TM::CalDate p gdate.frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian" sdate = gdate.to_date p sdate #=> #<Date: 1582-10-15 ((2299161j,0s,0n),+0s,-Infj)> jdate = when?('1582-10-04', :frame=>'Julian') p jdate #=> 1582-10-04 p jdate.class #=> When::TM::CalDate p jdate.frame.iri #=> "http://hosi.org/When/CalendarTypes/Julian" sdate = jdate.to_date p sdate #=> #<Date: 1582-10-04 ((2299160j,0s,0n),+0s,+Infj)>
to_date による変換結果は原則としてグレゴリオ暦だが、変換元がCivilのユリウス暦期間かまたはJulianの場合のみユリウス暦
gtime = when?('2013-05-30T16:20:45') p gtime #=> 2013-05-30T16:20:45Z p gtime.class #=> When::TM::DateAndTime p gtime.frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian" p gtime.clock.iri #=> "http://hosi.org/When/CalendarTypes/UTC" stime = gtime.to_time p stime #=> 2013-05-31 01:20:45 +0900
if Object.const_defined?(:TZInfo) # TZInfo gem がインストールされていること gtime = when?('2013-05-30T16:20:45', :tz=>'Asia/Tokyo') p gtime #=> 2013-05-30T16:20:45+09:00 p gtime.class #=> When::TM::DateAndTime p gtime.frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian" p gtime.clock.tz_prop.timezone #=> #<TZInfo::DataTimezone: Asia/Tokyo> stime = gtime.to_time p stime #=> 2013-05-30 16:20:45 +0900 end to_time による変換結果は常にローカル時刻である
gdate = when?('2013-05-30T16:20:45') p gdate #=> 2013-05-30T16:20:45Z p gdate.class #=> When::TM::DateAndTime p gdate.frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian" p gdate.clock.iri #=> "http://hosi.org/When/CalendarTypes/UTC" sdate = gdate.to_datetime p sdate #=> #<DateTime: 2013-05-30T16:20:45+00:00 ((2456443j,58845s,0n),+0s,-Infj)>
if Object.const_defined?(:TZInfo) # TZInfo gem がインストールされていること gdate = when?('2013-05-30T16:20:45', :tz=>'Asia/Tokyo') p gdate #=> 2013-05-30T16:20:45+09:00 p gdate.class #=> When::TM::DateAndTime p gdate.frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian" p gtime.clock.tz_prop.timezone #=> #<TZInfo::DataTimezone: Asia/Tokyo> sdate = gdate.to_datetime p sdate #=> #<DateTime: 2013-05-30T16:20:45+09:00 ((2456443j,26445s,0n),+32400s,-Infj)> end