Skip to content

Commit

Permalink
Merge branch 'allocations'
Browse files Browse the repository at this point in the history
  • Loading branch information
adzap committed Dec 2, 2024
2 parents a85c4d7 + 93a7814 commit 5bb587b
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ["2.5", "2.6", "2.7", "3.0", "3.1", "3.2", jruby-9.2, jruby-9.3, jruby-9.4]
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3", ruby-head, jruby-9.2, jruby-9.3, jruby-head]
channel: ['stable']

include:
Expand All @@ -21,6 +21,7 @@ jobs:
channel: 'experimental'

continue-on-error: ${{ matrix.channel != 'stable' }}
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3", ruby-head, jruby-9.2, jruby-9.3, jruby-head]

steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--color
--require debug
--require spec_helper
--require timeliness_helper
13 changes: 11 additions & 2 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
= 0.5.0 - 2024-12-02
* Reduce allocations through some internal parsing changes
* Changed parse method arg handling to simple using keyword args

= 0.4.5 - 2023-01-19
* Support case insensitive months
* Migrated to Github Actions (@petergoldstein)
* Various doc, spec, and gemspec fixes and updates (@tagliala)

= 0.4.4 - 2019-08-06
* Raise compilation error if token with capturing arg is used more than once in a format
* Some small internal refactorings in format compilation
Expand All @@ -10,7 +19,7 @@
= 0.4.2 - 2019-06-15
* Fixed thread safe issue that forced you to use one of the date format methods e.g. `use_euro_formats`
to initialize the format sets in each new thread. Now a new thread will default to the global default (main thread).
* Add `Timeliness.ambiguous_date_format` config setting (:us or :euro) to control global default for date format sets.
* Add `Timeliness.ambiguous_date_format` config setting (:us or :euro) to control global default for date format sets.

= 0.4.1 - 2019-06-11
* Add format for ISO 8601 with usec and 'Z' UTC zone offset (jartek)
Expand All @@ -19,7 +28,7 @@

= 0.4.0 - 2019-02-09
* Add threadsafety for use_euro_formats & use_us_formats to allow runtime
switching (andruby, timdiggins)
switching (andruby, timdiggins)

= 0.3.10 - 2019-02-06
* Fixed file permissions in gem build
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ gemspec

gem 'activesupport', '~> 6.0.0'
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'memory_profiler'
4 changes: 3 additions & 1 deletion lib/timeliness/definitions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Timeliness
module Definitions

Expand Down Expand Up @@ -83,7 +85,7 @@ module Definitions
'yyyy-mm-ddThh:nn:sszt', # ISO 8601 with 'Zulu time' (i.e. Z) UTC zone designator
'yyyy-mm-ddThh:nn:ss.u', # ISO 8601 with usec
'yyyy-mm-ddThh:nn:ss.uzo', # ISO 8601 with usec and offset
'yyyy-mm-ddThh:nn:ss.uzt', # ISO 8601 with usec and 'Zulu time' (i.e. Z) UTC zone designator
'yyyy-mm-ddThh:nn:ss.uzt', # ISO 8601 with usec and 'Zulu time' (i.e. Z) UTC zone designator
'yyyy-mm-dd hh:nn:ss zo', # Ruby time string in later versions
'yyyy-mm-dd hh:nn:ss tz', # Ruby time string for UTC in later versions
]
Expand Down
3 changes: 2 additions & 1 deletion lib/timeliness/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def compile!

define_process_method(token_order.compact)
@regexp_string = format
@regexp = Regexp.new("^(#{format})$")
@regexp = Regexp.new("^(?>#{format})$")
self
rescue => ex
raise CompilationFailed, "The format '#{format_string}' failed to compile using regexp string #{format}. Error message: #{ex.inspect}"
Expand All @@ -63,6 +63,7 @@ def define_process_method(components)
position, code = Definitions.format_components[component]
values[position] = code || "#{component}.to_i" if position
end
components << '*_' # absorb any excess arguments not used by format
instance_eval <<-DEF
def process(#{components.join(',')})
[#{values.map { |i| i || 'nil' }.join(',')}]
Expand Down
12 changes: 6 additions & 6 deletions lib/timeliness/format_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def compile!
format = Format.new(format_string).compile!
@formats_hash[format_string] = format
@match_indexes[index] = format
regexp_string = "#{regexp_string}(#{format.regexp_string})|"
index + format.token_count + 1 # add one for wrapper capture
regexp_string.concat("(?>#{format.regexp_string})|")
index + format.token_count
}
@regexp = %r[\A(?:#{regexp_string.chop})\z]
self
Expand All @@ -31,10 +31,10 @@ def match(string, format_string=nil)
format = single_format(format_string) if format_string
match_regexp = format && format.regexp || @regexp

if match_data = match_regexp.match(string)
index = match_data.captures.index(string)
start = index + 1
values = match_data.captures[start..(start+7)].compact
match_regexp.match(string) do |match_data|
captures = match_data.captures # For a multi-format regexp there are lots of nils
index = captures.find_index { |e| !e.nil? } # Find the start of captures for matched format
values = captures.values_at(index..(index+7))
format ||= @match_indexes[index]
format.process(*values)
end
Expand Down
21 changes: 14 additions & 7 deletions lib/timeliness/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ class MissingTimezoneSupport < StandardError; end

class << self

def parse(value, *args)
def parse(value, type=nil, **options)
return value if acts_like_temporal?(value)
return nil unless parseable?(value)

type, options = type_and_options_from_args(args)
if type && !type.is_a?(Symbol)
options[:now] = type if type
type = nil
end

time_array = _parse(value, type, options)
return nil if time_array.nil?
Expand Down Expand Up @@ -40,7 +43,7 @@ def _parse(string, type=nil, options={})
Definitions.send("#{type}_format_set").match(string, options[:format])
else
values = nil
Definitions.format_sets(type, string).find { |set| values = set.match(string, options[:format]) }
Definitions.format_sets(type, string).any? { |set| values = set.match(string, options[:format]) }
values
end
rescue
Expand Down Expand Up @@ -95,8 +98,10 @@ def current_date(options)

def current_time_in_zone(zone)
case zone
when :utc, :local
Time.now.send("get#{zone}")
when :utc
Time.now.getutc
when :local
Time.now.getlocal
when :current
Time.current
else
Expand All @@ -107,8 +112,10 @@ def current_time_in_zone(zone)
def shift_time_to_zone(time, zone=nil)
zone ||= Timeliness.configuration.default_timezone
case zone
when :utc, :local
time.send("get#{zone}")
when :utc
time.getutc
when :local
time.getlocal
when :current
time.in_time_zone
else
Expand Down
2 changes: 1 addition & 1 deletion lib/timeliness/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Timeliness
VERSION = '0.4.5'
VERSION = '0.5.0'.freeze
end
14 changes: 7 additions & 7 deletions spec/timeliness_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ def definitions
Timeliness::Definitions
end

def parse(*args)
Timeliness::Parser.parse(*args)
def parse(value, type=nil, **args)
Timeliness::Parser.parse(value, type, **args)
end

def current_date(options={})
Timeliness::Parser.send(:current_date, options)
end

def should_parse(*args)
expect(Timeliness::Parser.parse(*args)).not_to be_nil
def should_parse(value, type=nil, **args)
expect(Timeliness::Parser.parse(value, type, **args)).not_to be_nil
end

def should_not_parse(*args)
expect(Timeliness::Parser.parse(*args)).to be_nil
def should_not_parse(value, type=nil, **args)
expect(Timeliness::Parser.parse(value, type, **args)).to be_nil
end
end

Expand All @@ -38,4 +38,4 @@ def should_not_parse(*args)
c.after do
Timeliness.configuration = Timeliness::Configuration.new
end
end
end

0 comments on commit 5bb587b

Please sign in to comment.