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)


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

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::LocalizedTimespan.new(seconds, options.merge(:locale => @locale))
end

#to_dateObject



51
52
53
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 51

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



47
48
49
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 47

def to_s
  to_default_s
end

#to_timeObject



55
56
57
# File 'lib/twitter_cldr/core_ext/calendars/datetime.rb', line 55

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

#to_timespan(options = {}) ⇒ Object



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

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::LocalizedTimespan.new(seconds, options.merge(:locale => @locale, :direction => :none))
end

#until(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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

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::LocalizedTimespan.new(seconds, options.merge(:locale => @locale))
end