Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite Handlers, Repeaters and improve Tagging #347

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4b2a95d
Correct tests
davispuh Sep 27, 2014
1a6c745
Add and improve Date and Time methods
davispuh Sep 27, 2014
a845dd2
Create TimeZone class and remove MiniDate
davispuh Sep 27, 2014
7c360a4
Update and add more Tags
davispuh Sep 28, 2014
ffcca87
Add TimeZone support and other Date/Time related methods
davispuh Sep 27, 2014
d720b64
Rewrite handler parsing and tagging
davispuh Sep 27, 2014
2ec151c
Rewrite Repeaters as (Date/Anchor/Arrow, etc.) Object handlers
davispuh Sep 28, 2014
a260544
Remove pre_normalize regexp's
davispuh Sep 28, 2014
b9f0fa5
Add `pre_proccess` step to reduce whitespace to single character
davispuh Sep 28, 2014
0330f1a
Set Span to be exclusive so that it doesn't include Span's end
davispuh Sep 28, 2014
e1af122
Fix test times as Span's range is determined by precision
davispuh Sep 28, 2014
6b5849e
Don't match partial date looking string
davispuh Sep 28, 2014
2a80f00
Implement Quarter parsing
davispuh Sep 28, 2014
92037d3
Short-hand year support eg. '97
davispuh Sep 28, 2014
15f2156
Support for scalar unit pointer
davispuh Sep 28, 2014
5fd4b42
Enable some disabled tests
davispuh Sep 28, 2014
27a873f
Fix endian order for EndianDefinitions
davispuh Jan 25, 2016
1fd3695
Fix test_chronic tests
davispuh Jan 25, 2016
27e6116
Fix test_handler tests
davispuh Jan 25, 2016
ec22ba7
When :context == :none assume time isn't ambiguous
davispuh Aug 3, 2017
345e9f4
Fix test_daylight_savings tests
davispuh Aug 3, 2017
8e243c6
Support (mm/yy), :endian_precedence == :month_year
davispuh Aug 3, 2017
ec65880
Add support for 'Wed, Sep 27, 2017' date format
davispuh Sep 27, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions chronic.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- test`.split($/)

s.add_runtime_dependency 'numerizer', '~> 0.2'
s.add_runtime_dependency 'tzinfo'
s.add_runtime_dependency 'TimezoneParser'

s.add_development_dependency 'rake', '~> 10'
s.add_development_dependency 'simplecov', '~> 0'
Expand Down
83 changes: 25 additions & 58 deletions lib/chronic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,38 @@

require 'chronic/version'

require 'tzinfo'
require 'timezone_parser'

require 'chronic/parser'
require 'chronic/date'
require 'chronic/time'
require 'chronic/time_zone'
require 'chronic/arrow'

require 'chronic/handler'
require 'chronic/handlers'
require 'chronic/mini_date'
require 'chronic/span'
require 'chronic/token'
require 'chronic/token_group'
require 'chronic/tokenizer'
require 'chronic/season'

require 'chronic/tag'
require 'chronic/tags/day_name'
require 'chronic/tags/day_portion'
require 'chronic/tags/day_special'
require 'chronic/tags/grabber'
require 'chronic/tags/keyword'
require 'chronic/tags/month_name'
require 'chronic/tags/ordinal'
require 'chronic/tags/pointer'
require 'chronic/tags/rational'
require 'chronic/tags/scalar'
require 'chronic/tags/season_name'
require 'chronic/tags/separator'
require 'chronic/tags/sign'
require 'chronic/tags/time_special'
require 'chronic/tags/time_zone'

require 'chronic/tags/repeater'
require 'chronic/repeaters/repeater_year'
require 'chronic/repeaters/repeater_quarter'
require 'chronic/repeaters/repeater_quarter_name'
require 'chronic/repeaters/repeater_season'
require 'chronic/repeaters/repeater_season_name'
require 'chronic/repeaters/repeater_month'
require 'chronic/repeaters/repeater_month_name'
require 'chronic/repeaters/repeater_fortnight'
require 'chronic/repeaters/repeater_week'
require 'chronic/repeaters/repeater_weekend'
require 'chronic/repeaters/repeater_weekday'
require 'chronic/repeaters/repeater_day'
require 'chronic/repeaters/repeater_day_name'
require 'chronic/repeaters/repeater_day_portion'
require 'chronic/repeaters/repeater_hour'
require 'chronic/repeaters/repeater_minute'
require 'chronic/repeaters/repeater_second'
require 'chronic/repeaters/repeater_time'
require 'chronic/tags/unit'

# Parse natural language dates and times into Time or Chronic::Span objects.
#
Expand Down Expand Up @@ -106,48 +98,23 @@ def self.parse(text, options = {})
# second - Integer second.
#
# Returns a new Time object constructed from these params.
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
def self.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, timezone = nil)
day, hour, minute, second = Time::normalize(day, hour, minute, second)

# 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
year, month, day = Date::add_day(year, month, day, 0) if day > 28
year, month = Date::add_month(year, month, 0) if month > 12

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'
if timezone and timezone.respond_to?(:to_offset)
offset = timezone.to_offset(year, month, day, hour, minute, second)
else
offset = timezone
end
offset = TimeZone::normalize_offset(offset) if Chronic.time_class.name == 'DateTime'
Chronic.time_class.new(year, month, day, hour, minute, second, offset)
end
end
Expand Down
Loading