Class: TwitterCldr::Localized::LocalizedDateTime

Inherits:
LocalizedObject show all
Defined in:
lib/twitter_cldr/localized/localized_datetime.rb

Direct Known Subclasses

LocalizedDate, LocalizedTime

Instance Attribute Summary collapse

Attributes inherited from LocalizedObject

#base_obj, #formatter, #locale

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LocalizedObject

localize

Constructor Details

#initialize(obj, locale, options = {}) ⇒ LocalizedDateTime

Returns a new instance of LocalizedDateTime.



20
21
22
23
24
# 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

Instance Attribute Details

#calendar_typeObject (readonly)

Returns the value of attribute calendar_type.



12
13
14
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 12

def calendar_type
  @calendar_type
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



12
13
14
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 12

def timezone
  @timezone
end

Class Method Details

.typesObject



15
16
17
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 15

def types
  TwitterCldr::DataReaders::CalendarDataReader::TYPE_PATHS.keys
end

Instance Method Details

#additional_formatsObject



66
67
68
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 66

def additional_formats
  data_reader_for(nil).additional_format_selector.patterns
end

#ago(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 52

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) ⇒ Object



35
36
37
38
39
40
41
42
# 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 = data_reader.tokenizer.full_tokenize(data_reader.pattern)
  data_reader.formatter.format(tokens, base_in_timezone)
end

#to_dateObject



74
75
76
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 74

def to_date
  LocalizedDate.new(@base_obj, @locale, chain_params)
end

#to_sObject



70
71
72
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 70

def to_s
  to_default_s
end

#to_time(base = Time.now) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 78

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 * (RUBY_VERSION < '1.9' ? 86400000000 : 1000000)
  )

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

#to_timespan(options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 46

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 = {}) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
64
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 59

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) ⇒ Object



94
95
96
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 94

def with_timezone(timezone)
  self.class.new(@base_obj, @locale, chain_params.merge(:timezone => timezone))
end