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.types
end

Instance Method Details

#additional_formatsObject



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

def additional_formats
  data_reader_for(nil).additional_format_selector.patterns
end

#ago(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# 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)
end

#to_dateObject



79
80
81
# File 'lib/twitter_cldr/localized/localized_datetime.rb', line 79

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

#to_sObject



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

def to_s
  to_default_s
end

#to_time(base = Time.now) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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 = {}) ⇒ Object



51
52
53
54
55
# 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 = {}) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
# 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) ⇒ Object



99
100
101
# 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