Module Chronos
In: lib/chronos/calendar/gregorian.rb
lib/chronos/calendar.rb
lib/chronos/datetime/gregorian.rb
lib/chronos/datetime.rb
lib/chronos/duration/gregorian.rb
lib/chronos/duration.rb
lib/chronos/exceptions.rb
lib/chronos/interval/gregorian.rb
lib/chronos/interval.rb
lib/chronos/minimalistic.rb
lib/chronos/numeric/gregorian.rb
lib/chronos/version.rb
lib/chronos/zone.rb
lib/chronos.rb

Summary

Synopsis

Description

Duck typing

All chronos classes will accept in places of

Datetime:objects that respond to to_datetime (careful as this collides with date‘s Time#to_datetime, you have to explicitly import them)
Duration:objects that respond to to_duration
Interval:objects that respond to to_interval
Calendar:objects that respond to to_calendar

For classes that don‘t have those methods you can try Klassname::import. For example

  Chronos::Datetime.import(Time.now) # => Chronos::Datetime

Methods

Classes and Modules

Module Chronos::Datetime
Module Chronos::Numeric
Class Chronos::Calendar
Class Chronos::Datetime
Class Chronos::Duration
Class Chronos::Interval
Class Chronos::LocalizationError
Class Chronos::NoDatePart
Class Chronos::NoTimePart
Class Chronos::Zone

Constants

VERSION = Gem::Version.new(version)
VERSION = version
Zone = Struct.new( :timezone_id, :language, :utc, :offset, :dstrule, :numeric, :alpha2, :alpha3, :country, :latitude, :longitude, :latitude_iso, :longitude_iso
PS_IN_NANOSECOND = 1_000   picoseconds in a nanosecond
PS_IN_MICROSECOND = PS_IN_NANOSECOND * 1_000   picoseconds in a microsecond
PS_IN_MILLISECOND = PS_IN_MICROSECOND * 1_000   picoseconds in a microsecond
PS_IN_SECOND = PS_IN_MILLISECOND * 1_000   picoseconds in a second
PS_IN_MINUTE = PS_IN_SECOND * 60   picoseconds in a minute
PS_IN_HOUR = PS_IN_MINUTE * 60   picoseconds in an hour
PS_IN_DAY = PS_IN_HOUR * 24   picoseconds in a day
PS_IN_WEEK = PS_IN_DAY * 7   picoseconds in a week
YAMLExt = '.yaml'.freeze   The extension YAML files use
ZonesFile = File.join(File.dirname(__FILE__), "chronos", "data", "zones.tab").freeze   The full path of the zones.tab file
ZonesData = File.join(File.dirname(__FILE__), "chronos", "data", "zones.marshal").freeze   The full path of the marshalled zones data cache file
DefaultizeStrings = [ :picosecond, :nanosecond, :microsecond, :millisecond, :second, :minute, :hour, :day, :week, :month, :year
UTC = Chronos.timezone('UTC')

Attributes

calendar  [R] 
strings  [R] 

Public Class methods

[Source]

     # File lib/chronos.rb, line 174
174:     def language(lang=nil)
175:       case lang
176:         when NilClass
177:           @language
178:         else
179:           normalize_language(lang)
180:       end
181:     end

Set the default language to use with Chronos classes (parsing/printing)

[Source]

     # File lib/chronos.rb, line 155
155:     def language=(value)
156:       @language = normalize_language(value)
157:     end

Load a yaml strings file

[Source]

     # File lib/chronos.rb, line 128
128:     def load_strings(strfile, language)
129:       data = YAML.load_file(strfile)
130:       DefaultizeStrings.each do |key|
131:         data[key] = Hash.new(data[key].delete(nil)).merge(data[key])
132:       end
133:       @strings[language] ||= {}
134:       @strings[language].update(data)
135:     end

TODO: refactor this ugly piece of code

[Source]

     # File lib/chronos.rb, line 101
101:     def string(lang, key, quantity=nil)
102:       if localized1 = @strings[lang] then
103:         if localized2 = localized1[key] then
104:           quantity ? localized2[quantity] : localized2
105:         elsif lang != 'en_US' && localized1 = @strings['en_US'] then
106:           if localized2 = localized1[key] then
107:             warn "Couldn't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}, falling back to en_US"
108:             quantity ? localized2[quantity] : localized2
109:           else
110:             raise LocalizationError, "Can't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}"
111:           end
112:         else
113:           raise LocalizationError, "Can't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}"
114:         end
115:       elsif lang != 'en_US' && localized1 = @strings['en_US'] then
116:         if localized2 = localized1[key] then
117:           warn "Couldn't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}, falling back to en_US"
118:           quantity ? localized2[quantity] : localized2
119:         else
120:           raise LocalizationError, "Can't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}"
121:         end
122:       else
123:         raise LocalizationError, "Can't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}"
124:       end
125:     end

[Source]

     # File lib/chronos.rb, line 163
163:     def timezone(tz=nil)
164:       case tz
165:         when Chronos::Zone
166:           tz
167:         when NilClass
168:           @timezone
169:         else
170:           normalize_timezone(tz)
171:       end
172:     end

Set the default timezone to use with Chronos classes

[Source]

     # File lib/chronos.rb, line 160
160:     def timezone=(value)
161:       @timezone = normalize_timezone(value)
162:     end

Set the calendar system Chronos should use. You can also just require the appropriate file, e.g.:

  require 'chronos/gregorian'

will call Chronos.use :Gregorian

[Source]

     # File lib/chronos.rb, line 187
187:     def use(calendar_system)
188:       raise "Calendar system is already set" if @calendar
189:       raise TypeError, "Symbol expected, #{calendar_system.class} given" unless calendar_system.kind_of?(Symbol)
190:       @calendar = calendar_system
191:     end

[Validate]