Sha256: a19e33f31b0a737863d442670a1d1f53b6cd1bea8520a66435d69a10b3b64a86

Contents?: true

Size: 824 Bytes

Versions: 10

Compression:

Stored size: 824 Bytes

Contents

require 'active_support/time'
require 'ndr_support/ourdate'

# Convert a string into a time value (timestamp)
# (helped by String.thetime)
class Ourtime
  attr_reader :thetime

  def self.zone
    @zone ||= ActiveSupport::TimeZone.new('London')
  end

  # TODO: deprecate this...
  def initialize(x = nil)
    if x.is_a?(Time)
      @thetime = x
    elsif x.is_a?(Date)
      @thetime = x.to_time
    elsif x.is_a?(String)
      self.source = x
    else
      @thetime = nil
    end
  end

  def to_s
    @thetime ? @thetime.to_time.to_formatted_s(:ui) : ''
  end

  def empty?
    # An unspecified time will be empty. A valid time will not.
    @thetime.nil?
  end

  private

  def source=(s)
    @thetime = zone.parse(s)
  end

  def zone
    # `delegate` doesn't work for this on Rails 3.2
    self.class.zone
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ndr_support-5.10.4 lib/ndr_support/ourtime.rb
ndr_support-5.10.3 lib/ndr_support/ourtime.rb
ndr_support-5.10.2 lib/ndr_support/ourtime.rb
ndr_support-5.10.1 lib/ndr_support/ourtime.rb
ndr_support-5.10.0 lib/ndr_support/ourtime.rb
ndr_support-5.9.7 lib/ndr_support/ourtime.rb
ndr_support-5.9.6 lib/ndr_support/ourtime.rb
ndr_support-5.9.5 lib/ndr_support/ourtime.rb
ndr_support-5.9.4 lib/ndr_support/ourtime.rb
ndr_support-5.9.3 lib/ndr_support/ourtime.rb