Sha256: 0ac4547061ab0ba8fe38c0614cd4fd36b94e35b110f34dbea45af1766417c0ea

Contents?: true

Size: 993 Bytes

Versions: 1

Compression:

Stored size: 993 Bytes

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

class DateTime
  def localize(locale = TwitterCldr.get_locale)
    TwitterCldr::LocalizedDateTime.new(self, locale)
  end
end

module TwitterCldr
  class LocalizedDateTime < LocalizedObject
    def method_missing(method, *args, &block)
      type = method.to_s.match(/to_(\w+)_s/)[1]
      if type && !type.empty? && TwitterCldr::Tokenizers::DateTimeTokenizer::VALID_TYPES.include?(type.to_sym)
        @formatter.format(@base_obj, :type => type.to_sym)
      else
        raise "Method not supported"
      end
    end

    def to_s
      self.to_default_s
    end

    def to_date
      LocalizedDate.new(Date.parse(@base_obj.strftime("%Y-%m-%dT%H:%M:%S%z")), @locale)
    end

    def to_time
      LocalizedTime.new(Time.parse(@base_obj.strftime("%Y-%m-%dT%H:%M:%S%z")), @locale)
    end

    protected

    def formatter_const
      TwitterCldr::Formatters::DateTimeFormatter
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter_cldr-1.1.0 lib/ext/calendars/datetime.rb