Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/time-helper.rb
Overview
Defined Under Namespace
Classes: GenericInputError, InvalidArgument, InvalidTimeStringFormat
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.
10 11 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 |
# File 'lib/time-helper.rb', line 10 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.
51 52 53 |
# File 'lib/time-helper.rb', line 51 def tomorrow return Time.now.add(:day => 1 ) end |
.utc_parse(value) ⇒ Time
Converts a Date in to utc format
62 63 64 |
# File 'lib/time-helper.rb', line 62 def utc_parse value Time.strtotime(value).utc end |
.valid_datetime?(string) ⇒ Boolean
46 47 48 49 |
# File 'lib/time-helper.rb', line 46 def valid_datetime? string return false unless string.match /^\d{2,4}.?\d{2}.?\d{2,4}(?:.?\d{2}:?\d{2}:?\d{2})?$/ return true end |
.yesterday ⇒ Time
Return time object 24 hours from now
55 56 57 |
# File 'lib/time-helper.rb', line 55 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
209 210 211 212 213 214 |
# File 'lib/time-helper.rb', line 209 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.
160 161 162 163 |
# File 'lib/time-helper.rb', line 160 def add params seconds = get_seconds params return self + seconds end |
#substract(params) ⇒ Time
Returns new time object with date set to the result.
172 173 174 175 |
# File 'lib/time-helper.rb', line 172 def substract params seconds = get_seconds params return self - seconds end |