Module: GreenButtonData::Utilities
- Included in:
- ApplicationInformation, Authorization, Dst, Fetchable::ClassMethods, IntervalBlock, Parser::ApplicationInformation, Parser::Authorization, Parser::Entry, Parser::Feed, Parser::Interval, Parser::LocalTimeParameters, Parser::UsageSummary, UsageSummary
- Defined in:
- lib/green-button-data/utilities.rb
Instance Method Summary collapse
-
#attributes_to_hash(obj) ⇒ Object
Returns a hash representation of object instance’s attributes.
- #epoch_to_time(epoch, kwargs = {}) ⇒ Object
-
#first_sunday_of(year = Time.now.year, month = Time.now.month) ⇒ Object
Retrieves the first Sunday of the month.
-
#last_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0) ⇒ Object
Returns the last weekday in the given month.
-
#normalize_epoch(epoch) ⇒ Object
Normalizes UNIX
epochticks to seconds. -
#nth_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0, week = 1) ⇒ Object
Returns the Nth weekday in the given month.
- #parse_datetime(string) ⇒ Object
Instance Method Details
#attributes_to_hash(obj) ⇒ Object
Returns a hash representation of object instance’s attributes
Arguments
-
obj- an object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/green-button-data/utilities.rb', line 122 def attributes_to_hash(obj) attributes_hash = {} obj.instance_variables.each do |var| attr_name = var.to_s.delete('@').to_sym attributes_hash[attr_name] = obj.instance_variable_get(var) end return attributes_hash end |
#epoch_to_time(epoch, kwargs = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/green-button-data/utilities.rb', line 37 def epoch_to_time(epoch, kwargs = {}) time = Time.at normalize_epoch(epoch) if kwargs[:local] == true return time.localtime else return time.utc end end |
#first_sunday_of(year = Time.now.year, month = Time.now.month) ⇒ Object
Retrieves the first Sunday of the month
Arguments
-
year- year -
month- month
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/green-button-data/utilities.rb', line 54 def first_sunday_of(year = Time.now.year, month = Time.now.month) first_day = DateTime.new(year, month, 1) first_weekday = first_day.wday # If today is Sunday, no offset, otherwise, calculate number of days that # need to be added before hitting the first Sunday of month day_offset = first_weekday == 0 ? 0 : 7 - first_weekday # Return first Monday of the month first_day + day_offset end |
#last_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0) ⇒ Object
Returns the last weekday in the given month
Arguments
-
year- year -
month- month -
weekday- day of week; 0 = Sunday, 6 = Saturday
Examples
To retrieve the last Wednesday of September 2015,
last_weekday_of 2015, 9, 3
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/green-button-data/utilities.rb', line 100 def last_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0) # Get the last day of month last_day = DateTime.new year, month, -1 last_weekday = last_day.wday day_offset = if last_weekday >= weekday last_weekday - weekday else 7 + last_weekday - weekday end last_day - day_offset end |
#normalize_epoch(epoch) ⇒ Object
Normalizes UNIX epoch ticks to seconds.
If the number of digits for epoch are greater than or equal to 15 decimal digits, it assumes that the epoch is in microseconds.
If the number of digits for epoch are less than 15 but greater than or equal to 13 decimal digits, it assumes that epoch is in milliseconds.
Less than 13 digits is assumed to be in seconds.
Arguments
-
epoch- Amount of seconds/milliseconds/microseconds since 1970-01-01
27 28 29 30 31 32 33 34 35 |
# File 'lib/green-button-data/utilities.rb', line 27 def normalize_epoch(epoch) if epoch.digits >= 15 epoch / 100000 elsif epoch.digits >= 13 epoch / 1000 else epoch end end |
#nth_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0, week = 1) ⇒ Object
Returns the Nth weekday in the given month
Arguments
-
year- year -
month- month -
weekday- day of week; 0 = Sunday, 6 = Saturday -
week- Nth week of month
Examples
To retrieve third Friday of July 2015,
nth_weekday_of 2015, 7, 5, 3
80 81 82 83 84 85 |
# File 'lib/green-button-data/utilities.rb', line 80 def nth_weekday_of(year = Time.now.year, month = Time.now.month, weekday = 0, week = 1) # Day offset needed for Nth day of the week first_sunday_of(year, month) + weekday + (week - 1) * 7 end |
#parse_datetime(string) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/green-button-data/utilities.rb', line 4 def parse_datetime(string) begin DateTime.parse(string).utc rescue warn "Parsing failed for string: #{string.inspect}" nil end end |