Module: UsefulUtilities::Time

Extended by:
Time
Included in:
Time
Defined in:
lib/useful_utilities/time.rb

Overview

Author:

  • Virtuozzo

Constant Summary collapse

SECONDS_IN_HOUR =
3_600
MILLISECONDS_IN_SECOND =
1_000

Instance Method Summary collapse

Instance Method Details

#assume_utc(time) ⇒ DateTime

Returns time in UTC.

Examples:

time #=> 2013-12-19 14:01:22 +0200

UsefulUtilities::Time.assume_utc(time) #=> 2013-12-19 14:01:22 UTC

Parameters:

Returns:

  • (DateTime)

    time in UTC



19
20
21
# File 'lib/useful_utilities/time.rb', line 19

def assume_utc(time)
  time.to_datetime.change(offset: 0)
end

#beginning_of_next_day(time) ⇒ Time

Returns beginning of the next day.

Examples:

time #=> 2018-03-07 15:51:58 +0200
UsefulUtilities::Time.beginning_of_next_day(time) #=> 2018-03-08 00:00:00 +0200

Parameters:

Returns:

  • (Time)

    beginning of the next day



47
48
49
# File 'lib/useful_utilities/time.rb', line 47

def beginning_of_next_day(time)
  time.beginning_of_day.in(1.day)
end

#beginning_of_next_hour(time) ⇒ Time

Returns beginning of the next hour.

Examples:

time #=> 2018-03-07 15:51:58 +0200
UsefulUtilities::Time.beginning_of_next_hour(time) #=> 2018-03-07 16:00:00 +0200

Parameters:

Returns:

  • (Time)

    beginning of the next hour



38
39
40
# File 'lib/useful_utilities/time.rb', line 38

def beginning_of_next_hour(time)
  time.beginning_of_hour.in(1.hour)
end

#beginning_of_next_month(time) ⇒ Time

Returns beginning of the next month.

Examples:

time #=> 2018-03-07 15:51:58 +0200
UsefulUtilities::Time.beginning_of_next_month(time) #=> 2018-04-01 00:00:00 +0300

Parameters:

Returns:

  • (Time)

    beginning of the next month



56
57
58
# File 'lib/useful_utilities/time.rb', line 56

def beginning_of_next_month(time)
  time.beginning_of_month.in(1.month)
end

#diff_in_hours(end_time, start_time) ⇒ Integer

Returns difference in hours.

Examples:

end_time   #=> 2018-03-07 14:51:58 +0200
start_time #=> 2018-03-07 15:51:58 +0200

UsefulUtilities::Time.diff_in_hours(end_time, start_time) #=> 1

Parameters:

  • end_time (Time)

    end time

  • start_time (Time)

    start time

Returns:

  • (Integer)

    difference in hours



143
144
145
# File 'lib/useful_utilities/time.rb', line 143

def diff_in_hours(end_time, start_time)
  ((end_time.to_i - start_time.to_i) / SECONDS_IN_HOUR).abs
end

#each_day_from(from, till = ::Time.now) ⇒ Object

Yields local time of the beginning of each day between from & till

Parameters:

  • from (Time)
  • till (Time) (defaults to: ::Time.now)


79
80
81
82
83
84
85
86
87
88
# File 'lib/useful_utilities/time.rb', line 79

def each_day_from(from, till = ::Time.now)
  cursor = from.beginning_of_day
  end_time = till.beginning_of_day

  while cursor < end_time
    yield cursor, next_day = beginning_of_next_day(cursor)

    cursor = next_day
  end
end

#each_hour_from(from, till = ::Time.now) ⇒ Object

Yields local time of the beginning of each hour between from & till

Parameters:

  • from (Time)
  • till (Time) (defaults to: ::Time.now)


64
65
66
67
68
69
70
71
72
73
# File 'lib/useful_utilities/time.rb', line 64

def each_hour_from(from, till = ::Time.now)
  cursor = from.dup.utc.beginning_of_hour
  end_time = till.dup.utc.beginning_of_hour

  while cursor < end_time
    yield cursor, next_hour = beginning_of_next_hour(cursor)

    cursor = next_hour
  end
end

#round_to_hours(time) ⇒ Time

Returns time rounded to hours.

Examples:

time #=> 2014-01-04 14:29:59

UsefulUtilities::Time.round_to_hours(time) #=> 2014-01-04 14:00:00

Parameters:

Returns:

  • (Time)

    time rounded to hours



29
30
31
# File 'lib/useful_utilities/time.rb', line 29

def round_to_hours(time)
  (time.min < 30) ? time.beginning_of_hour : time.end_of_hour
end

#timestamp_to_seconds(timestamp) ⇒ Integer

Returns timestamp in seconds.

Examples:

timestamp #=> 1568629976000
UsefulUtilities::Time.timestamp_to_seconds(timestamp) #=> 1568629976

Parameters:

  • timestamp (Integer)

    timestamp in milliseconds

Returns:

  • (Integer)

    timestamp in seconds



131
132
133
# File 'lib/useful_utilities/time.rb', line 131

def timestamp_to_seconds(timestamp)
  timestamp / MILLISECONDS_IN_SECOND
end

#to_milliseconds(time) ⇒ Integer

Returns time in milliseconds.

Examples:

time #=> 2018-03-07 15:51:58 +0200
UsefulUtilities::Time.to_milliseconds(time) #=> 1520430718000

Parameters:

Returns:

  • (Integer)

    time in milliseconds



122
123
124
# File 'lib/useful_utilities/time.rb', line 122

def to_milliseconds(time)
  time.to_time.to_i * MILLISECONDS_IN_SECOND
end

#to_utc(time_string, time_zone) ⇒ DateTime

Parse and convert Time to the given time zone. If time zone is not given, Time is assumed to be given in UTC. Returns result in UTC

Examples:

time_string #=> "2018-03-07 15:51:58 +0200"
time_zone   #=> "EET"

UsefulUtilities::Time.to_utc(time_string, time_zone) #=> Wed, 07 Mar 2018 13:51:58 +0000

Parameters:

  • time_string (String)

    string to parse from

  • time_zone (String)

    timezone

Returns:

  • (DateTime)

    parsed time in UTC



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/useful_utilities/time.rb', line 103

def to_utc(time_string, time_zone)
  time = ::DateTime.parse(time_string) # Wed, 22 Aug 2012 13:34:18 +0000
rescue
  nil
else
  if time_zone.present?
    current_user_offset = ::Time.now.in_time_zone(::Time.find_zone(time_zone)).strftime("%z") # "+0300"
    time = time.change(:offset => current_user_offset) # Wed, 22 Aug 2012 13:34:18 +0300
    time.utc # Wed, 22 Aug 2012 10:34:18 +0000
  else
    time
  end
end

#valid_date?(date) ⇒ Boolean

Returns checks if date is valid.

Parameters:

  • date (Date)

Returns:

  • (Boolean)

    checks if date is valid



149
150
151
152
153
154
155
156
157
158
# File 'lib/useful_utilities/time.rb', line 149

def valid_date?(date)
  return true if date.acts_like?(:date)
  return false if date.blank?

  # http://stackoverflow.com/a/35502357/717336
  date_hash = Date._parse(date.to_s)
  Date.valid_date?(date_hash[:year].to_i,
                   date_hash[:mon].to_i,
                   date_hash[:mday].to_i)
end