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, smallest_unit = :hour) ⇒ 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.
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 44 45 46 47 48 |
# File 'lib/time-helper.rb', line 11 def strtotime string unless valid_datetime? string begin return Time.parse(string) rescue raise InvalidTimeStringFormat end 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.
56 57 58 |
# File 'lib/time-helper.rb', line 56 def tomorrow return Time.now.add(:day => 1 ) end |
.utc_parse(value) ⇒ Time
Converts a Date in to utc format
67 68 69 |
# File 'lib/time-helper.rb', line 67 def utc_parse value Time.strtotime(value).utc end |
.valid_datetime?(string) ⇒ Boolean
51 52 53 54 |
# File 'lib/time-helper.rb', line 51 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 |
Instance Method Details
#=~(other_time, smallest_unit = :hour) ⇒ Object
Note:
minute is :min seconds is :sec
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/time-helper.rb', line 214 def =~(other_time, smallest_unit = :hour) raise InvalidArgument.new('Argument must be an instance of Time') unless other_time.class == Time equal = true [:year, :month, :day, :hour, :min, :sec].each do |method| self_unit, other_unit = self.method(method).call(), other_time.method(method).call() equal = false unless self_unit == other_unit break if method == smallest_unit end return equal end |
#add(params) ⇒ Time
Returns new time object with date set to the result.
165 166 167 168 |
# File 'lib/time-helper.rb', line 165 def add params seconds = get_seconds params return self + seconds end |
#substract(params) ⇒ Time
Returns new time object with date set to the result.
177 178 179 180 |
# File 'lib/time-helper.rb', line 177 def substract params seconds = get_seconds params return self - seconds end |