Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/time-helper.rb
Overview
Defined Under Namespace
Classes: GenericInputError, InvalidArgument, InvalidTimeStringFormat
Constant Summary collapse
- DateTimeRegex =
datetime regex
/^\d{2}|\d{4}[^\d]?\d{2}[^\d]?\d{2}|\d{4}(?:[^\d]?\d{2}:?\d{2}:?\d{2})?$/
Class Method Summary collapse
-
.strtotime(string) ⇒ Time
Set in the date of the string passed.
-
.tomorrow ⇒ Time
Time object set 24 hours ago.
-
.utc_parse(value) ⇒ Time
Converts a Date in to utc format.
- .valid_datetime?(string) ⇒ Boolean
-
.yesterday ⇒ Time
Return time object 24 hours from now.
Instance Method Summary collapse
- #=~(other_time, acceptable_diff = 300) ⇒ Object
-
#add(params) ⇒ Time
New time object with date set to the result.
-
#substract(params) ⇒ Time
New time object with date set to the result.
Class Method Details
.strtotime(string) ⇒ Time
Returns set in the date of the string passed.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/time-helper.rb', line 12 def strtotime string unless valid_datetime? string raise InvalidTimeStringFormat end h,m,s = 0,0,0 if date_or_dt( string ) == :datetime separator = dt_get_separator string date, time = 0, 0 if separator date, time = string.split( separator ) else date, time = string[0..7], string[8..13] end h,m,s = get_time_array time string = date end separator = get_separator string order = get_order string if separator dy, mo, yd = string.split( separator ) if order == :dmy return Time.local( yd, mo, dy,h,m,s ) elsif order == :ymd return Time.local( dy, mo, yd,h,m,s) end end if order == :dmy return Time.local( string[4..7].to_i, string[2..3].to_i, string[0..1].to_i,h,m,s ) elsif order == :ymd return Time.local( string[0..3].to_i, string[4..5].to_i, string[6..7].to_i,h,m,s ) end end |
.tomorrow ⇒ Time
Returns Time object set 24 hours ago.
60 61 62 |
# File 'lib/time-helper.rb', line 60 def tomorrow return Time.now.add(:day => 1 ) end |
.utc_parse(value) ⇒ Time
Converts a Date in to utc format
71 72 73 |
# File 'lib/time-helper.rb', line 71 def utc_parse value Time.strtotime(value).utc end |
.valid_datetime?(string) ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/time-helper.rb', line 48 def valid_datetime? string valid_number_only_date_sizes = [8,14] valid_date_sizes = [10, 19] if string.match /^\d+$/ return false unless valid_number_only_date_sizes.include? string.length else return false unless valid_date_sizes.include? string.length end return false unless string.match DateTimeRegex return true end |
.yesterday ⇒ Time
Return time object 24 hours from now
64 65 66 |
# File 'lib/time-helper.rb', line 64 def yesterday return Time.now.substract(:day => 1 ) end |
Instance Method Details
#=~(other_time, acceptable_diff = 300) ⇒ Object
Note:
minute is :min seconds is :sec
218 219 220 221 222 223 |
# File 'lib/time-helper.rb', line 218 def =~(other_time, acceptable_diff = 300 ) raise InvalidArgument.new('Argument must be an instance of Time') unless other_time.class == Time raise InvalidArgument.new('Argument must be fix number') unless acceptable_diff.class == Fixnum diff = self.to_i - other_time.to_i return ( diff.abs <= acceptable_diff ) end |
#add(params) ⇒ Time
Returns new time object with date set to the result.
169 170 171 172 |
# File 'lib/time-helper.rb', line 169 def add params seconds = get_seconds params return self + seconds end |
#substract(params) ⇒ Time
Returns new time object with date set to the result.
181 182 183 184 |
# File 'lib/time-helper.rb', line 181 def substract params seconds = get_seconds params return self - seconds end |