Module: Chronos
- Defined in:
- lib/chronos.rb,
lib/chronos/zone.rb,
lib/chronos/version.rb,
lib/chronos/calendar.rb,
lib/chronos/datetime.rb,
lib/chronos/duration.rb,
lib/chronos/interval.rb,
lib/chronos/exceptions.rb,
lib/chronos/minimalistic.rb,
lib/chronos/numeric/gregorian.rb,
lib/chronos/calendar/gregorian.rb,
lib/chronos/duration/gregorian.rb,
lib/chronos/interval/gregorian.rb
Overview
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
Defined Under Namespace
Modules: Datetime, Numeric Classes: Calendar, Duration, Interval, LocalizationError, NoDatePart, NoTimePart, Zone
Constant Summary collapse
- PS_IN_NANOSECOND =
picoseconds in a nanosecond
1_000- PS_IN_MICROSECOND =
picoseconds in a microsecond
PS_IN_NANOSECOND * 1_000
- PS_IN_MILLISECOND =
picoseconds in a microsecond
PS_IN_MICROSECOND * 1_000
- PS_IN_SECOND =
picoseconds in a second
PS_IN_MILLISECOND * 1_000
- PS_IN_MINUTE =
picoseconds in a minute
PS_IN_SECOND * 60
- PS_IN_HOUR =
picoseconds in an hour
PS_IN_MINUTE * 60
- PS_IN_DAY =
picoseconds in a day
PS_IN_HOUR * 24
- PS_IN_WEEK =
picoseconds in a week
PS_IN_DAY * 7
- YAMLExt =
The extension YAML files use
'.yaml'.freeze
- ZonesFile =
The full path of the zones.tab file
File.join(File.dirname(__FILE__), "chronos", "data", "zones.tab").freeze
- ZonesData =
The full path of the marshalled zones data cache file
File.join(File.dirname(__FILE__), "chronos", "data", "zones.marshal").freeze
- DefaultizeStrings =
[ :picosecond, :nanosecond, :microsecond, :millisecond, :second, :minute, :hour, :day, :week, :month, :year ].freeze
- UTC =
Chronos.timezone('UTC')
- VERSION =
version
Class Attribute Summary collapse
-
.calendar ⇒ Object
readonly
Returns the value of attribute calendar.
-
.strings ⇒ Object
readonly
Returns the value of attribute strings.
Class Method Summary collapse
- .language(lang = nil) ⇒ Object
-
.language=(value) ⇒ Object
Set the default language to use with Chronos classes (parsing/printing).
-
.load_strings(strfile, language) ⇒ Object
Load a yaml strings file.
-
.normalize_language(val) ⇒ Object
Normalize the language to something Chronos can work with (or raise).
-
.normalize_timezone(val) ⇒ Object
Normalize the timezone to something Chronos can work with (or raise).
-
.string(lang, key, quantity = nil) ⇒ Object
TODO: refactor this ugly piece of code.
- .timezone(tz = nil) ⇒ Object
-
.timezone=(value) ⇒ Object
Set the default timezone to use with Chronos classes.
-
.use(calendar_system) ⇒ Object
Set the calendar system Chronos should use.
Class Attribute Details
.calendar ⇒ Object (readonly)
Returns the value of attribute calendar.
97 98 99 |
# File 'lib/chronos.rb', line 97 def calendar @calendar end |
.strings ⇒ Object (readonly)
Returns the value of attribute strings.
98 99 100 |
# File 'lib/chronos.rb', line 98 def strings @strings end |
Class Method Details
.language(lang = nil) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/chronos.rb', line 174 def language(lang=nil) case lang when NilClass @language else normalize_language(lang) end end |
.language=(value) ⇒ Object
Set the default language to use with Chronos classes (parsing/printing)
155 156 157 |
# File 'lib/chronos.rb', line 155 def language=(value) @language = normalize_language(value) end |
.load_strings(strfile, language) ⇒ Object
Load a yaml strings file
128 129 130 131 132 133 134 135 |
# File 'lib/chronos.rb', line 128 def load_strings(strfile, language) data = YAML.load_file(strfile) DefaultizeStrings.each do |key| data[key] = Hash.new(data[key].delete(nil)).merge(data[key]) end @strings[language] ||= {} @strings[language].update(data) end |
.normalize_language(val) ⇒ Object
Normalize the language to something Chronos can work with (or raise)
138 139 140 141 142 143 144 145 146 |
# File 'lib/chronos.rb', line 138 def normalize_language(val) # :nodoc: raise ArgumentError, "Invalid language #{val.inspect}" unless lang = val[/^[a-z]{2}_[A-Z]{2}/] unless @strings.has_key?(language) then warn "Language #{lang} not available, falling back to en_US" 'en_US' else lang end end |
.normalize_timezone(val) ⇒ Object
Normalize the timezone to something Chronos can work with (or raise)
149 150 151 152 |
# File 'lib/chronos.rb', line 149 def normalize_timezone(val) # :nodoc: raise ArgumentError, "Could not normalize timezone #{val.inspect}" unless zone = Zone[val] zone end |
.string(lang, key, quantity = nil) ⇒ Object
TODO: refactor this ugly piece of code
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/chronos.rb', line 101 def string(lang, key, quantity=nil) if localized1 = @strings[lang] then if localized2 = localized1[key] then quantity ? localized2[quantity] : localized2 elsif lang != 'en_US' && localized1 = @strings['en_US'] then if localized2 = localized1[key] then warn "Couldn't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}, falling back to en_US" quantity ? localized2[quantity] : localized2 else raise LocalizationError, "Can't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}" end else raise LocalizationError, "Can't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}" end elsif lang != 'en_US' && localized1 = @strings['en_US'] then if localized2 = localized1[key] then warn "Couldn't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}, falling back to en_US" quantity ? localized2[quantity] : localized2 else raise LocalizationError, "Can't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}" end else raise LocalizationError, "Can't localize #{key.inspect} for #{lang} with quantity #{quantity.inspect}" end end |
.timezone(tz = nil) ⇒ Object
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/chronos.rb', line 163 def timezone(tz=nil) case tz when Chronos::Zone tz when NilClass @timezone else normalize_timezone(tz) end end |
.timezone=(value) ⇒ Object
Set the default timezone to use with Chronos classes
160 161 162 |
# File 'lib/chronos.rb', line 160 def timezone=(value) @timezone = normalize_timezone(value) end |
.use(calendar_system) ⇒ Object
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
187 188 189 190 191 |
# File 'lib/chronos.rb', line 187 def use(calendar_system) raise "Calendar system is already set" if @calendar raise TypeError, "Symbol expected, #{calendar_system.class} given" unless calendar_system.kind_of?(Symbol) @calendar = calendar_system end |