class TwitterCldr::Localized::LocalizedDateTime

Attributes

calendar_type[R]
timezone[R]

Public Class Methods

new(obj, locale, options = {}) click to toggle source
Calls superclass method
# File lib/twitter_cldr/localized/localized_datetime.rb, line 20
def initialize(obj, locale, options = {})
  super
  @calendar_type = options[:calendar_type] || TwitterCldr::DEFAULT_CALENDAR_TYPE
  @timezone = options[:timezone] || "UTC"
end
types() click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 15
def types
  TwitterCldr::DataReaders::CalendarDataReader.types
end

Public Instance Methods

additional_formats() click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 71
def additional_formats
  data_reader_for(nil).additional_format_selector.patterns
end
ago(options = {}) click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 57
def ago(options = {})
  base_time = (options[:base_time] || Time.now).gmtime
  seconds = self.to_time(base_time).base_obj.gmtime.to_i - base_time.to_i
  raise ArgumentError.new('Start date is after end date. Consider using "until" function.') if seconds > 0
  TwitterCldr::Localized::LocalizedTimespan.new(seconds, options.merge(locale: @locale))
end
to_additional_s(additional_format) click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 35
def to_additional_s(additional_format)
  data_reader = data_reader_for(:additional, {
    additional_format: additional_format
  })

  tokens = if data_reader.tokenizer.respond_to?(:full_tokenize)
    data_reader.tokenizer.full_tokenize(data_reader.pattern)
  else
    data_reader.tokenizer.tokenize(data_reader.pattern)
  end

  data_reader.formatter.format(tokens, base_in_timezone, chain_params)
end
to_date() click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 79
def to_date
  LocalizedDate.new(@base_obj, @locale, chain_params)
end
to_s() click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 75
def to_s
  to_default_s
end
to_time(base = Time.now) click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 83
def to_time(base = Time.now)
  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 * 1_000_000
  )

  LocalizedTime.new(time, @locale, chain_params)
end
to_timespan(options = {}) click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 51
def to_timespan(options = {})
  base_time = options[:base_time] || Time.now
  seconds = (self.to_time.base_obj.to_i - base_time.to_i).abs
  TwitterCldr::Localized::LocalizedTimespan.new(seconds, options.merge(locale: @locale, direction: :none))
end
until(options = {}) click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 64
def until(options = {})
  base_time = (options[:base_time] || Time.now).gmtime
  seconds = self.to_time(base_time).base_obj.gmtime.to_i - base_time.to_i
  raise ArgumentError.new('End date is before start date. Consider using "ago" function.') if seconds < 0
  TwitterCldr::Localized::LocalizedTimespan.new(seconds, options.merge(locale: @locale))
end
with_timezone(timezone) click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 99
def with_timezone(timezone)
  self.class.new(@base_obj, @locale, chain_params.merge(timezone: timezone))
end

Protected Instance Methods

base_in_timezone() click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 118
def base_in_timezone
  timezone_info.utc_to_local(@base_obj.new_offset(0))
end
chain_params() click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 114
def chain_params
  { calendar_type: @calendar_type, timezone: @timezone }
end
data_reader_for(type, options = {}) click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 105
def data_reader_for(type, options = {})
  TwitterCldr::DataReaders::DateTimeDataReader.new(
    locale, options.merge({
      calendar_type: calendar_type,
      type: type
    })
  )
end
timezone_info() click to toggle source
# File lib/twitter_cldr/localized/localized_datetime.rb, line 122
def timezone_info
  (@@timezone_info ||= {})[@timezone] ||= TZInfo::Timezone.get(@timezone)
end