Module: UsefulUtilities::Time
Overview
Constant Summary collapse
- SECONDS_IN_HOUR =
3_600
- MILLISECONDS_IN_SECOND =
1_000
Instance Method Summary collapse
-
#assume_utc(time) ⇒ DateTime
Time in UTC.
-
#beginning_of_next_day(time) ⇒ Time
Beginning of the next day.
-
#beginning_of_next_hour(time) ⇒ Time
Beginning of the next hour.
-
#beginning_of_next_month(time) ⇒ Time
Beginning of the next month.
-
#diff_in_hours(end_time, start_time) ⇒ Integer
Difference in hours.
-
#each_day_from(from, till = ::Time.now) ⇒ Object
Yields local time of the beginning of each day between from & till.
-
#each_hour_from(from, till = ::Time.now) ⇒ Object
Yields local time of the beginning of each hour between from & till.
-
#round_to_hours(time) ⇒ Time
Time rounded to hours.
-
#timestamp_to_seconds(timestamp) ⇒ Integer
Timestamp in seconds.
-
#to_milliseconds(time) ⇒ Integer
Time in milliseconds.
-
#to_utc(time_string, time_zone) ⇒ DateTime
Parse and convert Time to the given time zone.
-
#valid_date?(date) ⇒ Boolean
Checks if date is valid.
Instance Method Details
#assume_utc(time) ⇒ DateTime
Returns 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.
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.
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.
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.
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
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
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.
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.
131 132 133 |
# File 'lib/useful_utilities/time.rb', line 131 def () / MILLISECONDS_IN_SECOND end |
#to_milliseconds(time) ⇒ Integer
Returns 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
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.
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 |