Class: Time

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

Overview

Version:

  • 2.0.0

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:



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

.tomorrowTime

Returns Time object set 24 hours ago.

Returns:

  • (Time)

    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

Parameters:

  • value (String)

Returns:



62
63
64
# File 'lib/time-helper.rb', line 62

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

.valid_datetime?(string) ⇒ Boolean

Parameters:

  • string (String)

    datetime string

Returns:

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

.yesterdayTime

Return time object 24 hours from now

Returns:

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

Raises:



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.

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



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.

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



172
173
174
175
# File 'lib/time-helper.rb', line 172

def substract params
    seconds = get_seconds params
    return self - seconds
end