Sha256: 731431565e7e0f6dcf7cd7eefd72405dac754cb24c234a1d32b22a347069da35

Contents?: true

Size: 1.46 KB

Versions: 9

Compression:

Stored size: 1.46 KB

Contents

# encoding: UTF-8

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

module TwitterCldr
  module Localized

    class LocalizedDate < LocalizedDateTime
      def to_datetime(time = Time.now)
        dt = DateTime.parse("#{@base_obj.strftime("%Y-%m-%d")}T#{unwrap_time_obj(time).strftime("%H:%M:%S%z")}")
        LocalizedDateTime.new(dt, @locale, chain_params)
      end

      def to_time(base = Time.now)
        case @base_obj
          when Time
            LocalizedTime.new(@base_obj, @locale, chain_params)
          when DateTime
            utc_dt = @base_obj.new_offset(0)

            time = Time.gm(
              utc_dt.year,
              utc_dt.month,
              utc_dt.day,
              utc_dt.hour,
              utc_dt.min,
              utc_dt.sec,
              utc_dt.sec_fraction * (RUBY_VERSION < '1.9' ? 86400000000 : 1000000)
            )

            LocalizedTime.new(time, @locale, chain_params)
          else
            nil
        end
      end

      protected

      def unwrap_time_obj(time)
        time.is_a?(LocalizedTime) ? time.base_obj : time
      end

      def base_in_timezone
        time = unwrap_time_obj(to_time)
        timezone_info.utc_to_local(time.is_a?(DateTime) ? time.new_offset(0) : time.utc)
      end

      def data_reader_for(type)
        TwitterCldr::DataReaders::DateDataReader.new(locale, {
          :calendar_type => calendar_type,
          :type => type
        })
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
twitter_cldr-3.0.8 lib/twitter_cldr/localized/localized_date.rb
twitter_cldr-3.0.7 lib/twitter_cldr/localized/localized_date.rb
twitter_cldr-3.0.6 lib/twitter_cldr/localized/localized_date.rb
twitter_cldr-3.0.5 lib/twitter_cldr/localized/localized_date.rb
twitter_cldr-3.0.4 lib/twitter_cldr/localized/localized_date.rb
twitter_cldr-3.0.3 lib/twitter_cldr/localized/localized_date.rb
twitter_cldr-3.0.2 lib/twitter_cldr/localized/localized_date.rb
twitter_cldr-3.0.1 lib/twitter_cldr/localized/localized_date.rb
twitter_cldr-3.0.0 lib/twitter_cldr/localized/localized_date.rb