Class: ICU::Calendar

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/icu/calendar.rb,
lib/icu/calendar/library.rb,
lib/icu/calendar/version.rb,
lib/icu/calendar/library/constants.rb,
lib/icu/calendar/library/error_code.rb,
lib/icu/calendar/library/version_info.rb

Defined Under Namespace

Modules: Library

Constant Summary collapse

RuntimeError =
Class.new(::RuntimeError)
VERSION =
'1.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Calendar

Returns a new instance of Calendar.



171
172
173
174
175
176
177
178
179
180
# File 'lib/icu/calendar.rb', line 171

def initialize(options = {})
  calendar = wchar_buffer_from_string_or_nil(options[:timezone]) do |timezone|
    Library.assert_success do |status|
      Library.ucal_open(timezone, -1, options[:locale], :default, status)
    end
  end

  @calendar = automatically_close(calendar)
  self.time = options[:time] if options[:time]
end

Class Method Details

.available_localesObject



10
11
12
# File 'lib/icu/calendar.rb', line 10

def available_locales
  (0...Library.ucal_countAvailable).map(&Library.method(:ucal_getAvailable))
end

.canonical_timezone_identifier(timezone) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/icu/calendar.rb', line 14

def canonical_timezone_identifier(timezone)
  FFI::MemoryPointer.new(:bool) do |is_system_id|
    return Library.wchar_buffer_from_string(timezone) do |w_timezone|
      Library.read_into_wchar_buffer(32) do |buffer, status|
        Library.ucal_getCanonicalTimeZoneID(
          w_timezone, -1,
          buffer, buffer.size / buffer.type_size,
          is_system_id, status
        )
      end
    end
  end
end

.country_timezones(country) ⇒ Object



28
29
30
31
32
# File 'lib/icu/calendar.rb', line 28

def country_timezones(country)
  Library.read_wchar_enumeration(->(status) do
    Library.ucal_openCountryTimeZones(country, status)
  end).to_a
end

.default_timezoneObject



34
35
36
37
38
# File 'lib/icu/calendar.rb', line 34

def default_timezone
  Library.read_into_wchar_buffer(32) do |buffer, status|
    Library.ucal_getDefaultTimeZone(buffer, buffer.size / buffer.type_size, status)
  end
end

.default_timezone=(timezone) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/icu/calendar.rb', line 40

def default_timezone=(timezone)
  Library.wchar_buffer_from_string(timezone) do |w_timezone|
    Library.assert_success do |status|
      Library.ucal_setDefaultTimeZone(w_timezone, status)
    end
  end
end

.dst_savings(timezone) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/icu/calendar.rb', line 48

def dst_savings(timezone)
  Library.wchar_buffer_from_string(timezone) do |w_timezone|
    Library.assert_success do |status|
      Library.ucal_getDSTSavings(w_timezone, status)
    end
  end
end

.timezone_data_versionObject



56
57
58
59
60
# File 'lib/icu/calendar.rb', line 56

def timezone_data_version
  Library.assert_success do |status|
    Library.ucal_getTZDataVersion(status)
  end
end

.timezone_identifiers(type, region = nil, offset = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/icu/calendar.rb', line 62

def timezone_identifiers(type, region = nil, offset = nil)
  offset = FFI::MemoryPointer.new(:int32).write_int32(offset) unless offset.nil?

  Library.read_wchar_enumeration(->(status) do
    Library.ucal_openTimeZoneIDEnumeration(type, region, offset, status)
  end).to_a
ensure
  offset.free unless offset.nil?
end

.timezonesObject



72
73
74
75
76
# File 'lib/icu/calendar.rb', line 72

def timezones
  Library.read_wchar_enumeration(->(status) do
    Library.ucal_openTimeZones(status)
  end).to_a
end

Instance Method Details

#<=>(other) ⇒ Object



93
94
95
# File 'lib/icu/calendar.rb', line 93

def <=>(other)
  time <=> coerce_to_milliseconds(other)
end

#[](field) ⇒ Object



79
80
81
82
83
# File 'lib/icu/calendar.rb', line 79

def [](field)
  field_value_to_symbol(field, Library.assert_success do |status|
    Library.ucal_get(@calendar, field, status)
  end)
end

#[]=(field, value) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/icu/calendar.rb', line 85

def []=(field, value)
  if value.nil?
    Library.ucal_clearField(@calendar, field)
  else
    Library.ucal_set(@calendar, field, value)
  end
end

#actual_maximum(field) ⇒ Object



97
98
99
100
101
# File 'lib/icu/calendar.rb', line 97

def actual_maximum(field)
  field_value_to_symbol(field, Library.assert_success do |status|
    Library.ucal_getLimit(@calendar, field, :actual_maximum, status)
  end)
end

#actual_minimum(field) ⇒ Object



103
104
105
106
107
# File 'lib/icu/calendar.rb', line 103

def actual_minimum(field)
  field_value_to_symbol(field, Library.assert_success do |status|
    Library.ucal_getLimit(@calendar, field, :actual_minimum, status)
  end)
end

#add(field, amount) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/icu/calendar.rb', line 109

def add(field, amount)
  Library.assert_success do |status|
    Library.ucal_add(@calendar, field, amount, status)
  end

  self
end

#clearObject



117
118
119
# File 'lib/icu/calendar.rb', line 117

def clear
  Library.ucal_clear(@calendar)
end

#daylight_time?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'lib/icu/calendar.rb', line 121

def daylight_time?
  Library.assert_success do |status|
    Library.ucal_inDaylightTime(@calendar, status)
  end
end

#difference(from, field) ⇒ Object



127
128
129
130
131
132
# File 'lib/icu/calendar.rb', line 127

def difference(from, field)
  from = coerce_to_milliseconds(from)
  Library.assert_success do |status|
    Library.ucal_getFieldDifference(@calendar, from, field, status)
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/icu/calendar.rb', line 134

def eql?(other)
  equivalent?(other) && time == other.time
end

#equivalent?(other) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/icu/calendar.rb', line 138

def equivalent?(other)
  Calendar === other && Library.ucal_equivalentTo(@calendar, other.calendar)
end

#first_day_of_weekObject



142
143
144
# File 'lib/icu/calendar.rb', line 142

def first_day_of_week
  Library.enum_type(:day_of_week)[Library.ucal_getAttribute(@calendar, :first_day_of_week)]
end

#first_day_of_week=(day_of_week) ⇒ Object



146
147
148
149
# File 'lib/icu/calendar.rb', line 146

def first_day_of_week=(day_of_week)
  day_of_week = Library.enum_type(:day_of_week)[day_of_week] if Symbol === day_of_week
  Library.ucal_setAttribute(@calendar, :first_day_of_week, day_of_week)
end

#greatest_minimum(field) ⇒ Object



151
152
153
154
155
# File 'lib/icu/calendar.rb', line 151

def greatest_minimum(field)
  field_value_to_symbol(field, Library.assert_success do |status|
    Library.ucal_getLimit(@calendar, field, :greatest_minimum, status)
  end)
end

#gregorian_changeObject



157
158
159
160
161
# File 'lib/icu/calendar.rb', line 157

def gregorian_change
  Library.assert_success do |status|
    Library.ucal_getGregorianChange(@calendar, status)
  end
end

#gregorian_change=(time) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/icu/calendar.rb', line 163

def gregorian_change=(time)
  time = coerce_to_milliseconds(time)

  Library.assert_success do |status|
    Library.ucal_setGregorianChange(@calendar, time, status)
  end
end

#is_set?(field) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/icu/calendar.rb', line 182

def is_set?(field)
  Library.ucal_isSet(@calendar, field)
end

#least_maximum(field) ⇒ Object



186
187
188
189
190
# File 'lib/icu/calendar.rb', line 186

def least_maximum(field)
  field_value_to_symbol(field, Library.assert_success do |status|
    Library.ucal_getLimit(@calendar, field, :least_maximum, status)
  end)
end

#lenient=(value) ⇒ Object



196
197
198
# File 'lib/icu/calendar.rb', line 196

def lenient=(value)
  Library.ucal_setAttribute(@calendar, :lenient, value ? 1 : 0)
end

#lenient?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/icu/calendar.rb', line 192

def lenient?
  Library.ucal_getAttribute(@calendar, :lenient).nonzero?
end

#locale(type = :valid) ⇒ Object



200
201
202
203
204
# File 'lib/icu/calendar.rb', line 200

def locale(type = :valid)
  Library.assert_success do |status|
    Library.ucal_getLocaleByType(@calendar, type, status)
  end
end

#maximum(field) ⇒ Object



206
207
208
209
210
# File 'lib/icu/calendar.rb', line 206

def maximum(field)
  field_value_to_symbol(field, Library.assert_success do |status|
    Library.ucal_getLimit(@calendar, field, :maximum, status)
  end)
end

#minimal_days_in_first_weekObject



212
213
214
# File 'lib/icu/calendar.rb', line 212

def minimal_days_in_first_week
  Library.ucal_getAttribute(@calendar, :minimal_days_in_first_week)
end

#minimal_days_in_first_week=(value) ⇒ Object



216
217
218
# File 'lib/icu/calendar.rb', line 216

def minimal_days_in_first_week=(value)
  Library.ucal_setAttribute(@calendar, :minimal_days_in_first_week, value)
end

#minimum(field) ⇒ Object



220
221
222
223
224
# File 'lib/icu/calendar.rb', line 220

def minimum(field)
  field_value_to_symbol(field, Library.assert_success do |status|
    Library.ucal_getLimit(@calendar, field, :minimum, status)
  end)
end

#next_timezone_transition(inclusive = false) ⇒ Object



226
227
228
# File 'lib/icu/calendar.rb', line 226

def next_timezone_transition(inclusive = false)
  timezone_transition(inclusive ? :next_inclusive : :next)
end

#previous_timezone_transition(inclusive = false) ⇒ Object



230
231
232
# File 'lib/icu/calendar.rb', line 230

def previous_timezone_transition(inclusive = false)
  timezone_transition(inclusive ? :previous_inclusive : :previous)
end

#repeated_wall_timeObject



234
235
236
# File 'lib/icu/calendar.rb', line 234

def repeated_wall_time
  Library.enum_type(:walltime_option)[Library.ucal_getAttribute(@calendar, :repeated_wall_time)]
end

#repeated_wall_time=(option) ⇒ Object



238
239
240
241
# File 'lib/icu/calendar.rb', line 238

def repeated_wall_time=(option)
  option = Library.enum_type(:walltime_option)[option] if Symbol === option
  Library.ucal_setAttribute(@calendar, :repeated_wall_time, option)
end

#roll(field, amount) ⇒ Object



243
244
245
246
247
248
249
# File 'lib/icu/calendar.rb', line 243

def roll(field, amount)
  Library.assert_success do |status|
    Library.ucal_roll(@calendar, field, amount, status)
  end

  self
end

#set_date(year, month, day) ⇒ Object



251
252
253
254
255
# File 'lib/icu/calendar.rb', line 251

def set_date(year, month, day)
  Library.assert_success do |status|
    Library.ucal_setDate(@calendar, year, month, day, status)
  end
end

#set_date_and_time(year, month, day, hour, minute, second) ⇒ Object



257
258
259
260
261
# File 'lib/icu/calendar.rb', line 257

def set_date_and_time(year, month, day, hour, minute, second)
  Library.assert_success do |status|
    Library.ucal_setDateTime(@calendar, year, month, day, hour, minute, second, status)
  end
end

#skipped_wall_timeObject



263
264
265
# File 'lib/icu/calendar.rb', line 263

def skipped_wall_time
  Library.enum_type(:walltime_option)[Library.ucal_getAttribute(@calendar, :skipped_wall_time)]
end

#skipped_wall_time=(option) ⇒ Object



267
268
269
270
# File 'lib/icu/calendar.rb', line 267

def skipped_wall_time=(option)
  option = Library.enum_type(:walltime_option)[option] if Symbol === option
  Library.ucal_setAttribute(@calendar, :skipped_wall_time, option)
end

#timeObject



272
273
274
275
276
# File 'lib/icu/calendar.rb', line 272

def time
  Library.assert_success do |status|
    Library.ucal_getMillis(@calendar, status)
  end
end

#time=(time) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/icu/calendar.rb', line 278

def time=(time)
  time = coerce_to_milliseconds(time)

  Library.assert_success do |status|
    Library.ucal_setMillis(@calendar, time, status)
  end
end

#timezoneObject



286
287
288
289
290
# File 'lib/icu/calendar.rb', line 286

def timezone
  Library.read_into_wchar_buffer(32) do |buffer, status|
    Library.ucal_getTimeZoneID(@calendar, buffer, buffer.size / buffer.type_size, status)
  end
end

#timezone=(timezone) ⇒ Object



292
293
294
295
296
297
298
# File 'lib/icu/calendar.rb', line 292

def timezone=(timezone)
  wchar_buffer_from_string_or_nil(timezone) do |w_timezone|
    Library.assert_success do |status|
      Library.ucal_setTimeZone(@calendar, w_timezone, -1, status)
    end
  end
end

#timezone_display_name(type = :standard, locale = nil) ⇒ Object



300
301
302
303
304
# File 'lib/icu/calendar.rb', line 300

def timezone_display_name(type = :standard, locale = nil)
  Library.read_into_wchar_buffer(32) do |buffer, status|
    Library.ucal_getTimeZoneDisplayName(@calendar, type, locale, buffer, buffer.size / buffer.type_size, status)
  end
end

#to_timeObject



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/icu/calendar.rb', line 306

def to_time
  Time.new(
    self[:year],
    self[Library.enum_type(:date_field)[:month]] + 1,
    self[:day_of_month],
    self[:hour_of_day],
    self[:minute],
    self[:second] + Rational(self[:millisecond], 1000),
    (self[:zone_offset] + self[:dst_offset]) / 1000
  )
end

#typeObject



318
319
320
321
322
# File 'lib/icu/calendar.rb', line 318

def type
  Library.assert_success do |status|
    Library.ucal_getType(@calendar, status)
  end.to_sym
end

#weekday_type(day_of_week) ⇒ Object



324
325
326
327
328
# File 'lib/icu/calendar.rb', line 324

def weekday_type(day_of_week)
  Library.assert_success do |status|
    Library.ucal_getDayOfWeekType(@calendar, day_of_week, status)
  end
end

#weekend?Boolean

Returns:

  • (Boolean)


330
331
332
333
334
# File 'lib/icu/calendar.rb', line 330

def weekend?
  Library.assert_success do |status|
    Library.ucal_isWeekend(@calendar, time, status)
  end
end

#weekend_transition(day_of_week) ⇒ Object



336
337
338
339
340
# File 'lib/icu/calendar.rb', line 336

def weekend_transition(day_of_week)
  Library.assert_success do |status|
    Library.ucal_getWeekendTransition(@calendar, day_of_week, status)
  end
end