Module: Chronic
- Defined in:
- lib/chronic.rb,
lib/chronic/tag.rb,
lib/chronic/date.rb,
lib/chronic/span.rb,
lib/chronic/time.rb,
lib/chronic/token.rb,
lib/chronic/parser.rb,
lib/chronic/season.rb,
lib/chronic/handler.rb,
lib/chronic/version.rb,
lib/chronic/handlers.rb,
lib/chronic/mini_date.rb,
lib/chronic/tags/sign.rb,
lib/chronic/tokenizer.rb,
lib/chronic/definition.rb,
lib/chronic/dictionary.rb,
lib/chronic/tags/scalar.rb,
lib/chronic/tags/grabber.rb,
lib/chronic/tags/ordinal.rb,
lib/chronic/tags/pointer.rb,
lib/chronic/tags/repeater.rb,
lib/chronic/tags/separator.rb,
lib/chronic/tags/time_zone.rb,
lib/chronic/repeaters/repeater_day.rb,
lib/chronic/repeaters/repeater_hour.rb,
lib/chronic/repeaters/repeater_time.rb,
lib/chronic/repeaters/repeater_week.rb,
lib/chronic/repeaters/repeater_year.rb,
lib/chronic/repeaters/repeater_month.rb,
lib/chronic/repeaters/repeater_minute.rb,
lib/chronic/repeaters/repeater_season.rb,
lib/chronic/repeaters/repeater_second.rb,
lib/chronic/repeaters/repeater_quarter.rb,
lib/chronic/repeaters/repeater_weekday.rb,
lib/chronic/repeaters/repeater_weekend.rb,
lib/chronic/repeaters/repeater_day_name.rb,
lib/chronic/repeaters/repeater_fortnight.rb,
lib/chronic/repeaters/repeater_month_name.rb,
lib/chronic/repeaters/repeater_day_portion.rb,
lib/chronic/repeaters/repeater_season_name.rb,
lib/chronic/repeaters/repeater_quarter_name.rb
Overview
Defined Under Namespace
Modules: Handlers, Tokenizer Classes: AnchorDefinitions, ArrowDefinitions, Date, DateDefinitions, Definitions, Dictionary, EndianDefinitions, Grabber, Handler, MiniDate, NarrowDefinitions, Ordinal, OrdinalDay, OrdinalMonth, OrdinalYear, Parser, Pointer, Repeater, RepeaterDay, RepeaterDayName, RepeaterDayPortion, RepeaterFortnight, RepeaterHour, RepeaterMinute, RepeaterMonth, RepeaterMonthName, RepeaterQuarter, RepeaterQuarterName, RepeaterSeason, RepeaterSeasonName, RepeaterSecond, RepeaterTime, RepeaterWeek, RepeaterWeekday, RepeaterWeekend, RepeaterYear, Scalar, ScalarDay, ScalarHour, ScalarMinute, ScalarMonth, ScalarSecond, ScalarSubsecond, ScalarWide, ScalarYear, Season, Separator, SeparatorAnd, SeparatorAt, SeparatorColon, SeparatorComma, SeparatorDash, SeparatorDot, SeparatorIn, SeparatorOn, SeparatorQuote, SeparatorSlash, SeparatorSpace, SeparatorT, SeparatorW, Sign, SignMinus, SignPlus, Span, SpanDefinitions, SpanDictionary, Tag, Time, TimeDefinitions, TimeZone, Token
Constant Summary collapse
- VERSION =
'0.10.2'
Class Attribute Summary collapse
-
.debug ⇒ Object
Returns true when debug mode is enabled.
-
.time_class ⇒ Object
Examples:.
Class Method Summary collapse
-
.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, offset = nil) ⇒ Object
Construct a new time object determining possible month overflows and leap years.
-
.parse(text, options = {}) ⇒ Object
Parses a string containing a natural language date or time.
Class Attribute Details
.debug ⇒ Object
Returns true when debug mode is enabled.
66 67 68 |
# File 'lib/chronic.rb', line 66 def debug @debug end |
.time_class ⇒ Object
79 80 81 |
# File 'lib/chronic.rb', line 79 def time_class @time_class end |
Class Method Details
.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, offset = nil) ⇒ Object
Construct a new time object determining possible month overflows and leap years.
year - Integer year. month - Integer month. day - Integer day. hour - Integer hour. minute - Integer minute. second - Integer second.
Returns a new Time object constructed from these params.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/chronic.rb', line 109 def self.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, offset = nil) if second >= 60 minute += second / 60 second = second % 60 end if minute >= 60 hour += minute / 60 minute = minute % 60 end if hour >= 24 day += hour / 24 hour = hour % 24 end # determine if there is a day overflow. this is complicated by our crappy calendar # system (non-constant number of days per month) day <= 56 || raise('day must be no more than 56 (makes month resolution easier)') if day > 28 # no month ever has fewer than 28 days, so only do this if necessary days_this_month = ::Date.leap?(year) ? Date::MONTH_DAYS_LEAP[month] : Date::MONTH_DAYS[month] if day > days_this_month month += day / days_this_month day = day % days_this_month end end if month > 12 if month % 12 == 0 year += (month - 12) / 12 month = 12 else year += month / 12 month = month % 12 end end if Chronic.time_class.name == 'Date' Chronic.time_class.new(year, month, day) elsif not Chronic.time_class.respond_to?(:new) or (RUBY_VERSION.to_f < 1.9 and Chronic.time_class.name == 'Time') Chronic.time_class.local(year, month, day, hour, minute, second) else offset = Time::normalize_offset(offset) if Chronic.time_class.name == 'DateTime' Chronic.time_class.new(year, month, day, hour, minute, second, offset) end end |
.parse(text, options = {}) ⇒ Object
Parses a string containing a natural language date or time.
If the parser can find a date or time, either a Time or Chronic::Span will be returned (depending on the value of ‘:guess`). If no date or time can be found, `nil` will be returned.
text - The String text to parse. opts - An optional Hash of configuration options passed to Parser::new.
94 95 96 |
# File 'lib/chronic.rb', line 94 def self.parse(text, = {}) Parser.new().parse(text) end |