diff options
| author | Steve Kemp <steve@steve.org.uk> | 2013-06-24 07:06:09 +0100 | 
|---|---|---|
| committer | Steve Kemp <steve@steve.org.uk> | 2013-06-24 07:06:09 +0100 | 
| commit | 48b454593a342e16a1e64849ea69b4f57f8afb5d (patch) | |
| tree | bd641fb75e68abd6603accb47de68ce49550d1fd /lib/custodian | |
| parent | ddb1be3d65022bb0bf21327bddca62f6a38c3e07 (diff) | |
  Added Custodian::Util::TimeSpan
This class is used to determine whether an "hour" is within
a given hour-span.  e.g. To cope with:
    foo must run ping except between 04-06 otherwise 'alert'.
This updates issue #4551.
Diffstat (limited to 'lib/custodian')
| -rwxr-xr-x | lib/custodian/util/timespan.rb | 93 | 
1 files changed, 93 insertions, 0 deletions
| diff --git a/lib/custodian/util/timespan.rb b/lib/custodian/util/timespan.rb new file mode 100755 index 0000000..7d45e44 --- /dev/null +++ b/lib/custodian/util/timespan.rb @@ -0,0 +1,93 @@ +#!/usr/bin/ruby1.8 + +module Custodian + +  module Util + +    # +    # A class for working with time-spans. +    # +    class TimeSpan + +      # +      #  Given a starting hour such as 10pm and and ending hour such as 4am +      # see if the current hour is inside that range. +      # +      def TimeSpan.inside?( p_start, p_end, cur_hour = nil) + +        # +        #  If we don't have an hour specified then use the current one. +        # +        if ( cur_hour.nil? ) +          cur_hour = Time.now.hour +        end + +        # +        #  Convert "XXPM" to appropriate 24-hour based integers +        # +        if ( ( p_start.kind_of? String ) && ( p_start =~ /([0-9]+)pm$/i ) ) +          p_start = $1.dup.to_i + 12; +        end +        if ( ( p_end.kind_of? String ) && ( p_end =~ /([0-9]+)pm$/i ) ) +          p_end = $1.dup.to_i + 12; +        end +        if ( ( cur_hour.kind_of? String ) && ( cur_hour =~ /([0-9]+)pm$/i ) ) +          cur_hour = $1.dup.to_i + 12; +        end + +        # +        #  If we have AM suffixes then strip them +        # +        if ( p_start.kind_of? String ) +          p_start = p_start.sub( /am$/, '' ) +          p_start = p_start.to_i +        end +        if ( p_end.kind_of? String ) +          p_end = p_end.sub( /am$/, '' ) +          p_end = p_end.to_i +        end +        if ( cur_hour.kind_of? String ) +          cur_hour = cur_hour.sub( /am$/, '' ) +          cur_hour = cur_hour.to_i +        end + + +        # +        #  Ensure we're now left with integer values. +        # +        raise ArgumentError, "Integer required for start time"   unless( p_start.kind_of? Integer ) +        raise ArgumentError,  "Integer required for end time"     unless( p_end.kind_of? Integer ) +        raise ArgumentError,  "Integer required for current hour" unless( cur_hour.kind_of? Integer ) + +        # +        #  Ensure the values have appropriate bounds. +        # +        raise ArgumentError,  "Invalid start time"   unless( ( p_start >= 0 ) && ( p_start <= 23 ) ) +        raise ArgumentError,  "Invalid end time"     unless( ( p_end >= 0 ) && ( p_end <= 23 ) ) +        raise ArgumentError,  "Invalid current time" unless( ( cur_hour >= 0 ) && ( cur_hour <= 23 ) ) + +        # +        #  Valid hours, within the span +        # +        valid = {} + +        # +        #  Iterate over the hours.  Store in a hash. +        # +        hour = p_start +        while( hour != p_end ) +          valid[hour] = 1 +          hour += 1 +          hour  = 0 if ( hour >= 23 ) +        end +        valid[p_end]=1 + +        # now do the test. +        ( valid[cur_hour] == 1 ) +      end + +    end +  end +end + +puts Custodian::Util::TimeSpan.inside?( "11pm", "2am", 14 ) | 
