module PowerTools module Builtins module Time DATE_FORMATS = { :time => '%H:%M', :date => '%d/%m/%y' } module Utilities def to(to, with_time=false, format_string="%H:%M %d %b '%y") fp = format_string.split ' ' from_date = with_time ? "#{self.part_format(fp[0,1])} " : '' to_date = with_time ? "#{to.part_format(fp[0,1])} " : '' from_date << self.day.ordinalize to_date << to.day.ordinalize if self.year != to.year "#{from_date} #{self.part_format(fp[-2,2])} - #{to_date} #{to.part_format(fp[-2,2])}" elsif self.month != to.month || (with_time && self.day != to.day) "#{from_date} #{self.part_format(fp[-2,1])} - #{to_date} #{to.part_format(fp[-2,1])} #{to.part_format(fp[-1,1])}" elsif self.day != to.day "#{from_date} - #{to_date} #{to.part_format(fp[-2,2])}" elsif !with_time "#{from_date} #{to.part_format(fp[-2,2])}" else "#{self.part_format(fp[0,1])} - #{to_date} #{to.part_format(fp[-2,2])}" end end def part_format(parts) strftime(parts.join(' ')) end end end module Integer def of(thing) if self == 1 "1 #{thing}" else "#{self.to_s} #{thing.pluralize}" end end end end end Integer.class_eval do include PowerTools::Builtins::Integer end Time.class_eval do include PowerTools::Builtins::Time::Utilities end ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(PowerTools::Builtins::Time::DATE_FORMATS)