Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/time-helper.rb

Overview

Author:

  • Arthur

Version:

  • 1.3.1

Defined Under Namespace

Classes: GenericInputError, InvalidArgument, InvalidTimeStringFormat

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.strtotime(string) ⇒ Time

Returns set in the date of the string passed.

Parameters:

  • string (String)

    a datetime string with many possible formats

Returns:

  • (Time)

    set in the date of the string passed

See Also:



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

.tomorrowTime

Returns Time object set 24 hours ago.

Returns:

  • (Time)

    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

Parameters:

  • value (String)

Returns:



67
68
69
# File 'lib/time-helper.rb', line 67

def utc_parse value
    Time.strtotime(value).utc
end

.valid_datetime?(string) ⇒ Boolean

Parameters:

  • string (String)

    datetime string

Returns:

  • (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

.yesterdayTime

Return time object 24 hours from now

Returns:

  • (Time)

    return time object 24 hours from now



60
61
62
# File 'lib/time-helper.rb', line 60

def yesterday
    return Time.now.substract(:day => 1 )
end

Instance Method Details

#=~(other_time, smallest_unit = :hour) ⇒ Object

Note:

minute is :min seconds is :sec

Raises:



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.

Examples:

add time

t = Time.now()
t.add(:year => 1 ) #=> a time object set for 365 days from now
t.add(:year => 2, :day => 1 ) #=>  a time object set 731 days from now
accepts :year :month :day :minute :hour :second :week

Parameters:

  • params (Hash)

    hash of time to be added {:time => amount } (can take several at once)

Returns:

  • (Time)

    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.

Examples:

substract time

t = Time.now()
t.substract(:year => 1 ) returns a time object set for 365 days ago
t.substract(:year => 2, :day => 1 ) returns a time object set 731 ago
accepts :year :month :day :minute :hour :second :week

Parameters:

  • params (Hash)

    hash of time to be added {:time => amount } (can take several at once)

Returns:

  • (Time)

    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