Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Last active February 24, 2017 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixeltrix/d654f401f8438a2313a979b104fc5288 to your computer and use it in GitHub Desktop.
Save pixeltrix/d654f401f8438a2313a979b104fc5288 to your computer and use it in GitHub Desktop.
Script to output behaviours of Time, Date and DateTime across Ruby versions
require 'date'
require 'time'
ENV['TZ'] = 'America/New_York'
time = Time.new(2017, 1, 1, 0, 0, 0, "+02:00").freeze
date = Date.new(2017, 1, 1).freeze
datetime = DateTime.new(2017, 1, 1, 0, 0, 0, Rational(2, 24)).freeze
# Time
puts "Time#to_time | #{RUBY_VERSION} "
puts "--------------------------|-------"
puts "Maintains UTC offset | %-5s " % (time.utc_offset == time.to_time.utc_offset).to_s[0]
puts "Converts to +00:00 | %-5s " % (time.to_time.utc_offset.zero?).to_s[0]
puts "Converts to TZ | %-5s " % (time.to_time.utc_offset == -18000).to_s[0]
puts "Returns self | %-5s " % (time.object_id == time.to_time.object_id).to_s[0]
puts "Returns frozen | %-5s \n\n" % (time.to_time.frozen?).to_s[0]
puts "Time#to_datetime | #{RUBY_VERSION} "
puts "--------------------------|-------"
puts "Maintains UTC offset | %-5s " % (Rational(time.utc_offset, 86400) == time.to_datetime.offset).to_s[0]
puts "Converts to +00:00 | %-5s " % (time.to_datetime.offset.zero?).to_s[0]
puts "Converts to TZ | %-5s " % (time.to_datetime.offset == Rational(-5,24)).to_s[0]
puts "Returns self | %-5s " % (time.object_id == time.to_datetime.object_id).to_s[0]
puts "Returns frozen | %-5s \n\n" % (time.to_datetime.frozen?).to_s[0]
puts "Time#to_date | #{RUBY_VERSION} "
puts "--------------------------|-------"
puts "Returns self | %-5s " % (time.object_id == time.to_date.object_id).to_s[0]
puts "Returns frozen | %-5s \n\n" % (time.to_date.frozen?).to_s[0]
# Date
puts "Date#to_time | #{RUBY_VERSION} "
puts "--------------------------|-------"
puts "Maintains UTC offset | %-5s " % ("N/A")
puts "Converts to +00:00 | %-5s " % (date.to_time.utc_offset.zero?).to_s[0]
puts "Converts to TZ | %-5s " % (date.to_time.utc_offset == -18000).to_s[0]
puts "Returns self | %-5s " % (date.object_id == date.to_time.object_id).to_s[0]
puts "Returns frozen | %-5s \n\n" % (date.to_time.frozen?).to_s[0]
puts "Date#to_datetime | #{RUBY_VERSION} "
puts "--------------------------|-------"
puts "Maintains UTC offset | %-5s " % ("N/A")
puts "Converts to +00:00 | %-5s " % (date.to_datetime.offset.zero?).to_s[0]
puts "Converts to TZ | %-5s " % (date.to_datetime.offset == Rational(-5,24)).to_s[0]
puts "Returns self | %-5s " % (date.object_id == date.to_datetime.object_id).to_s[0]
puts "Returns frozen | %-5s \n\n" % (date.to_datetime.frozen?).to_s[0]
puts "Date#to_date | #{RUBY_VERSION} "
puts "--------------------------|-------"
puts "Returns self | %-5s " % (date.object_id == date.to_date.object_id).to_s[0]
puts "Returns frozen | %-5s \n\n" % (date.to_date.frozen?).to_s[0]
# DateTime
puts "DateTime#to_time | #{RUBY_VERSION} "
puts "--------------------------|-------"
puts "Maintains UTC offset | %-5s " % (datetime.offset * 86400 == datetime.to_time.utc_offset).to_s[0]
puts "Converts to +00:00 | %-5s " % (datetime.to_time.utc_offset.zero?).to_s[0]
puts "Converts to TZ | %-5s " % (datetime.to_time.utc_offset == -18000).to_s[0]
puts "Returns self | %-5s " % (datetime.object_id == datetime.to_time.object_id).to_s[0]
puts "Returns frozen | %-5s \n\n" % (datetime.to_time.frozen?).to_s[0]
puts "DateTime#to_datetime | #{RUBY_VERSION} "
puts "--------------------------|-------"
puts "Maintains UTC offset | %-5s " % (datetime.offset == datetime.to_datetime.offset).to_s[0]
puts "Converts to +00:00 | %-5s " % (datetime.to_datetime.offset.zero?).to_s[0]
puts "Converts to TZ | %-5s " % (datetime.to_datetime.offset == Rational(-5,24)).to_s[0]
puts "Returns self | %-5s " % (datetime.object_id == datetime.to_datetime.object_id).to_s[0]
puts "Returns frozen | %-5s \n\n" % (datetime.to_datetime.frozen?).to_s[0]
puts "DateTime#to_date | #{RUBY_VERSION} "
puts "--------------------------|-------"
puts "Returns self | %-5s " % (datetime.object_id == datetime.to_date.object_id).to_s[0]
puts "Returns frozen | %-5s \n\n" % (datetime.to_date.frozen?).to_s[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment