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

Instance Method Summary collapse

Methods inherited from LocalizedObject

localize

Constructor Details

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

Returns a new instance of LocalizedDateTime.



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

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

Instance Method Details

#ago(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 32

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_dateObject



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

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

#to_s(options = {}) ⇒ Object



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

def to_s(options = {})
  if options[:format]
    @formatter.format(base_in_timezone, options.merge(:type => :additional))
  else
    to_default_s
  end
end

#to_time(base = Time.now) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 58

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



26
27
28
29
30
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 26

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)


39
40
41
42
43
44
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 39

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



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

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