Class: TwitterCldr::LocalizedDateTime

Inherits:
LocalizedObject show all
Defined in:
lib/twitter_cldr/core_ext/calendars/datetime.rb

Direct Known Subclasses

LocalizedDate, LocalizedTime

Instance Attribute Summary collapse

Attributes inherited from LocalizedObject

#base_obj, #formatter, #locale

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of LocalizedDateTime.



16
17
18
19
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 16

def initialize(obj, locale, options = {})
  super
  @calendar_type = options[:calendar_type] || TwitterCldr::DEFAULT_CALENDAR_TYPE
end

Instance Attribute Details

#calendar_typeObject (readonly)

Returns the value of attribute calendar_type.



14
15
16
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 14

def calendar_type
  @calendar_type
end

Instance Method Details

#ago(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 27

def ago(options = {})
  base_time = options[:base_time] || Time.now
  seconds = self.to_time.base_obj.to_i - base_time.to_i
  raise ArgumentError.new('Start date is after end date. Consider using "until" function.') if seconds > 0
  TwitterCldr::Shared::LocalizedTimespan.new(seconds, @locale).to_s(options[:unit])
end

#to_dateObject



45
46
47
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 45

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

#to_sObject



41
42
43
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 41

def to_s
  to_default_s
end

#to_timeObject



49
50
51
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 49

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

#until(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 34

def until(options = {})
  base_time = options[:base_time] || Time.now
  seconds = self.to_time.base_obj.to_i - base_time.to_i
  raise ArgumentError.new('End date is before start date. Consider using "ago" function.') if seconds < 0
  TwitterCldr::Shared::LocalizedTimespan.new(seconds, @locale).to_s(options[:unit])
end