Class: MegliHelper::TimeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/time_helper.rb

Class Method Summary collapse

Class Method Details

.change_timezone(time, timezone = '+00:00') ⇒ Time

Change timezone of a time passed

Parameters:

  • time (Time)
    String

    time to be changed

  • timezone (String) (defaults to: '+00:00')

    timezone to be set in the time

Returns:

  • (Time)

    time with a new timezone



9
10
11
12
# File 'lib/helpers/time_helper.rb', line 9

def self.change_timezone(time, timezone = '+00:00')
  time = Time.parse(time) if time.class == String
  time.localtime(timezone)
end

.format_date(time, format) ⇒ String

Format a time according to format passed by param

Parameters:

  • time (Time | String)

    time to be formatted

  • format (String)

    format used

Returns:



18
19
20
21
# File 'lib/helpers/time_helper.rb', line 18

def self.format_date(time, format)
  time = Time.parse(time) if time.class == String
  time.strftime(format)
end

.get_time(format = :iso8601, add_minutes = 0) ⇒ String

Get time now with addinional minutes and convert it to format passed

Parameters:

  • format (Symbol) (defaults to: :iso8601)

    time format. Default value: :iso8601

  • add_minutes (Integer) (defaults to: 0)

    quantity of minutes to sum

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/helpers/time_helper.rb', line 27

def self.get_time(format = :iso8601, add_minutes = 0)
  return nil if add_minutes.nil?
  add_minutes = add_minutes.to_i if add_minutes.class == String
  time = Time.now + (60 * add_minutes)
  unless format.nil?
    time = time.iso8601 if format == :iso8601
    time = time.utc if format == :utc
    time = time.rfc822 if format == :rfc822 || format == :rfc2822
    time = time.ctime if format == :ctime
  end
  time
end